Add draw_timstamp: Helper for timestamp mods

This commit is contained in:
Sebastian Englbrecht 2021-04-28 19:22:48 +02:00 committed by Blake Blackshear
parent 8e0c2b256e
commit 7bfe8e4f5b

View File

@ -19,6 +19,79 @@ import numpy as np
logger = logging.getLogger(__name__)
def draw_timestamp(
frame,
timestamp,
timestamp_format,
font_effect=None,
font_scale=1.0,
font_thickness=2,
font_color=(255, 255, 255),
position="ul",
):
time_to_show = datetime.datetime.fromtimestamp(
timestamp
).strftime(timestamp_format)
size = cv2.getTextSize(
time_to_show,
cv2.FONT_HERSHEY_SIMPLEX,
fontScale = font_scale,
thickness = font_thickness
)
image_width = frame.shape[1]
image_height = frame.shape[0]
text_width = size[0][0]
text_height = size[0][1]
line_height = text_height + size[1]
if position == "ul":
text_offset_x = 0
text_offset_y = 0 if 0 < line_height else 0 - (line_height + 8)
elif position == "ur":
text_offset_x = image_width - text_width
text_offset_y = 0 if 0 < line_height else 0 - (line_height + 8)
elif position == "bl":
text_offset_x = 0
text_offset_y = image_height - (line_height + 8)
elif position == "br":
text_offset_x = image_width - text_width
text_offset_y = image_height - (line_height + 8)
if font_effect == "solid":
# make the coords of the box with a small padding of two pixels
timestamp_box_coords = np.array([
[text_offset_x, text_offset_y],
[text_offset_x+text_width, text_offset_y],
[text_offset_x+text_width, text_offset_y + line_height+ 8],
[text_offset_x, text_offset_y + line_height+ 8],
])
cv2.fillPoly(
frame,
[timestamp_box_coords],
# inverse color of text for background for max. contrast
(255-font_color[0], 255-font_color[1], 255-font_color[2]),
)
elif font_effect == "shadow":
cv2.putText(
frame,
time_to_show,
(text_offset_x + 3, text_offset_y + line_height),
cv2.FONT_HERSHEY_SIMPLEX,
fontScale = font_scale,
color = (255-font_color[0], 255-font_color[1], 255-font_color[2]),
thickness = font_thickness,
)
cv2.putText(
frame,
time_to_show,
(text_offset_x, text_offset_y + line_height - 3),
cv2.FONT_HERSHEY_SIMPLEX,
fontScale = font_scale,
color = font_color,
thickness = font_thickness,
)
def draw_box_with_label(
frame,