Skip to content

Commit

Permalink
Upate webcam operator example to make use of opentelemetry jaeger
Browse files Browse the repository at this point in the history
  • Loading branch information
haixuanTao committed Mar 30, 2023
1 parent 350c4d5 commit 75e7f2c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ on:
paths:
- apis/python/**
- binaries/runtime/**
- examples/python-dataflow/**
- examples/python-operator-dataflow/**
pull_request:
workflow_dispatch:

Expand Down
7 changes: 6 additions & 1 deletion examples/python-dataflow/webcam.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time

import cv2

from dora import Node

node = Node()
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions examples/python-operator-dataflow/dataflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ communication:

nodes:
- id: webcam
custom:
source: webcam.py
operator:
python: webcam.py
inputs:
tick: dora/timer/millis/100
outputs:
Expand Down
2 changes: 1 addition & 1 deletion examples/python-operator-dataflow/object_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import torch

from dora import DoraStatus


class Operator:
"""
Expand Down
67 changes: 41 additions & 26 deletions examples/python-operator-dataflow/webcam.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 75e7f2c

Please sign in to comment.