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

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