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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import cv2
  5. import numpy as np
  6. import pyarrow as pa
  7. from dora import DoraStatus
  8. from utils import LABELS
  9. pa.array([])
  10. CI = os.environ.get("CI")
  11. CAMERA_WIDTH = 640
  12. CAMERA_HEIGHT = 480
  13. font = cv2.FONT_HERSHEY_SIMPLEX
  14. class Operator:
  15. """
  16. Plot image and bounding box
  17. """
  18. def __init__(self):
  19. self.image = []
  20. self.bboxs = []
  21. self.bounding_box_messages = 0
  22. self.image_messages = 0
  23. def on_event(
  24. self,
  25. dora_event,
  26. send_output,
  27. ) -> DoraStatus:
  28. if dora_event["type"] == "INPUT":
  29. return self.on_input(dora_event, send_output)
  30. return DoraStatus.CONTINUE
  31. def on_input(
  32. self,
  33. dora_input,
  34. send_output,
  35. ) -> DoraStatus:
  36. """
  37. Put image and bounding box on cv2 window.
  38. Args:
  39. dora_input["id"] (str): Id of the dora_input declared in the yaml configuration
  40. dora_input["value"] (arrow array): message of the dora_input
  41. send_output Callable[[str, bytes | pa.Array, Optional[dict]], None]:
  42. Function for sending output to the dataflow:
  43. - First argument is the `output_id`
  44. - Second argument is the data as either bytes or `pa.Array`
  45. - Third argument is dora metadata dict
  46. e.g.: `send_output("bbox", pa.array([100], type=pa.uint8()), dora_event["metadata"])`
  47. """
  48. if dora_input["id"] == "image":
  49. frame = (
  50. dora_input["value"]
  51. .to_numpy()
  52. .reshape((CAMERA_HEIGHT, CAMERA_WIDTH, 3))
  53. .copy() # copy the image because we want to modify it below
  54. )
  55. self.image = frame
  56. self.image_messages += 1
  57. print("received " + str(self.image_messages) + " images")
  58. elif dora_input["id"] == "bbox" and len(self.image) != 0:
  59. bboxs = dora_input["value"].to_numpy()
  60. self.bboxs = np.reshape(bboxs, (-1, 6))
  61. self.bounding_box_messages += 1
  62. print("received " + str(self.bounding_box_messages) + " bounding boxes")
  63. for bbox in self.bboxs:
  64. [
  65. min_x,
  66. min_y,
  67. max_x,
  68. max_y,
  69. confidence,
  70. label,
  71. ] = bbox
  72. cv2.rectangle(
  73. self.image,
  74. (int(min_x), int(min_y)),
  75. (int(max_x), int(max_y)),
  76. (0, 255, 0),
  77. 2,
  78. )
  79. cv2.putText(
  80. self.image,
  81. LABELS[int(label)] + f", {confidence:0.2f}",
  82. (int(max_x), int(max_y)),
  83. font,
  84. 0.75,
  85. (0, 255, 0),
  86. 2,
  87. 1,
  88. )
  89. if CI != "true":
  90. cv2.imshow("frame", self.image)
  91. if cv2.waitKey(1) & 0xFF == ord("q"):
  92. return DoraStatus.STOP
  93. return DoraStatus.CONTINUE
  94. def __del__(self):
  95. cv2.destroyAllWindows()

DORA (Dataflow-Oriented Robotic Architecture) is middleware designed to streamline and simplify the creation of AI-based robotic applications. It offers low latency, composable, and distributed datafl