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

本页目录

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

队列添加回调

此示例展示了如何使用队列回调。它通过一个 XLinkOut 节点将单色帧和彩色帧都从设备发送到主机。在回调函数 newFrame() 中,我们解码出帧来自哪个摄像头,以便稍后能以正确的标题向用户显示该帧。

演示

https://user-images.githubusercontent.com/18037362/120119546-309d5200-c190-11eb-932a-8235be7a4aa1.gif

设置

请运行 安装脚本 以下载所有必需的依赖项。请注意,此脚本必须在 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
2import cv2
3import depthai as dai
4import queue
5
6# Create pipeline
7pipeline = dai.Pipeline()
8
9# Add all three cameras
10camRgb = pipeline.create(dai.node.ColorCamera)
11left = pipeline.create(dai.node.MonoCamera)
12right = pipeline.create(dai.node.MonoCamera)
13
14# Create XLink output
15xout = pipeline.create(dai.node.XLinkOut)
16xout.setStreamName("frames")
17
18# Properties
19camRgb.setPreviewSize(300, 300)
20left.setCamera("left")
21left.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
22right.setCamera("right")
23right.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
24
25# Stream all the camera streams through the same XLink node
26camRgb.preview.link(xout.input)
27left.out.link(xout.input)
28right.out.link(xout.input)
29
30q = queue.Queue()
31
32def newFrame(inFrame):
33    global q
34    # Get "stream name" from the instance number
35    num = inFrame.getInstanceNum()
36    name = "color" if num == 0 else "left" if num == 1 else "right"
37    frame = inFrame.getCvFrame()
38    # This is a different thread and you could use it to
39    # run image processing algorithms here
40    q.put({"name": name, "frame": frame})
41
42# Connect to device and start pipeline
43with dai.Device(pipeline) as device:
44
45    # Add callback to the output queue "frames" for all newly arrived frames (color, left, right)
46    device.getOutputQueue(name="frames", maxSize=4, blocking=False).addCallback(newFrame)
47
48    while True:
49        # You could also get the data as non-blocking (block=False)
50        data = q.get(block=True)
51        cv2.imshow(data["name"], data["frame"])
52
53        if cv2.waitKey(1) == ord('q'):
54            break

C++

1#include <iostream>
2#include <queue>
3
4// Includes common necessary includes for development using depthai library
5#include "depthai/depthai.hpp"
6
7struct callbackType {
8    std::string name;
9    cv::Mat frame;
10};
11
12int main() {
13    // Create pipeline
14    dai::Pipeline pipeline;
15
16    // Add all three cameras
17    auto camRgb = pipeline.create<dai::node::ColorCamera>();
18    auto left = pipeline.create<dai::node::MonoCamera>();
19    auto right = pipeline.create<dai::node::MonoCamera>();
20
21    // Create XLink output
22    auto xout = pipeline.create<dai::node::XLinkOut>();
23    xout->setStreamName("frames");
24
25    // Properties
26    camRgb->setPreviewSize(300, 300);
27    left->setCamera("left");
28    left->setResolution(dai::MonoCameraProperties::SensorResolution::THE_400_P);
29    right->setCamera("right");
30    right->setResolution(dai::MonoCameraProperties::SensorResolution::THE_400_P);
31
32    // Stream all the camera streams through the same XLink node
33    camRgb->preview.link(xout->input);
34    left->out.link(xout->input);
35    right->out.link(xout->input);
36
37    auto queue = std::queue<callbackType>();
38    std::mutex queueMtx;
39
40    // Connect to device and start pipeline
41    dai::Device device(pipeline);
42
43    auto newFrame = [&queueMtx, &queue](std::shared_ptr<dai::ADatatype> callback) {
44        if(dynamic_cast<dai::ImgFrame*>(callback.get()) != nullptr) {
45            std::unique_lock<std::mutex> lock(queueMtx);
46            callbackType cb;
47            dai::ImgFrame* imgFrame = static_cast<dai::ImgFrame*>(callback.get());
48            auto num = imgFrame->getInstanceNum();
49            cb.name = num == 0 ? "color" : (num == 1 ? "left" : "right");
50            cb.frame = imgFrame->getCvFrame();
51            queue.push(cb);
52        }
53    };
54
55    // Add callback to the output queue "frames" for all newly arrived frames (color, left, right)
56    device.getOutputQueue("frames", 4, false)->addCallback(newFrame);
57
58    while(true) {
59        callbackType data;
60        {
61            std::unique_lock<std::mutex> lock(queueMtx);
62            if(!queue.empty()) {
63                data = queue.front();
64                queue.pop();
65            }
66        }
67
68        if(!data.frame.empty()) {
69            cv::imshow(data.name.c_str(), data.frame);
70        }
71
72        int key = cv::waitKey(1);
73        if(key == 'q' || key == 'Q') {
74            return 0;
75        }
76    }
77    return 0;
78}

管道

需要帮助?

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