Demuxing Synchronized Script Outputs
This example demonstrates the use of the DepthAI Sync node in conjunction with the Demux node to synchronize and then demux outputs from two separate script nodes. Each script node generates data buffers at different intervals, which are first synchronized by the Sync node and then demultiplexed by the MessageDemux node.Similar samples
Demo
Command Line
1~/depthai-python/examples/Sync $ python3 demux_message_group.py
2Start
3Buffer 1 timestamp: 0:00:03.581073
4Buffer 2 timestamp: 0:00:03.591084
5----------
6Buffer 1 timestamp: 0:00:04.583100
7Buffer 2 timestamp: 0:00:04.497079
8----------
9Buffer 1 timestamp: 0:00:06.587174
10Buffer 2 timestamp: 0:00:06.611154
11----------
12Buffer 1 timestamp: 0:00:07.589147
13Buffer 2 timestamp: 0:00:07.517125
14----------
15Buffer 1 timestamp: 0:00:09.593076
16Buffer 2 timestamp: 0:00:09.631089
17----------
18Buffer 1 timestamp: 0:00:10.595106
19Buffer 2 timestamp: 0:00:10.537082
Setup
Please run the install script to download all required dependencies. Please note that this script must be ran from git context, so you have to download the depthai-python repository first and then run the scriptCommand Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
Source code
Python
C++
Python
PythonGitHub
1import depthai as dai
2import time
3from datetime import timedelta
4
5pipeline = dai.Pipeline()
6
7script1 = pipeline.create(dai.node.Script)
8script1.setScript("""
9from time import sleep
10
11while True:
12 sleep(1)
13 b = Buffer(512)
14 b.setData(bytes(4 * [i for i in range(0, 128)]))
15 b.setTimestamp(Clock.now())
16 node.io['out'].send(b)
17""")
18
19script2 = pipeline.create(dai.node.Script)
20script2.setScript("""
21from time import sleep
22
23while True:
24 sleep(0.3)
25 b = Buffer(512)
26 b.setData(bytes(4 * [i for i in range(128, 256)]))
27 b.setTimestamp(Clock.now())
28 node.io['out'].send(b)
29""")
30
31sync = pipeline.create(dai.node.Sync)
32sync.setSyncThreshold(timedelta(milliseconds=100))
33
34demux = pipeline.create(dai.node.MessageDemux)
35
36xout1 = pipeline.create(dai.node.XLinkOut)
37xout1.setStreamName("xout1")
38xout2 = pipeline.create(dai.node.XLinkOut)
39xout2.setStreamName("xout2")
40
41script1.outputs["out"].link(sync.inputs["s1"])
42script2.outputs["out"].link(sync.inputs["s2"])
43sync.out.link(demux.input)
44demux.outputs["s1"].link(xout1.input)
45demux.outputs["s2"].link(xout2.input)
46
47with dai.Device(pipeline) as device:
48 print("Start")
49 q1 = device.getOutputQueue("xout1", maxSize=10, blocking=True)
50 q2 = device.getOutputQueue("xout2", maxSize=10, blocking=True)
51 while True:
52 bufS1 = q1.get()
53 bufS2 = q2.get()
54 print(f"Buffer 1 timestamp: {bufS1.getTimestamp()}")
55 print(f"Buffer 2 timestamp: {bufS2.getTimestamp()}")
56 print("----------")
57 time.sleep(0.2)
How it Works
- Initialize a DepthAI pipeline.
- Create two Script nodes, with each script generating and sending data buffers at different intervals.
- Set up a Sync node with a synchronization threshold.
- Integrate a MessageDemux node to separate the synchronized data streams.
- Link the outputs of the Script nodes to the Sync node, and then from the Sync node to the MessageDemux node.
- Start the pipeline and continuously receive demultiplexed data from the MessageDemux node.
- Print the timestamps of the demultiplexed data for comparison.
Pipeline
Need assistance?
Head over to Discussion Forum for technical support or any other questions you might have.