From 75e7f2c1a84a60db1fba8f6680f1b9b59009020d Mon Sep 17 00:00:00 2001 From: haixuanTao Date: Wed, 29 Mar 2023 19:46:27 +0800 Subject: [PATCH] Upate webcam operator example to make use of opentelemetry jaeger --- .github/workflows/ci-python.yml | 2 + examples/python-dataflow/webcam.py | 7 +- .../python-operator-dataflow/dataflow.yml | 4 +- .../object_detection.py | 2 +- examples/python-operator-dataflow/webcam.py | 67 ++++++++++++------- 5 files changed, 52 insertions(+), 30 deletions(-) diff --git a/.github/workflows/ci-python.yml b/.github/workflows/ci-python.yml index f49116c7..cbdb6f37 100644 --- a/.github/workflows/ci-python.yml +++ b/.github/workflows/ci-python.yml @@ -6,6 +6,8 @@ on: paths: - apis/python/** - binaries/runtime/** + - examples/python-dataflow/** + - examples/python-operator-dataflow/** pull_request: workflow_dispatch: diff --git a/examples/python-dataflow/webcam.py b/examples/python-dataflow/webcam.py index cbcaedfc..c50a28e4 100755 --- a/examples/python-dataflow/webcam.py +++ b/examples/python-dataflow/webcam.py @@ -4,6 +4,7 @@ import time import cv2 + from dora import Node node = Node() @@ -20,7 +21,11 @@ case "INPUT": ret, frame = video_capture.read() if ret: - node.send_output("image", cv2.imencode(".jpg", frame)[1].tobytes()) + node.send_output( + "image", + cv2.imencode(".jpg", frame)[1].tobytes(), + event["metadata"], + ) case "STOP": print("received stop") break diff --git a/examples/python-operator-dataflow/dataflow.yml b/examples/python-operator-dataflow/dataflow.yml index 0690fa9e..d43a1523 100644 --- a/examples/python-operator-dataflow/dataflow.yml +++ b/examples/python-operator-dataflow/dataflow.yml @@ -4,8 +4,8 @@ communication: nodes: - id: webcam - custom: - source: webcam.py + operator: + python: webcam.py inputs: tick: dora/timer/millis/100 outputs: diff --git a/examples/python-operator-dataflow/object_detection.py b/examples/python-operator-dataflow/object_detection.py index 762422e1..d06974cc 100755 --- a/examples/python-operator-dataflow/object_detection.py +++ b/examples/python-operator-dataflow/object_detection.py @@ -8,7 +8,7 @@ import torch from dora import DoraStatus - + class Operator: """ diff --git a/examples/python-operator-dataflow/webcam.py b/examples/python-operator-dataflow/webcam.py index cbcaedfc..e2c73ab4 100755 --- a/examples/python-operator-dataflow/webcam.py +++ b/examples/python-operator-dataflow/webcam.py @@ -1,31 +1,46 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- import time +from typing import Callable import cv2 -from dora import Node - -node = Node() - -video_capture = cv2.VideoCapture(0) - -start = time.time() - -# Run for 20 seconds -while time.time() - start < 10: - # Wait next dora_input - event = node.next() - match event["type"]: - case "INPUT": - ret, frame = video_capture.read() - if ret: - node.send_output("image", cv2.imencode(".jpg", frame)[1].tobytes()) - case "STOP": - print("received stop") - break - case other: - print("received unexpected event:", other) - break - -video_capture.release() + +from dora import DoraStatus + + +class Operator: + """ + Sending image from webcam to the dataflow + """ + + def __init__(self): + self.video_capture = cv2.VideoCapture(0) + self.start_time = time.time() + + def on_event( + self, + dora_event: dict, + send_output: Callable[[str, bytes], None], + ) -> DoraStatus: + match dora_event["type"]: + case "INPUT": + ret, frame = self.video_capture.read() + if ret: + send_output( + "image", + cv2.imencode(".jpg", frame)[1].tobytes(), + dora_event["metadata"], + ) + case "STOP": + print("received stop") + case other: + print("received unexpected event:", other) + + if time.time() - self.start_time < 20: + return DoraStatus.CONTINUE + else: + return DoraStatus.STOP + + def __del__(self): + self.video_capture.release()