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

12345678910111213141516171819202122232425262728293031323334353637383940
  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. """
  10. Infering object from images
  11. """
  12. def on_event(
  13. self,
  14. dora_event,
  15. send_output,
  16. ) -> DoraStatus:
  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