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

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. Infering object from images
  12. """
  13. def __init__(self):
  14. self.model = torch.hub.load("ultralytics/yolov5", "yolov5n")
  15. def on_input(
  16. self,
  17. input_id: str,
  18. value: bytes,
  19. send_output: Callable[[str, bytes], None],
  20. ) -> DoraStatus:
  21. """Handle image
  22. Args:
  23. input_id (str): Id of the input declared in the yaml configuration
  24. value (bytes): Bytes message of the input
  25. send_output (Callable[[str, bytes]]): Function enabling sending output back to dora.
  26. """
  27. frame = np.frombuffer(value, dtype="uint8")
  28. frame = cv2.imdecode(frame, -1)
  29. frame = frame[:, :, ::-1] # OpenCV image (BGR to RGB)
  30. results = self.model(frame) # includes NMS
  31. arrays = np.array(results.xyxy[0].cpu()).tobytes()
  32. send_output("bbox", arrays)
  33. 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