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

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