From 6a1069a0b29b213c412b605f16f7bfffb1c152bd Mon Sep 17 00:00:00 2001 From: Erol444 Date: Wed, 31 Aug 2022 18:30:23 +0200 Subject: [PATCH 1/6] Added example of script node changing pipeline flow. Crashes atm --- .../Script/script_change_pipeline_flow.py | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 examples/Script/script_change_pipeline_flow.py diff --git a/examples/Script/script_change_pipeline_flow.py b/examples/Script/script_change_pipeline_flow.py new file mode 100644 index 000000000..6d1544b0a --- /dev/null +++ b/examples/Script/script_change_pipeline_flow.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python3 +import depthai as dai +import cv2 +from pathlib import Path +import numpy as np + +parentDir = Path(__file__).parent +nnPath = str((parentDir / Path('../models/mobilenet-ssd_openvino_2021.4_5shave.blob')).resolve().absolute()) + +pipeline = dai.Pipeline() + +cam = pipeline.createColorCamera() +cam.setBoardSocket(dai.CameraBoardSocket.RGB) +cam.setIspScale(2,3) +cam.setVideoSize(720,720) +cam.setPreviewSize(300,300) + +xoutRgb = pipeline.create(dai.node.XLinkOut) +xoutRgb.setStreamName('rgb') +cam.video.link(xoutRgb.input) + +script = pipeline.createScript() + +xin = pipeline.create(dai.node.XLinkIn) +xin.setStreamName('in') +xin.out.link(script.inputs['toggle']) + +cam.preview.link(script.inputs['rgb']) +script.setScript(""" +toggle = True +while True: + msg = node.io['toggle'].tryGet() + if msg is not None: + toggle = not toggle + node.warn('Toggle! Perform NN inferencing: ' + str(toggle)) + + frame = node.io['rgb'].get() + + if toggle: + node.io['nn'].send(frame) +""") + +nn = pipeline.create(dai.node.MobileNetDetectionNetwork) +nn.setBlobPath(nnPath) +script.outputs['nn'].link(nn.input) + +xoutNn = pipeline.create(dai.node.XLinkOut) +xoutNn.setStreamName('nn') +nn.out.link(xoutNn.input) + +# Connect to device with pipeline +with dai.Device(pipeline) as device: + inQ = device.getInputQueue("in") + qRgb = device.getOutputQueue("rgb") + qNn = device.getOutputQueue("nn") + + def frameNorm(frame, bbox): + normVals = np.full(len(bbox), frame.shape[0]) + normVals[::2] = frame.shape[1] + return (np.clip(np.array(bbox), 0, 1) * normVals).astype(int) + + def drawDetections(frame, detections): + color = (255, 0, 0) + for detection in detections: + bbox = frameNorm(frame, (detection.xmin, detection.ymin, detection.xmax, detection.ymax)) + cv2.putText(frame, f"{int(detection.confidence * 100)}%", (bbox[0] + 10, bbox[1] + 40), cv2.FONT_HERSHEY_TRIPLEX, 0.5, color) + cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color, 2) + + def send_trigger(): + inQ.send(dai.Buffer()) + + while True: + frame = qRgb.get().getCvFrame() + + if qNn.has(): + detections = qNn.get().detections + drawDetections(frame, detections) + + cv2.imshow('Color frame', frame) + + key = cv2.waitKey(1) + if key == ord('q'): + break + elif key == ord('t'): + print('send trigger to IR filter') + send_trigger() \ No newline at end of file From 5fda1f74b5d537e02679dab6306997b1826d334d Mon Sep 17 00:00:00 2001 From: Erol444 Date: Wed, 31 Aug 2022 18:50:10 +0200 Subject: [PATCH 2/6] Fixed example code. TODO: docs --- .../Script/script_change_pipeline_flow.py | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/examples/Script/script_change_pipeline_flow.py b/examples/Script/script_change_pipeline_flow.py index 6d1544b0a..b7fa900d8 100644 --- a/examples/Script/script_change_pipeline_flow.py +++ b/examples/Script/script_change_pipeline_flow.py @@ -11,6 +11,7 @@ cam = pipeline.createColorCamera() cam.setBoardSocket(dai.CameraBoardSocket.RGB) +cam.setInterleaved(False) cam.setIspScale(2,3) cam.setVideoSize(720,720) cam.setPreviewSize(300,300) @@ -27,11 +28,11 @@ cam.preview.link(script.inputs['rgb']) script.setScript(""" -toggle = True +toggle = False while True: msg = node.io['toggle'].tryGet() if msg is not None: - toggle = not toggle + toggle = msg.getData()[0] node.warn('Toggle! Perform NN inferencing: ' + str(toggle)) frame = node.io['rgb'].get() @@ -54,20 +55,20 @@ qRgb = device.getOutputQueue("rgb") qNn = device.getOutputQueue("nn") + runNn = False + def frameNorm(frame, bbox): normVals = np.full(len(bbox), frame.shape[0]) normVals[::2] = frame.shape[1] return (np.clip(np.array(bbox), 0, 1) * normVals).astype(int) + color = (255, 127, 0) def drawDetections(frame, detections): - color = (255, 0, 0) for detection in detections: bbox = frameNorm(frame, (detection.xmin, detection.ymin, detection.xmax, detection.ymax)) - cv2.putText(frame, f"{int(detection.confidence * 100)}%", (bbox[0] + 10, bbox[1] + 40), cv2.FONT_HERSHEY_TRIPLEX, 0.5, color) + cv2.putText(frame, f"{int(detection.confidence * 100)}%", (bbox[0] + 10, bbox[1] + 20), cv2.FONT_HERSHEY_TRIPLEX, 0.5, color) cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color, 2) - def send_trigger(): - inQ.send(dai.Buffer()) while True: frame = qRgb.get().getCvFrame() @@ -76,11 +77,15 @@ def send_trigger(): detections = qNn.get().detections drawDetections(frame, detections) + cv2.putText(frame, f"NN inferencing: {runNn}", (20,20), cv2.FONT_HERSHEY_TRIPLEX, 0.7, color) cv2.imshow('Color frame', frame) key = cv2.waitKey(1) if key == ord('q'): break elif key == ord('t'): - print('send trigger to IR filter') - send_trigger() \ No newline at end of file + runNn = not runNn + print(f"{'Enabling' if runNn else 'Disabling'} NN inferencing") + buf = dai.Buffer() + buf.setData(runNn) + inQ.send(buf) \ No newline at end of file From 041aa055bd6e79034defa92b6ea7d9d078e30940 Mon Sep 17 00:00:00 2001 From: Erol444 Date: Wed, 31 Aug 2022 18:58:57 +0200 Subject: [PATCH 3/6] Added "Script change pipeline flow" docs --- .../Script/script_change_pipeline_flow.rst | 43 +++++++++++++++++++ docs/source/tutorials/code_samples.rst | 1 + 2 files changed, 44 insertions(+) create mode 100644 docs/source/samples/Script/script_change_pipeline_flow.rst diff --git a/docs/source/samples/Script/script_change_pipeline_flow.rst b/docs/source/samples/Script/script_change_pipeline_flow.rst new file mode 100644 index 000000000..4667928bd --- /dev/null +++ b/docs/source/samples/Script/script_change_pipeline_flow.rst @@ -0,0 +1,43 @@ +Script change pipeline flow +=========================== + +This example shows how you can change the flow of data inside your pipeline in runtime using the :ref:`Script` node. In this example, we send a message from +the host to choose whether we want Script node to forwards color frame to the :ref:`MobileNetDetectionNetwork`. + +Demo +#### + +.. image:: https://user-images.githubusercontent.com/18037362/187734814-df3b46c9-5e04-4a9d-bf6f-d738b40b4421.gif + +Pipeline Graph +############## + +.. image:: https://user-images.githubusercontent.com/18037362/187736249-db7ff175-fcea-4d4e-b567-f99087bd82ee.png + +Setup +##### + +.. include:: /includes/install_from_pypi.rst + +Source code +########### + +.. tabs:: + + .. tab:: Python + + Also `available on GitHub `__ + + .. literalinclude:: ../../../../examples/Script/script_change_pipeline_flow.py + :language: python + :linenos: + + .. tab:: C++ + + Also `available on GitHub `__ + + .. literalinclude:: ../../../../depthai-core/examples/Script/script_change_pipeline_flow.cpp + :language: cpp + :linenos: + +.. include:: /includes/footer-short.rst diff --git a/docs/source/tutorials/code_samples.rst b/docs/source/tutorials/code_samples.rst index afc506e48..868061568 100644 --- a/docs/source/tutorials/code_samples.rst +++ b/docs/source/tutorials/code_samples.rst @@ -113,6 +113,7 @@ are presented with code. .. rubric:: Script - :ref:`Script camera control` - Controlling the camera with the Script node +- :ref:`Script change pipeline flow` - Change the flow of data inside your pipeline in runtime with :ref:`Script`` node - :ref:`Script forward frames` - Forward incoming image stream to two different output streams (demuxing) - :ref:`Script get local IP` - Get local IP of the device (only OAK-POE devices) - :ref:`Script HTTP client` - Send HTTP request to a server (only OAK-POE devices) From e83fd3a1364f9234f8aa8f5aba0e32dff85e173d Mon Sep 17 00:00:00 2001 From: Erol444 Date: Wed, 31 Aug 2022 18:59:47 +0200 Subject: [PATCH 4/6] Felt free to indent the script node script --- examples/Script/script_change_pipeline_flow.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/Script/script_change_pipeline_flow.py b/examples/Script/script_change_pipeline_flow.py index b7fa900d8..3774faf50 100644 --- a/examples/Script/script_change_pipeline_flow.py +++ b/examples/Script/script_change_pipeline_flow.py @@ -28,17 +28,17 @@ cam.preview.link(script.inputs['rgb']) script.setScript(""" -toggle = False -while True: - msg = node.io['toggle'].tryGet() - if msg is not None: - toggle = msg.getData()[0] - node.warn('Toggle! Perform NN inferencing: ' + str(toggle)) + toggle = False + while True: + msg = node.io['toggle'].tryGet() + if msg is not None: + toggle = msg.getData()[0] + node.warn('Toggle! Perform NN inferencing: ' + str(toggle)) - frame = node.io['rgb'].get() + frame = node.io['rgb'].get() - if toggle: - node.io['nn'].send(frame) + if toggle: + node.io['nn'].send(frame) """) nn = pipeline.create(dai.node.MobileNetDetectionNetwork) From 70ea9600c97cb88392bafbd83f1e914e85fc5cf9 Mon Sep 17 00:00:00 2001 From: Erol444 Date: Thu, 1 Sep 2022 00:25:00 +0200 Subject: [PATCH 5/6] Updated core --- depthai-core | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/depthai-core b/depthai-core index 41de567ea..a5bd36cff 160000 --- a/depthai-core +++ b/depthai-core @@ -1 +1 @@ -Subproject commit 41de567ea52443368224145b9e54a4f858abaeb8 +Subproject commit a5bd36cffd733938b5cdd6df744ebcb7b0762788 From 63f71037254b9596215c0deeb0abd53c7d96e7ad Mon Sep 17 00:00:00 2001 From: Erol444 Date: Thu, 1 Sep 2022 14:10:34 +0200 Subject: [PATCH 6/6] Fixed docs build error --- docs/source/tutorials/code_samples.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/tutorials/code_samples.rst b/docs/source/tutorials/code_samples.rst index 868061568..e817ae708 100644 --- a/docs/source/tutorials/code_samples.rst +++ b/docs/source/tutorials/code_samples.rst @@ -113,7 +113,7 @@ are presented with code. .. rubric:: Script - :ref:`Script camera control` - Controlling the camera with the Script node -- :ref:`Script change pipeline flow` - Change the flow of data inside your pipeline in runtime with :ref:`Script`` node +- :ref:`Script change pipeline flow` - Change the flow of data inside your pipeline in runtime with :ref:`Script` node - :ref:`Script forward frames` - Forward incoming image stream to two different output streams (demuxing) - :ref:`Script get local IP` - Get local IP of the device (only OAK-POE devices) - :ref:`Script HTTP client` - Send HTTP request to a server (only OAK-POE devices)