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

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