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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. def on_input(
  25. self,
  26. dora_input: dict,
  27. send_output: Callable[[str, bytes], None],
  28. ) -> DoraStatus:
  29. """Handle image
  30. Args:
  31. dora_input (dict): Dict containing the "id", "data", and "metadata"
  32. send_output (Callable[[str, bytes]]): Function enabling sending output back to dora.
  33. """
  34. frame = np.frombuffer(dora_input["data"], dtype="uint8")
  35. frame = cv2.imdecode(frame, -1)
  36. frame = frame[:, :, ::-1] # OpenCV image (BGR to RGB)
  37. results = self.model(frame) # includes NMS
  38. arrays = np.array(results.xyxy[0].cpu()).tobytes()
  39. send_output("bbox", arrays, dora_input["metadata"])
  40. 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