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.3 kB

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