From 6e034126bd5877346156d98b1ca68e7c9135491f Mon Sep 17 00:00:00 2001 From: haixuanTao Date: Thu, 11 Aug 2022 18:05:33 +0200 Subject: [PATCH] Add webcam and plot python example --- examples/python-dataflow/dataflow.yml | 20 +++++++++ examples/python-dataflow/plot_cv2.py | 49 +++++++++++++++++++++ examples/python-dataflow/requirements.txt | 1 + examples/python-dataflow/webcam_operator.py | 45 +++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 examples/python-dataflow/dataflow.yml create mode 100644 examples/python-dataflow/plot_cv2.py create mode 100644 examples/python-dataflow/requirements.txt create mode 100644 examples/python-dataflow/webcam_operator.py diff --git a/examples/python-dataflow/dataflow.yml b/examples/python-dataflow/dataflow.yml new file mode 100644 index 00000000..8561be2c --- /dev/null +++ b/examples/python-dataflow/dataflow.yml @@ -0,0 +1,20 @@ +communication: + zenoh: + prefix: /example-python-dataflow + +nodes: + - id: node-1 + operators: + - id: python_webcam + python: webcam_operator.py + inputs: + timer: dora/timer/millis/100 + outputs: + - image + + - id: node-2 + operators: + - id: python_plot + python: plot_cv2.py + inputs: + image: node-1/python_webcam/image diff --git a/examples/python-dataflow/plot_cv2.py b/examples/python-dataflow/plot_cv2.py new file mode 100644 index 00000000..50bc9e2b --- /dev/null +++ b/examples/python-dataflow/plot_cv2.py @@ -0,0 +1,49 @@ +from enum import Enum +from typing import Callable + +import cv2 +import numpy as np + + +class DoraStatus(Enum): + CONTINUE = 0 + STOP = 1 + + +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.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. + + 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. + """ + self.counter += 1 + if input_id == "image": + frame = np.frombuffer(value, dtype="uint8") + frame = np.reshape(frame, (480, 640, 3)) + cv2.imshow("frame", frame) + if cv2.waitKey(1) & 0xFF == ord("q"): + return DoraStatus.STOP + if self.counter > 20: + return DoraStatus.STOP + else: + return DoraStatus.CONTINUE + + def drop_operator(self): + cv2.destroyAllWindows() diff --git a/examples/python-dataflow/requirements.txt b/examples/python-dataflow/requirements.txt new file mode 100644 index 00000000..fb10efbf --- /dev/null +++ b/examples/python-dataflow/requirements.txt @@ -0,0 +1 @@ +pip install opencv-python \ No newline at end of file diff --git a/examples/python-dataflow/webcam_operator.py b/examples/python-dataflow/webcam_operator.py new file mode 100644 index 00000000..8eedc550 --- /dev/null +++ b/examples/python-dataflow/webcam_operator.py @@ -0,0 +1,45 @@ +from enum import Enum +from typing import Callable + +import cv2 + + +class DoraStatus(Enum): + CONTINUE = 0 + STOP = 1 + + +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) + + def on_input( + self, + input_id: str, + value: bytes, + send_output: Callable[[str, bytes], None], + ) -> DoraStatus: + """Handle input by incrementing count by one. + + 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. + """ + + ret, frame = self.video_capture.read() + if ret: + send_output("image", frame.tobytes()) + else: + print("did not sent video") + + return DoraStatus.CONTINUE + + def drop_operator(self): + self.video_capture.release()