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.

webcam.py 1.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import time
  4. from typing import Callable
  5. import cv2
  6. import pyarrow as pa
  7. from dora import DoraStatus
  8. CAMERA_WIDTH = 640
  9. CAMERA_HEIGHT = 480
  10. class Operator:
  11. """
  12. Sending image from webcam to the dataflow
  13. """
  14. def __init__(self):
  15. self.video_capture = cv2.VideoCapture(0)
  16. self.start_time = time.time()
  17. self.video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, CAMERA_WIDTH)
  18. self.video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, CAMERA_HEIGHT)
  19. def on_event(
  20. self,
  21. dora_event: str,
  22. send_output: Callable[[str, bytes], None],
  23. ) -> DoraStatus:
  24. match dora_event["type"]:
  25. case "INPUT":
  26. ret, frame = self.video_capture.read()
  27. frame = cv2.resize(frame, (CAMERA_WIDTH, CAMERA_HEIGHT))
  28. if ret:
  29. send_output(
  30. "image",
  31. pa.array(frame.flatten()),
  32. dora_event["metadata"],
  33. )
  34. case "STOP":
  35. print("received stop")
  36. case other:
  37. print("received unexpected event:", other)
  38. if time.time() - self.start_time < 20:
  39. return DoraStatus.CONTINUE
  40. else:
  41. return DoraStatus.STOP
  42. def __del__(self):
  43. self.video_capture.release()

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