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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import numpy as np
  4. import pyarrow as pa
  5. from dora import DoraStatus
  6. from ultralytics import YOLO
  7. pa.array([])
  8. CAMERA_WIDTH = 640
  9. CAMERA_HEIGHT = 480
  10. class Operator:
  11. """
  12. Infering object from images
  13. """
  14. def __init__(self):
  15. self.model = YOLO("yolov8n.pt")
  16. def on_event(
  17. self,
  18. dora_event,
  19. send_output,
  20. ) -> DoraStatus:
  21. if dora_event["type"] == "INPUT":
  22. return self.on_input(dora_event, send_output)
  23. return DoraStatus.CONTINUE
  24. def on_input(
  25. self,
  26. dora_input,
  27. send_output,
  28. ) -> DoraStatus:
  29. """Handle image
  30. Args:
  31. dora_input (dict) containing the "id", value, and "metadata"
  32. send_output Callable[[str, bytes | pa.Array, Optional[dict]], None]:
  33. Function for sending output to the dataflow:
  34. - First argument is the `output_id`
  35. - Second argument is the data as either bytes or `pa.Array`
  36. - Third argument is dora metadata dict
  37. e.g.: `send_output("bbox", pa.array([100], type=pa.uint8()), dora_event["metadata"])`
  38. """
  39. frame = dora_input["value"].to_numpy().reshape((CAMERA_HEIGHT, CAMERA_WIDTH, 3))
  40. frame = frame[:, :, ::-1] # OpenCV image (BGR to RGB)
  41. results = self.model(frame) # includes NMS
  42. # Process results
  43. boxes = np.array(results[0].boxes.xyxy.cpu())
  44. conf = np.array(results[0].boxes.conf.cpu())
  45. label = np.array(results[0].boxes.cls.cpu())
  46. # concatenate them together
  47. arrays = np.concatenate((boxes, conf[:, None], label[:, None]), axis=1)
  48. send_output("bbox", pa.array(arrays.ravel()), dora_input["metadata"])
  49. return DoraStatus.CONTINUE