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

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from enum import Enum
  2. from typing import Callable
  3. import cv2
  4. import numpy as np
  5. import torch
  6. class DoraStatus(Enum):
  7. CONTINUE = 0
  8. STOP = 1
  9. class Operator:
  10. """
  11. Example operator incrementing a counter every times its been called.
  12. The current value of the counter is sent back to dora on `counter`.
  13. """
  14. def __init__(self):
  15. self.model = torch.hub.load("ultralytics/yolov5", "yolov5n")
  16. def on_input(
  17. self,
  18. input_id: str,
  19. value: bytes,
  20. send_output: Callable[[str, bytes], None],
  21. ) -> DoraStatus:
  22. """Handle input by incrementing count by one.
  23. Args:
  24. input_id (str): Id of the input declared in the yaml configuration
  25. value (bytes): Bytes message of the input
  26. send_output (Callable[[str, bytes]]): Function enabling sending output back to dora.
  27. """
  28. frame = np.frombuffer(value, dtype="uint8")
  29. frame = cv2.imdecode(frame, -1)
  30. frame = frame[:, :, ::-1] # OpenCV image (BGR to RGB)
  31. results = self.model(frame) # includes NMS
  32. arrays = np.array(results.xyxy[0].cpu()).tobytes()
  33. send_output("bbox", arrays)
  34. 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