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 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import os
  4. from typing import Callable
  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], 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], 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]]): Function enabling sending output back to dora.
  43. """
  44. if dora_input["id"] == "image":
  45. frame = (
  46. dora_input["value"]
  47. .to_numpy()
  48. .reshape((CAMERA_HEIGHT, CAMERA_WIDTH, 3))
  49. )
  50. self.image = frame
  51. self.image_messages += 1
  52. print("received " + str(self.image_messages) + " images")
  53. elif dora_input["id"] == "bbox" and len(self.image) != 0:
  54. bboxs = dora_input["value"].to_numpy().view(np.float32)
  55. self.bboxs = np.reshape(bboxs, (-1, 6))
  56. self.bounding_box_messages += 1
  57. print(
  58. "received "
  59. + str(self.bounding_box_messages)
  60. + " bounding boxes"
  61. )
  62. for bbox in self.bboxs:
  63. [
  64. min_x,
  65. min_y,
  66. max_x,
  67. max_y,
  68. confidence,
  69. label,
  70. ] = bbox
  71. cv2.rectangle(
  72. self.image,
  73. (int(min_x), int(min_y)),
  74. (int(max_x), int(max_y)),
  75. (0, 255, 0),
  76. 2,
  77. )
  78. cv2.putText(
  79. self.image,
  80. LABELS[int(label)] + f", {confidence:0.2f}",
  81. (int(max_x), int(max_y)),
  82. font,
  83. 0.75,
  84. (0, 255, 0),
  85. 2,
  86. 1,
  87. )
  88. if CI != "true":
  89. cv2.imshow("frame", self.image)
  90. if cv2.waitKey(1) & 0xFF == ord("q"):
  91. return DoraStatus.STOP
  92. return DoraStatus.CONTINUE
  93. def __del__(self):
  94. 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