You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

plot.py 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import cv2
  2. from dora import DoraStatus
  3. from utils import LABELS, put_text, CAMERA_HEIGHT, CAMERA_WIDTH, FONT, CI
  4. class Operator:
  5. """
  6. Plot image and bounding box
  7. """
  8. def __init__(self):
  9. self.bboxs = []
  10. self.buffer = ""
  11. self.submitted = []
  12. self.lines = []
  13. def on_event(
  14. self,
  15. dora_event,
  16. send_output,
  17. ):
  18. if dora_event["type"] == "INPUT":
  19. id = dora_event["id"]
  20. value = dora_event["value"]
  21. if id == "image":
  22. image = (
  23. value.to_numpy().reshape((CAMERA_HEIGHT, CAMERA_WIDTH, 3)).copy()
  24. )
  25. for bbox in self.bboxs:
  26. [
  27. min_x,
  28. min_y,
  29. max_x,
  30. max_y,
  31. confidence,
  32. label,
  33. ] = bbox
  34. cv2.rectangle(
  35. image,
  36. (int(min_x), int(min_y)),
  37. (int(max_x), int(max_y)),
  38. (0, 255, 0),
  39. )
  40. cv2.putText(
  41. image,
  42. f"{LABELS[int(label)]}, {confidence:0.2f}",
  43. (int(max_x), int(max_y)),
  44. FONT,
  45. 0.45,
  46. (0, 255, 0),
  47. 2,
  48. 1,
  49. )
  50. put_text(
  51. image,
  52. self.buffer,
  53. (20, 12 * 25),
  54. (190, 250, 0),
  55. )
  56. for i, text in enumerate(self.submitted[::-1]):
  57. put_text(
  58. image,
  59. text["content"],
  60. (20, 25 + (10 - i) * 25),
  61. (0, 255, 190),
  62. )
  63. for line in self.lines:
  64. cv2.line(
  65. image,
  66. (int(line[0]), int(line[1])),
  67. (int(line[2]), int(line[3])),
  68. (0, 0, 255),
  69. 2,
  70. )
  71. if CI != "true":
  72. cv2.imshow("frame", image)
  73. if cv2.waitKey(1) & 0xFF == ord("q"):
  74. return DoraStatus.STOP
  75. elif id == "bbox":
  76. self.bboxs = value.to_numpy().reshape((-1, 6))
  77. elif id == "keyboard_buffer":
  78. self.buffer = value[0].as_py()
  79. elif id == "line":
  80. self.lines += [value.to_pylist()]
  81. elif "message" in id:
  82. self.submitted += [
  83. {
  84. "role": id,
  85. "content": value[0].as_py(),
  86. }
  87. ]
  88. return DoraStatus.CONTINUE