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. 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. print("before model")
  29. results = self.model(frame) # includes NMS
  30. print("after model")
  31. arrays = np.array(results.xyxy[0].cpu()).tobytes()
  32. send_output("bbox", arrays, dora_input["metadata"])
  33. print("after send output")
  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