|
|
|
@@ -1,50 +1,23 @@ |
|
|
|
from enum import Enum |
|
|
|
from typing import Callable |
|
|
|
|
|
|
|
import cv2 |
|
|
|
|
|
|
|
|
|
|
|
class DoraStatus(Enum): |
|
|
|
CONTINUE = 0 |
|
|
|
STOP = 1 |
|
|
|
#!/usr/bin/env python |
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
|
import time |
|
|
|
|
|
|
|
class Operator: |
|
|
|
""" |
|
|
|
Example operator incrementing a counter every times its been called. |
|
|
|
|
|
|
|
The current value of the counter is sent back to dora on `counter`. |
|
|
|
""" |
|
|
|
|
|
|
|
def __init__(self): |
|
|
|
self.video_capture = cv2.VideoCapture(0) |
|
|
|
self.counter = 0 |
|
|
|
|
|
|
|
def on_input( |
|
|
|
self, |
|
|
|
input_id: str, |
|
|
|
value: bytes, |
|
|
|
send_output: Callable[[str, bytes], None], |
|
|
|
) -> DoraStatus: |
|
|
|
"""Handle input by incrementing count by one. |
|
|
|
import cv2 |
|
|
|
from dora import Node |
|
|
|
|
|
|
|
Args: |
|
|
|
input_id (str): Id of the input declared in the yaml configuration |
|
|
|
value (bytes): Bytes message of the input |
|
|
|
send_output (Callable[[str, bytes]]): Function enabling sending output back to dora. |
|
|
|
""" |
|
|
|
node = Node() |
|
|
|
|
|
|
|
ret, frame = self.video_capture.read() |
|
|
|
if ret: |
|
|
|
send_output("image", frame.tobytes()) |
|
|
|
else: |
|
|
|
print("did not sent video") |
|
|
|
video_capture = cv2.VideoCapture(0) |
|
|
|
|
|
|
|
self.counter += 1 |
|
|
|
if self.counter > 100: |
|
|
|
return DoraStatus.STOP |
|
|
|
start = time.time() |
|
|
|
|
|
|
|
return DoraStatus.CONTINUE |
|
|
|
# Run for 20 seconds |
|
|
|
while time.time() - start < 20: |
|
|
|
# Wait next input |
|
|
|
node.next() |
|
|
|
ret, frame = video_capture.read() |
|
|
|
if ret: |
|
|
|
node.send_output("image", cv2.imencode(".jpg", frame)[1].tobytes()) |
|
|
|
|
|
|
|
def drop_operator(self): |
|
|
|
self.video_capture.release() |
|
|
|
video_capture.release() |