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

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. dora_input: dict,
  18. send_output: Callable[[str, bytes], None],
  19. ) -> DoraStatus:
  20. """Handle image
  21. Args:
  22. dora_input (dict): Dict containing the "id", "data", and "metadata"
  23. send_output (Callable[[str, bytes]]): Function enabling sending output back to dora.
  24. """
  25. frame = np.frombuffer(dora_input["data"], dtype="uint8")
  26. frame = cv2.imdecode(frame, -1)
  27. frame = frame[:, :, ::-1] # OpenCV image (BGR to RGB)
  28. results = self.model(frame) # includes NMS
  29. arrays = np.array(results.xyxy[0].cpu()).tobytes()
  30. send_output("bbox", arrays, dora_input["metadata"])
  31. 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