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

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