Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion depthai-core
43 changes: 43 additions & 0 deletions docs/source/samples/Script/script_change_pipeline_flow.rst
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/luxonis/depthai-python/blob/main/examples/Script/script_change_pipeline_flow.py>`__

.. literalinclude:: ../../../../examples/Script/script_change_pipeline_flow.py
:language: python
:linenos:

.. tab:: C++

Also `available on GitHub <https://github.com/luxonis/depthai-core/blob/main/examples/Script/script_change_pipeline_flow.cpp>`__

.. literalinclude:: ../../../../depthai-core/examples/Script/script_change_pipeline_flow.cpp
:language: cpp
:linenos:

.. include:: /includes/footer-short.rst
1 change: 1 addition & 0 deletions docs/source/tutorials/code_samples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
91 changes: 91 additions & 0 deletions examples/Script/script_change_pipeline_flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/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.setInterleaved(False)
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 = 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()

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")

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):
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] + 20), cv2.FONT_HERSHEY_TRIPLEX, 0.5, color)
cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color, 2)


while True:
frame = qRgb.get().getCvFrame()

if qNn.has():
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'):
runNn = not runNn
print(f"{'Enabling' if runNn else 'Disabling'} NN inferencing")
buf = dai.Buffer()
buf.setData(runNn)
inQ.send(buf)