From 89aa70c9a6e333e4b2a115929fda4098bbc002c8 Mon Sep 17 00:00:00 2001 From: haixuanTao Date: Fri, 12 Aug 2022 16:56:26 +0200 Subject: [PATCH] make python example usable without webcam --- examples/python-dataflow/no_webcam.py | 23 ++++++++ examples/python-dataflow/object_detection.py | 6 +- examples/python-dataflow/plot.py | 3 +- examples/python-dataflow/webcam.py | 59 ++++++-------------- 4 files changed, 44 insertions(+), 47 deletions(-) create mode 100755 examples/python-dataflow/no_webcam.py mode change 100644 => 100755 examples/python-dataflow/webcam.py diff --git a/examples/python-dataflow/no_webcam.py b/examples/python-dataflow/no_webcam.py new file mode 100755 index 00000000..d4b47c74 --- /dev/null +++ b/examples/python-dataflow/no_webcam.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import time +import urllib.request + +import cv2 +import numpy as np +from dora import Node + +req = urllib.request.urlopen( + "https://pyimagesearch.com/wp-content/uploads/2015/01/opencv_logo.png" +) + +arr = np.asarray(bytearray(req.read()), dtype=np.uint8) +node = Node() + +start = time.time() + +while time.time() - start < 20: + # Wait next input + node.next() + node.send_output("image", arr.tobytes()) diff --git a/examples/python-dataflow/object_detection.py b/examples/python-dataflow/object_detection.py index 5737fcfc..1e7ea431 100644 --- a/examples/python-dataflow/object_detection.py +++ b/examples/python-dataflow/object_detection.py @@ -1,6 +1,7 @@ from enum import Enum from typing import Callable +import cv2 import numpy as np import torch @@ -35,9 +36,8 @@ class Operator: """ frame = np.frombuffer(value, dtype="uint8") - frame = np.reshape(frame, (480, 640, 3))[ - :, :, ::-1 - ] # OpenCV image (BGR to RGB) + frame = cv2.imdecode(frame, -1) + frame = frame[:, :, ::-1] # OpenCV image (BGR to RGB) results = self.model(frame) # includes NMS arrays = np.array(results.xyxy[0].cpu()).tobytes() diff --git a/examples/python-dataflow/plot.py b/examples/python-dataflow/plot.py index 2ce8be30..1f64c0ef 100644 --- a/examples/python-dataflow/plot.py +++ b/examples/python-dataflow/plot.py @@ -35,8 +35,9 @@ class Operator: """ if input_id == "image": frame = np.frombuffer(value, dtype="uint8") - frame = np.reshape(frame, (480, 640, 3)) + frame = cv2.imdecode(frame, -1) self.image = frame + elif input_id == "bbox" and len(self.image) != 0: bboxs = np.frombuffer(value, dtype="float32") bboxs = np.reshape(bboxs, (-1, 6)) diff --git a/examples/python-dataflow/webcam.py b/examples/python-dataflow/webcam.py old mode 100644 new mode 100755 index ab704c13..fc913236 --- a/examples/python-dataflow/webcam.py +++ b/examples/python-dataflow/webcam.py @@ -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()