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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import os
  2. from enum import Enum
  3. from typing import Callable
  4. import cv2
  5. import numpy as np
  6. from utils import LABELS
  7. CI = os.environ.get("CI")
  8. font = cv2.FONT_HERSHEY_SIMPLEX
  9. class DoraStatus(Enum):
  10. CONTINUE = 0
  11. STOP = 1
  12. class Operator:
  13. """
  14. Plot image and bounding box
  15. """
  16. def __init__(self):
  17. self.image = []
  18. self.bboxs = []
  19. self.bounding_box_messages = 0
  20. self.image_messages = 0
  21. def on_event(
  22. self,
  23. dora_event: dict,
  24. send_output: Callable[[str, bytes], None],
  25. ) -> DoraStatus:
  26. if dora_event["type"] == "INPUT":
  27. return self.on_input(dora_event, send_output)
  28. return DoraStatus.CONTINUE
  29. def on_input(
  30. self,
  31. dora_input: dict,
  32. send_output: Callable[[str, bytes], None],
  33. ) -> DoraStatus:
  34. """
  35. Put image and bounding box on cv2 window.
  36. Args:
  37. dora_input["id"] (str): Id of the dora_input declared in the yaml configuration
  38. dora_input["data"] (bytes): Bytes message of the dora_input
  39. send_output (Callable[[str, bytes]]): Function enabling sending output back to dora.
  40. """
  41. if dora_input["id"] == "image":
  42. frame = np.frombuffer(dora_input["data"], dtype="uint8")
  43. frame = cv2.imdecode(frame, -1)
  44. self.image = frame
  45. self.image_messages += 1
  46. print("received " + str(self.image_messages) + " images")
  47. elif dora_input["id"] == "bbox" and len(self.image) != 0:
  48. bboxs = np.frombuffer(dora_input["data"], dtype="float32")
  49. self.bboxs = np.reshape(bboxs, (-1, 6))
  50. self.bounding_box_messages += 1
  51. print("received " + str(self.bounding_box_messages) + " bounding boxes")
  52. for bbox in self.bboxs:
  53. [
  54. min_x,
  55. min_y,
  56. max_x,
  57. max_y,
  58. confidence,
  59. label,
  60. ] = bbox
  61. cv2.rectangle(
  62. self.image,
  63. (int(min_x), int(min_y)),
  64. (int(max_x), int(max_y)),
  65. (0, 255, 0),
  66. 2,
  67. )
  68. cv2.putText(
  69. self.image,
  70. LABELS[int(label)] + f", {confidence:0.2f}",
  71. (int(max_x), int(max_y)),
  72. font,
  73. 0.75,
  74. (0, 255, 0),
  75. 2,
  76. 1,
  77. )
  78. if CI != "true":
  79. cv2.imshow("frame", self.image)
  80. if cv2.waitKey(1) & 0xFF == ord("q"):
  81. return DoraStatus.STOP
  82. return DoraStatus.CONTINUE
  83. def __del__(self):
  84. 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