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

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import cv2
  5. import numpy as np
  6. from ultralytics import YOLO
  7. from dora import Node
  8. import pyarrow as pa
  9. model = YOLO("yolov8n.pt")
  10. node = Node()
  11. IMAGE_WIDTH = int(os.getenv("IMAGE_WIDTH", 960))
  12. IMAGE_HEIGHT = int(os.getenv("IMAGE_HEIGHT", 540))
  13. for event in node:
  14. event_type = event["type"]
  15. if event_type == "INPUT":
  16. event_id = event["id"]
  17. if event_id == "image":
  18. print("[object detection] received image input")
  19. image = event["value"].to_numpy().reshape((IMAGE_HEIGHT, IMAGE_WIDTH, 3))
  20. frame = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
  21. frame = frame[:, :, ::-1] # OpenCV image (BGR to RGB)
  22. results = model(frame) # includes NMS
  23. # Process results
  24. boxes = np.array(results[0].boxes.xywh.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. node.send_output("bbox", pa.array(arrays.ravel()), event["metadata"])
  30. else:
  31. print("[object detection] ignoring unexpected input:", event_id)
  32. elif event_type == "STOP":
  33. print("[object detection] received stop")
  34. elif event_type == "ERROR":
  35. print("[object detection] error: ", event["error"])
  36. else:
  37. print("[object detection] received unexpected event:", event_type)