util.py 1.1 KB

1234567891011121314151617181920212223242526
  1. import numpy as np
  2. import cv2
  3. # convert shared memory array into numpy array
  4. def tonumpyarray(mp_arr):
  5. return np.frombuffer(mp_arr.get_obj(), dtype=np.uint8)
  6. def draw_box_with_label(frame, x_min, y_min, x_max, y_max, label):
  7. color = (255,0,0)
  8. cv2.rectangle(frame, (x_min, y_min),
  9. (x_max, y_max),
  10. color, 2)
  11. font_scale = 0.5
  12. font = cv2.FONT_HERSHEY_SIMPLEX
  13. # get the width and height of the text box
  14. size = cv2.getTextSize(label, font, fontScale=font_scale, thickness=2)
  15. text_width = size[0][0]
  16. text_height = size[0][1]
  17. line_height = text_height + size[1]
  18. # set the text start position
  19. text_offset_x = x_min
  20. text_offset_y = 0 if y_min < line_height else y_min - (line_height+8)
  21. # make the coords of the box with a small padding of two pixels
  22. textbox_coords = ((text_offset_x, text_offset_y), (text_offset_x + text_width + 2, text_offset_y + line_height))
  23. cv2.rectangle(frame, textbox_coords[0], textbox_coords[1], color, cv2.FILLED)
  24. cv2.putText(frame, label, (text_offset_x, text_offset_y + line_height - 3), font, fontScale=font_scale, color=(0, 0, 0), thickness=2)