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

~/depthai-python/examples/Sync $ python3 sync_scripts.py
Start
Received s1 with timestamp 0:00:02.420089
Received s2 with timestamp 0:00:02.461076
Time interval between messages: 40.987ms
----------
Received s1 with timestamp 0:00:03.422108
Received s2 with timestamp 0:00:03.367069
Time interval between messages: 55.039ms
----------
Received s1 with timestamp 0:00:05.426088
Received s2 with timestamp 0:00:05.481086
Time interval between messages: 54.998ms
----------
Received s1 with timestamp 0:00:06.428106
Received s2 with timestamp 0:00:06.387129
Time interval between messages: 40.977ms
----------

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

git clone https://github.com/luxonis/depthai-python.git
cd depthai-python/examples
python3 install_requirements.py

For additional information, please follow installation guide

Source code

Also available on GitHub

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import depthai as dai
import time
from datetime import timedelta

pipeline = dai.Pipeline()

script1 = pipeline.create(dai.node.Script)
script1.setScript("""
from time import sleep

while True:
    sleep(1)
    b = Buffer(512)
    b.setData(bytes(4 * [i for i in range(0, 128)]))
    b.setTimestamp(Clock.now())
    node.io['out'].send(b)
""")

script2 = pipeline.create(dai.node.Script)
script2.setScript("""
from time import sleep

while True:
    sleep(0.3)
    b = Buffer(512)
    b.setData(bytes(4 * [i for i in range(128, 256)]))
    b.setTimestamp(Clock.now())
    node.io['out'].send(b)
""")

sync = pipeline.create(dai.node.Sync)
sync.setSyncThreshold(timedelta(milliseconds=100))

xout = pipeline.create(dai.node.XLinkOut)
xout.setStreamName("xout")

sync.out.link(xout.input)

script1.outputs["out"].link(sync.inputs["s1"])
script2.outputs["out"].link(sync.inputs["s2"])

# script1.outputs["out"].link(xout.input)

with dai.Device(pipeline) as device:
    print("Start")
    q = device.getOutputQueue("xout", maxSize=10, blocking=True)
    while True:
        grp = q.get()
        for name, msg in grp:
            print(f"Received {name} with timestamp {msg.getTimestamp()}")
        print(f"Time interval between messages: {grp.getIntervalNs() / 1e6}ms")
        print("----------")
        time.sleep(0.2)

Also available on GitHub

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <chrono>
#include <iostream>

#include "depthai/depthai.hpp"

int main() {
    dai::Pipeline pipeline;

    auto script1 = pipeline.create<dai::node::Script>();
    script1->setScript(
        R"SCRPT(
from time import sleep

while True:
    sleep(1)
    b = Buffer(512)
    b.setData(bytes(4 * [i for i in range(0, 128)]))
    b.setTimestamp(Clock.now())
    node.io['out'].send(b)
)SCRPT");

    auto script2 = pipeline.create<dai::node::Script>();
    script2->setScript(
        R"SCRPT(
from time import sleep

while True:
    sleep(0.3)
    b = Buffer(512)
    b.setData(bytes(4 * [i for i in range(128, 256)]))
    b.setTimestamp(Clock.now())
    node.io['out'].send(b)
)SCRPT");

    auto sync = pipeline.create<dai::node::Sync>();
    sync->setSyncThreshold(std::chrono::milliseconds(100));

    auto xout = pipeline.create<dai::node::XLinkOut>();
    xout->setStreamName("xout");

    sync->out.link(xout->input);
    script1->outputs["out"].link(sync->inputs["s1"]);
    script2->outputs["out"].link(sync->inputs["s2"]);

    dai::Device device(pipeline);
    std::cout << "Start" << std::endl;
    auto queue = device.getOutputQueue("xout", 10, true);
    while(true) {
        auto grp = queue->get<dai::MessageGroup>();
        std::cout << "Buffer 1 timestamp: " << grp->get<dai::Buffer>("s1")->getTimestamp().time_since_epoch().count() << std::endl;
        std::cout << "Buffer 2 timestamp: " << grp->get<dai::Buffer>("s2")->getTimestamp().time_since_epoch().count() << std::endl;
        std::cout << "Time interval between messages: " << static_cast<double>(grp->getIntervalNs()) / 1e6 << "ms" << std::endl;
        std::cout << "----------" << std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(200));
    }
}

How it Works

  1. Initialize a DepthAI pipeline.

  2. Create two Script nodes, each generating and sending data buffers at different intervals.

  3. Set up a Sync node with a synchronization threshold.

  4. Link the outputs of the Script nodes to the Sync node.

  5. Start the pipeline and continuously receive synchronized data from the Script nodes.

  6. Print the received data along with timestamps and the interval between messages.

Got questions?

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