此页面由 AI 自动翻译。查看英文原版

本页目录

  • 演示
  • 源代码
  • 工作原理

多脚本同步

此示例说明了如何使用 DepthAI Sync 节点来同步两个独立脚本节点的输出。每个脚本以不同的间隔生成和发送数据缓冲区,Sync 节点根据其时间戳对这些输出进行对齐。

类似示例:

演示

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----------
此示例需要 DepthAI v3 API,请参阅 安装说明

源代码

Python

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)

C++

1#include <chrono>
2#include <iostream>
3
4#include "depthai/depthai.hpp"
5
6int main() {
7    dai::Pipeline pipeline;
8
9    auto script1 = pipeline.create<dai::node::Script>();
10    script1->setScript(
11        R"SCRPT(
12from time import sleep
13
14while True:
15    sleep(1)
16    b = Buffer(512)
17    b.setData(bytes(4 * [i for i in range(0, 128)]))
18    b.setTimestamp(Clock.now())
19    node.io['out'].send(b)
20)SCRPT");
21
22    auto script2 = pipeline.create<dai::node::Script>();
23    script2->setScript(
24        R"SCRPT(
25from time import sleep
26
27while True:
28    sleep(0.3)
29    b = Buffer(512)
30    b.setData(bytes(4 * [i for i in range(128, 256)]))
31    b.setTimestamp(Clock.now())
32    node.io['out'].send(b)
33)SCRPT");
34
35    auto sync = pipeline.create<dai::node::Sync>();
36    sync->setSyncThreshold(std::chrono::milliseconds(100));
37
38    auto xout = pipeline.create<dai::node::XLinkOut>();
39    xout->setStreamName("xout");
40
41    sync->out.link(xout->input);
42    script1->outputs["out"].link(sync->inputs["s1"]);
43    script2->outputs["out"].link(sync->inputs["s2"]);
44
45    dai::Device device(pipeline);
46    std::cout << "Start" << std::endl;
47    auto queue = device.getOutputQueue("xout", 10, true);
48    while(true) {
49        auto grp = queue->get<dai::MessageGroup>();
50        std::cout << "Buffer 1 timestamp: " << grp->get<dai::Buffer>("s1")->getTimestamp().time_since_epoch().count() << std::endl;
51        std::cout << "Buffer 2 timestamp: " << grp->get<dai::Buffer>("s2")->getTimestamp().time_since_epoch().count() << std::endl;
52        std::cout << "Time interval between messages: " << static_cast<double>(grp->getIntervalNs()) / 1e6 << "ms" << std::endl;
53        std::cout << "----------" << std::endl;
54        std::this_thread::sleep_for(std::chrono::milliseconds(200));
55    }
56}

工作原理

  • 初始化一个 DepthAI 管道。
  • 创建两个 Script 节点,每个节点以不同的间隔生成和发送数据缓冲区。
  • 设置一个具有同步阈值的 Sync 节点。
  • 将 Script 节点的输出链接到 Sync 节点。
  • 启动管道并持续从 Script 节点接收同步数据。
  • 打印接收到的数据以及时间戳和消息之间的时间间隔。

需要帮助?

请前往 Discussion Forum 获取技术支持或提出您可能有的任何其他问题。