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.

object_detection.py 1.1 kB

10 months ago
10 months ago
10 months ago
123456789101112131415161718192021222324252627282930313233343536373839
  1. """TODO: Add docstring."""
  2. import numpy as np
  3. import pyarrow as pa
  4. from dora import DoraStatus
  5. from ultralytics import YOLO
  6. CAMERA_WIDTH = 640
  7. CAMERA_HEIGHT = 480
  8. model = YOLO("yolov8n.pt")
  9. class Operator:
  10. """Inferring object from images."""
  11. def on_event(
  12. self,
  13. dora_event,
  14. send_output,
  15. ) -> DoraStatus:
  16. """TODO: Add docstring."""
  17. if dora_event["type"] == "INPUT":
  18. frame = (
  19. dora_event["value"].to_numpy().reshape((CAMERA_HEIGHT, CAMERA_WIDTH, 3))
  20. )
  21. frame = frame[:, :, ::-1] # OpenCV image (BGR to RGB)
  22. results = model(frame, verbose=False) # includes NMS
  23. # Process results
  24. boxes = np.array(results[0].boxes.xyxy.cpu())
  25. conf = np.array(results[0].boxes.conf.cpu())
  26. label = np.array(results[0].boxes.cls.cpu())
  27. # concatenate them together
  28. arrays = np.concatenate((boxes, conf[:, None], label[:, None]), axis=1)
  29. send_output("bbox", pa.array(arrays.ravel()), dora_event["metadata"])
  30. return DoraStatus.CONTINUE