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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. CI = os.environ.get("CI")
  9. font = cv2.FONT_HERSHEY_SIMPLEX
  10. IMAGE_WIDTH = int(os.getenv("IMAGE_WIDTH", 960))
  11. IMAGE_HEIGHT = int(os.getenv("IMAGE_HEIGHT", 540))
  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,
  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["value"] (arrow array): message of the dora_input
  28. """
  29. if dora_input["id"] == "image":
  30. image = (
  31. dora_input["value"].to_numpy().reshape((IMAGE_HEIGHT, IMAGE_WIDTH, 3))
  32. )
  33. image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
  34. self.image = image
  35. elif dora_input["id"] == "bbox" and len(self.image) != 0:
  36. bboxs = dora_input["value"].to_numpy()
  37. self.bboxs = np.reshape(bboxs, (-1, 6))
  38. for bbox in self.bboxs:
  39. [
  40. x,
  41. y,
  42. w,
  43. h,
  44. confidence,
  45. label,
  46. ] = bbox
  47. cv2.rectangle(
  48. self.image,
  49. (int(x - w / 2), int(y - h / 2)),
  50. (int(x + w / 2), int(y + h / 2)),
  51. (0, 255, 0),
  52. 2,
  53. )
  54. if CI != "true":
  55. cv2.imshow("frame", self.image)
  56. if cv2.waitKey(1) & 0xFF == ord("q"):
  57. return DoraStatus.STOP
  58. return DoraStatus.CONTINUE
  59. plotter = Plotter()
  60. node = Node()
  61. for event in node:
  62. event_type = event["type"]
  63. if event_type == "INPUT":
  64. status = plotter.on_input(event)
  65. if status == DoraStatus.CONTINUE:
  66. pass
  67. elif status == DoraStatus.STOP:
  68. print("plotter returned stop status")
  69. break
  70. elif event_type == "STOP":
  71. print("received stop")
  72. else:
  73. print("received unexpected event:", event_type)