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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from enum import Enum
  4. from typing import Callable
  5. import cv2
  6. import numpy as np
  7. import torch
  8. class DoraStatus(Enum):
  9. CONTINUE = 0
  10. STOP = 1
  11. class Operator:
  12. """
  13. Infering object from images
  14. """
  15. def __init__(self):
  16. self.model = torch.hub.load("ultralytics/yolov5", "yolov5n")
  17. def on_event(
  18. self,
  19. dora_event: dict,
  20. send_output: Callable[[str, bytes], None],
  21. ) -> DoraStatus:
  22. if dora_event["type"] == "INPUT":
  23. return self.on_input(dora_event, send_output)
  24. return DoraStatus.CONTINUE
  25. def on_input(
  26. self,
  27. dora_input: dict,
  28. send_output: Callable[[str, bytes], None],
  29. ) -> DoraStatus:
  30. """Handle image
  31. Args:
  32. dora_input (dict): Dict containing the "id", "data", and "metadata"
  33. send_output (Callable[[str, bytes]]): Function enabling sending output back to dora.
  34. """
  35. frame = np.frombuffer(dora_input["data"], dtype="uint8")
  36. frame = cv2.imdecode(frame, -1)
  37. frame = frame[:, :, ::-1] # OpenCV image (BGR to RGB)
  38. results = self.model(frame) # includes NMS
  39. arrays = np.array(results.xyxy[0].cpu()).tobytes()
  40. send_output("bbox", arrays, dora_input["metadata"])
  41. 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