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

本页目录

  • 演示
  • 设置
  • 源代码
  • Pipeline

OpenCV 支持

此示例展示了同时支持 NumPy 和 OpenCV 兼容图像类型的 API,以便于使用。 它使用 ColorCamera 节点来检索交错的 BGR 'preview' 和编码为 NV12 的 'video' 帧。 两者都使用 getFramegetCvFrame 函数进行显示。

演示

设置

请运行 安装脚本 以下载所有必需的依赖项。请注意,此脚本必须在 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
3import cv2
4import depthai as dai
5
6# Create pipeline
7pipeline = dai.Pipeline()
8
9# Define source and outputs
10camRgb = pipeline.create(dai.node.ColorCamera)
11xoutVideo = pipeline.create(dai.node.XLinkOut)
12xoutPreview = pipeline.create(dai.node.XLinkOut)
13
14xoutVideo.setStreamName("video")
15xoutPreview.setStreamName("preview")
16
17# Properties
18camRgb.setPreviewSize(300, 300)
19camRgb.setBoardSocket(dai.CameraBoardSocket.CAM_A)
20camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
21camRgb.setInterleaved(True)
22camRgb.setColorOrder(dai.ColorCameraProperties.ColorOrder.BGR)
23
24# Linking
25camRgb.video.link(xoutVideo.input)
26camRgb.preview.link(xoutPreview.input)
27
28# Connect to device and start pipeline
29with dai.Device(pipeline) as device:
30
31    video = device.getOutputQueue('video')
32    preview = device.getOutputQueue('preview')
33
34    while True:
35        videoFrame = video.get()
36        previewFrame = preview.get()
37
38        # Get BGR frame from NV12 encoded video frame to show with opencv
39        cv2.imshow("video", videoFrame.getCvFrame())
40        # Show 'preview' frame as is (already in correct format, no copy is made)
41        cv2.imshow("preview", previewFrame.getFrame())
42
43        if cv2.waitKey(1) == ord('q'):
44            break

C++

1#include <iostream>
2
3// Includes common necessary includes for development using depthai library
4#include "depthai/depthai.hpp"
5
6// Include OpenCV
7#include <opencv2/opencv.hpp>
8
9int main() {
10    // Create pipeline
11    dai::Pipeline pipeline;
12
13    // Define source and outputs
14    auto camRgb = pipeline.create<dai::node::ColorCamera>();
15    auto xoutVideo = pipeline.create<dai::node::XLinkOut>();
16    auto xoutPreview = pipeline.create<dai::node::XLinkOut>();
17
18    xoutVideo->setStreamName("video");
19    xoutPreview->setStreamName("preview");
20
21    // Properties
22    camRgb->setPreviewSize(300, 300);
23    camRgb->setBoardSocket(dai::CameraBoardSocket::CAM_A);
24    camRgb->setResolution(dai::ColorCameraProperties::SensorResolution::THE_1080_P);
25    camRgb->setInterleaved(true);
26    camRgb->setColorOrder(dai::ColorCameraProperties::ColorOrder::BGR);
27
28    // Linking
29    camRgb->video.link(xoutVideo->input);
30    camRgb->preview.link(xoutPreview->input);
31
32    // Connect to device and start pipeline
33    dai::Device device(pipeline);
34
35    auto video = device.getOutputQueue("video");
36    auto preview = device.getOutputQueue("preview");
37
38    while(true) {
39        auto videoFrame = video->get<dai::ImgFrame>();
40        auto previewFrame = preview->get<dai::ImgFrame>();
41
42        // Get BGR frame from NV12 encoded video frame to show with opencv
43        cv::imshow("video", videoFrame->getCvFrame());
44
45        // Show 'preview' frame as is (already in correct format, no copy is made)
46        cv::imshow("preview", previewFrame->getFrame());
47
48        int key = cv::waitKey(1);
49        if(key == 'q' || key == 'Q') return 0;
50    }
51    return 0;
52}

Pipeline

需要帮助?

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