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

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