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

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