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