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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import os
  4. from typing import Callable
  5. from dora import Node
  6. from dora import DoraStatus
  7. import cv2
  8. import numpy as np
  9. from utils import LABELS
  10. CI = os.environ.get("CI")
  11. font = cv2.FONT_HERSHEY_SIMPLEX
  12. class Plotter:
  13. """
  14. Plot image and bounding box
  15. """
  16. def __init__(self):
  17. self.image = []
  18. self.bboxs = []
  19. def on_input(
  20. self,
  21. dora_input: dict,
  22. ) -> DoraStatus:
  23. """
  24. Put image and bounding box on cv2 window.
  25. Args:
  26. dora_input["id"] (str): Id of the dora_input declared in the yaml configuration
  27. dora_input["data"] (bytes): Bytes message of the dora_input
  28. """
  29. if dora_input["id"] == "image":
  30. frame = np.frombuffer(dora_input["data"], dtype="uint8")
  31. frame = cv2.imdecode(frame, -1)
  32. self.image = frame
  33. elif dora_input["id"] == "bbox" and len(self.image) != 0:
  34. bboxs = np.frombuffer(dora_input["data"], dtype="float32")
  35. self.bboxs = np.reshape(bboxs, (-1, 6))
  36. for bbox in self.bboxs:
  37. [
  38. min_x,
  39. min_y,
  40. max_x,
  41. max_y,
  42. confidence,
  43. label,
  44. ] = bbox
  45. cv2.rectangle(
  46. self.image,
  47. (int(min_x), int(min_y)),
  48. (int(max_x), int(max_y)),
  49. (0, 255, 0),
  50. 2,
  51. )
  52. cv2.putText(
  53. self.image,
  54. LABELS[int(label)] + f", {confidence:0.2f}",
  55. (int(max_x), int(max_y)),
  56. font,
  57. 0.75,
  58. (0, 255, 0),
  59. 2,
  60. 1,
  61. )
  62. if CI != "true":
  63. cv2.imshow("frame", self.image)
  64. if cv2.waitKey(1) & 0xFF == ord("q"):
  65. return DoraStatus.STOP
  66. return DoraStatus.CONTINUE
  67. plotter = Plotter()
  68. node = Node()
  69. for event in node:
  70. match event["type"]:
  71. case "INPUT":
  72. status = plotter.on_input(event)
  73. match status:
  74. case DoraStatus.CONTINUE:
  75. pass
  76. case DoraStatus.STOP:
  77. print("plotter returned stop status")
  78. break
  79. case "STOP":
  80. print("received stop")
  81. case other:
  82. print("received unexpected event:", other)

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