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

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

DORA (Dataflow-Oriented Robotic Architecture) is middleware designed to streamline and simplify the creation of AI-based robotic applications. It offers low latency, composable, and distributed datafl