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

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import cv2
  4. import numpy as np
  5. from ultralytics import YOLO
  6. from dora import Node
  7. import pyarrow as pa
  8. model = YOLO("yolov8n.pt")
  9. node = Node()
  10. for event in node:
  11. event_type = event["type"]
  12. if event_type == "INPUT":
  13. event_id = event["id"]
  14. if event_id == "image":
  15. print("[object detection] received image input")
  16. frame = event["value"].to_numpy()
  17. frame = cv2.imdecode(frame, -1)
  18. frame = frame[:, :, ::-1] # OpenCV image (BGR to RGB)
  19. results = model(frame) # includes NMS
  20. # Process results
  21. boxes = np.array(results[0].boxes.xyxy.cpu())
  22. conf = np.array(results[0].boxes.conf.cpu())
  23. label = np.array(results[0].boxes.cls.cpu())
  24. # concatenate them together
  25. arrays = np.concatenate((boxes, conf[:, None], label[:, None]), axis=1)
  26. node.send_output("bbox", pa.array(arrays.ravel()), event["metadata"])
  27. else:
  28. print("[object detection] ignoring unexpected input:", event_id)
  29. elif event_type == "STOP":
  30. print("[object detection] received stop")
  31. elif event_type == "ERROR":
  32. print("[object detection] error: ", event["error"])
  33. else:
  34. print("[object detection] received unexpected event:", event_type)