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

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