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

本页目录

  • 演示
  • 设置
  • 源代码
  • 管道

设备队列事件

此示例演示了如何使用 getQueueEvent 函数,以便在选定流的某个数据包到达时收到通知。

演示

设置

请运行 安装脚本 以下载所有必需的依赖项。请注意,此脚本必须在 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
1#!/usr/bin/env python3
2
3"""
4 This example demonstrates use of queue events to block a thread until a message
5 arrives to any (of the specified) queue
6"""
7
8import cv2
9import depthai as dai
10
11# Create pipeline
12pipeline = dai.Pipeline()
13
14# Define sources and outputs
15camRgb = pipeline.create(dai.node.ColorCamera)
16camMono = pipeline.create(dai.node.MonoCamera)
17xoutRgb = pipeline.create(dai.node.XLinkOut)
18xoutMono = pipeline.create(dai.node.XLinkOut)
19
20xoutRgb.setStreamName("rgb")
21xoutMono.setStreamName("mono")
22
23# Properties
24camRgb.setInterleaved(True)
25camRgb.setPreviewSize(300, 300)
26
27# Linking
28camRgb.preview.link(xoutRgb.input)
29camMono.out.link(xoutMono.input)
30
31# Connect to device and start pipeline
32with dai.Device(pipeline) as device:
33
34    # Clear queue events
35    device.getQueueEvents()
36
37    while True:
38        # Block until a message arrives to any of the specified queues
39        queueName = device.getQueueEvent(("rgb", "mono"))
40
41        # Getting that message from queue with name specified by the event
42        # Note: number of events doesn't necessarily match number of messages in queues
43        # because queues can be set to non-blocking (overwriting) behavior
44        message = device.getOutputQueue(queueName).get()
45
46        # Display arrived frames
47        if type(message) == dai.ImgFrame:
48            cv2.imshow(queueName, message.getCvFrame())
49
50        if cv2.waitKey(1) == ord('q'):
51            break

C++

1#include <iostream>
2
3// Includes common necessary includes for development using depthai library
4#include "depthai/depthai.hpp"
5
6int main() {
7    // Create pipeline
8    dai::Pipeline pipeline;
9
10    // Define sources and outputs
11    auto camRgb = pipeline.create<dai::node::ColorCamera>();
12    auto camMono = pipeline.create<dai::node::MonoCamera>();
13    auto xoutRgb = pipeline.create<dai::node::XLinkOut>();
14    auto xoutMono = pipeline.create<dai::node::XLinkOut>();
15
16    xoutRgb->setStreamName("rgb");
17    xoutMono->setStreamName("mono");
18
19    // Properties
20    camRgb->setInterleaved(true);
21    camRgb->setPreviewSize(300, 300);
22
23    // Linking
24    camRgb->preview.link(xoutRgb->input);
25    camMono->out.link(xoutMono->input);
26
27    // Connect to device and start pipeline
28    dai::Device device(pipeline);
29
30    // Clear queue events
31    device.getQueueEvents();
32
33    while(true) {
34        auto ev = device.getQueueEvent();
35
36        if(ev == "rgb") {
37            auto rgb = device.getOutputQueue(ev)->get<dai::ImgFrame>();
38            cv::imshow("rgb", rgb->getFrame());
39        } else if(ev == "mono") {
40            auto mono = device.getOutputQueue(ev)->get<dai::ImgFrame>();
41            cv::imshow("mono", mono->getFrame());
42        }
43
44        int key = cv::waitKey(1);
45        if(key == 'q' || key == 'Q') {
46            return 0;
47        }
48    }
49    return 0;
50}

管道

需要帮助?

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