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

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