DepthAI v2 has been superseded by DepthAI v3. You are viewing legacy documentation.
此页面由 AI 自动翻译。查看英文原版
DepthAI 教程
DepthAI API 参考

本页目录

  • 演示
  • 设置
  • 源代码
  • 工作原理
  • 管道

解复用同步脚本输出

本示例演示了如何使用 DepthAI 的 Sync 节点与 Demux 节点结合,以同步和解复用两个独立脚本节点的输出。每个脚本节点以不同的间隔生成数据缓冲区,这些缓冲区首先由 Sync 节点同步,然后由 MessageDemux 节点解复用。

类似示例

演示

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

设置

请运行 安装脚本 以下载所有必需的依赖项。请注意,此脚本必须在 git 上下文中运行,因此您必须先下载 depthai-python 存储库,然后运行脚本
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
有关更多信息,请遵循 安装指南

源代码

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

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 demux = pipeline.create<dai::node::MessageDemux>();
39
40    auto xout1 = pipeline.create<dai::node::XLinkOut>();
41    xout1->setStreamName("xout1");
42    auto xout2 = pipeline.create<dai::node::XLinkOut>();
43    xout2->setStreamName("xout2");
44
45    script1->outputs["out"].link(sync->inputs["s1"]);
46    script2->outputs["out"].link(sync->inputs["s2"]);
47    sync->out.link(demux->input);
48    demux->outputs["s1"].link(xout1->input);
49    demux->outputs["s2"].link(xout2->input);
50
51    dai::Device device(pipeline);
52    std::cout << "Start" << std::endl;
53    auto queue1 = device.getOutputQueue("xout1", 10, true);
54    auto queue2 = device.getOutputQueue("xout2", 10, true);
55    while(true) {
56        auto bufS1 = queue1->get<dai::Buffer>();
57        auto bufS2 = queue2->get<dai::Buffer>();
58        std::cout << "Buffer 1 timestamp: " << bufS1->getTimestamp().time_since_epoch().count() << std::endl;
59        std::cout << "Buffer 2 timestamp: " << bufS2->getTimestamp().time_since_epoch().count() << std::endl;
60        std::cout << "----------" << std::endl;
61        std::this_thread::sleep_for(std::chrono::milliseconds(200));
62    }
63}

工作原理

  • 初始化一个 DepthAI 管道。
  • 创建两个 Script 节点,每个脚本以不同的间隔生成并发送数据缓冲区。
  • 设置一个具有同步阈值的 Sync 节点。
  • 集成一个 MessageDemux 节点来分离同步的数据流。
  • 将 Script 节点的输出链接到 Sync 节点,然后从 Sync 节点链接到 MessageDemux 节点。
  • 启动管道并从 MessageDemux 节点持续接收解复用的数据。
  • 打印解复用数据的时戳以供比较。

管道

需要帮助?

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