TensorRT-Demo/utils/display.py

77 lines
2.2 KiB
Python

"""display.py
"""
import time
import cv2
def open_window(window_name, title, width=None, height=None):
"""Open the display window."""
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.setWindowTitle(window_name, title)
if width and height:
cv2.resizeWindow(window_name, width, height)
def show_help_text(img, help_text):
"""Draw help text on image."""
cv2.putText(img, help_text, (11, 20), cv2.FONT_HERSHEY_PLAIN, 1.0,
(32, 32, 32), 4, cv2.LINE_AA)
cv2.putText(img, help_text, (10, 20), cv2.FONT_HERSHEY_PLAIN, 1.0,
(240, 240, 240), 1, cv2.LINE_AA)
return img
def show_fps(img, fps):
"""Draw fps number at top-left corner of the image."""
font = cv2.FONT_HERSHEY_PLAIN
line = cv2.LINE_AA
fps_text = 'FPS: {:.2f}'.format(fps)
cv2.putText(img, fps_text, (11, 20), font, 1.0, (32, 32, 32), 4, line)
cv2.putText(img, fps_text, (10, 20), font, 1.0, (240, 240, 240), 1, line)
return img
def set_display(window_name, full_scrn):
"""Set disply window to either full screen or normal."""
if full_scrn:
cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)
else:
cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_NORMAL)
class FpsCalculator():
"""Helper class for calculating frames-per-second (FPS)."""
def __init__(self, decay_factor=0.95):
self.fps = 0.0
self.tic = time.time()
self.decay_factor = decay_factor
def update(self):
toc = time.time()
curr_fps = 1.0 / (toc - self.tic)
self.fps = curr_fps if self.fps == 0.0 else self.fps
self.fps = self.fps * self.decay_factor + \
curr_fps * (1 - self.decay_factor)
self.tic = toc
return self.fps
def reset(self):
self.fps = 0.0
class ScreenToggler():
"""Helper class for toggling between non-fullscreen and fullscreen."""
def __init__(self):
self.full_scrn = False
def toggle(self):
self.full_scrn = not self.full_scrn
set_display(WINDOW_NAME, self.full_scrn)