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