DepthAI Tutorials
DepthAI API References

ON THIS PAGE

  • Multiple Scripts Sync
  • Similar samples:
  • Demo
  • Setup
  • Source code
  • How it Works

Multiple Scripts Sync

This example illustrates the use of the DepthAI Sync node to synchronize outputs from two separate script nodes. Each script generates and sends data buffers at different intervals, and the Sync node aligns these outputs based on their timestamps.

Similar samples:

Demo

Command Line
1~/depthai-python/examples/Sync $ python3 sync_scripts.py
2Start
3Received s1 with timestamp 0:00:02.420089
4Received s2 with timestamp 0:00:02.461076
5Time interval between messages: 40.987ms
6----------
7Received s1 with timestamp 0:00:03.422108
8Received s2 with timestamp 0:00:03.367069
9Time interval between messages: 55.039ms
10----------
11Received s1 with timestamp 0:00:05.426088
12Received s2 with timestamp 0:00:05.481086
13Time interval between messages: 54.998ms
14----------
15Received s1 with timestamp 0:00:06.428106
16Received s2 with timestamp 0:00:06.387129
17Time interval between messages: 40.977ms
18----------

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 script
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
For additional information, please follow the installation guide.

Source code

Python
C++
Python
GitHub
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
34xout = pipeline.create(dai.node.XLinkOut)
35xout.setStreamName("xout")
36
37sync.out.link(xout.input)
38
39script1.outputs["out"].link(sync.inputs["s1"])
40script2.outputs["out"].link(sync.inputs["s2"])
41
42# script1.outputs["out"].link(xout.input)
43
44with dai.Device(pipeline) as device:
45    print("Start")
46    q = device.getOutputQueue("xout", maxSize=10, blocking=True)
47    while True:
48        grp = q.get()
49        for name, msg in grp:
50            print(f"Received {name} with timestamp {msg.getTimestamp()}")
51        print(f"Time interval between messages: {grp.getIntervalNs() / 1e6}ms")
52        print("----------")
53        time.sleep(0.2)

How it Works

  • Initialize a DepthAI pipeline.
  • Create two Script nodes, each generating and sending data buffers at different intervals.
  • Set up a Sync node with a synchronization threshold.
  • Link the outputs of the Script nodes to the Sync node.
  • Start the pipeline and continuously receive synchronized data from the Script nodes.
  • Print the received data along with timestamps and the interval between messages.

Need assistance?

Head over to Discussion Forum for technical support or any other questions you might have.