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_dynamic.py 2.4 kB

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