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

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