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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from typing import Callable
  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], 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], None],
  30. ) -> DoraStatus:
  31. """Handle image
  32. Args:
  33. dora_input (dict): Dict containing the "id", "data", and "metadata"
  34. send_output (Callable[[str, bytes]]): Function enabling sending output back to dora.
  35. """
  36. frame = (
  37. dora_input["value"]
  38. .to_numpy()
  39. .reshape((CAMERA_HEIGHT, CAMERA_WIDTH, 3))
  40. )
  41. frame = frame[:, :, ::-1] # OpenCV image (BGR to RGB)
  42. results = self.model(frame) # includes NMS
  43. arrays = pa.array(
  44. np.array(results.xyxy[0].cpu()).ravel().view(np.uint8)
  45. )
  46. send_output("bbox", arrays, dora_input["metadata"])
  47. 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