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

本页目录

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

RGB 预览

此示例展示了如何设置一个输出 RGB 摄像头小预览的管道,通过 XLink 实时传输到主机,并在主机上使用 OpenCV 显示 RGB 帧。请注意,预览帧不适用于更高分辨率(例如 1920x1080)。 预览更适合用于神经网络或可视化目的。 请查看 ColorCamera 节点以获得更好的视图。如果您想获取更高分辨率的 RGB 帧样本,请访问 RGB 视频

类似示例:

演示

设置

请运行 安装脚本 以下载所有必需的依赖项。请注意,此脚本必须在 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 output
10camRgb = pipeline.create(dai.node.ColorCamera)
11xoutRgb = pipeline.create(dai.node.XLinkOut)
12
13xoutRgb.setStreamName("rgb")
14
15# Properties
16camRgb.setPreviewSize(300, 300)
17camRgb.setInterleaved(False)
18camRgb.setColorOrder(dai.ColorCameraProperties.ColorOrder.RGB)
19
20# Linking
21camRgb.preview.link(xoutRgb.input)
22
23# Connect to device and start pipeline
24with dai.Device(pipeline) as device:
25
26    print('Connected cameras:', device.getConnectedCameraFeatures())
27    # Print out usb speed
28    print('Usb speed:', device.getUsbSpeed().name)
29    # Bootloader version
30    if device.getBootloaderVersion() is not None:
31        print('Bootloader version:', device.getBootloaderVersion())
32    # Device name
33    print('Device name:', device.getDeviceName(), ' Product name:', device.getProductName())
34
35    # Output queue will be used to get the rgb frames from the output defined above
36    qRgb = device.getOutputQueue(name="rgb", maxSize=4, blocking=False)
37
38    while True:
39        inRgb = qRgb.get()  # blocking call, will wait until a new data has arrived
40
41        # Retrieve 'bgr' (opencv format) frame
42        cv2.imshow("rgb", inRgb.getCvFrame())
43
44        if cv2.waitKey(1) == ord('q'):
45            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    using namespace std;
8    // Create pipeline
9    dai::Pipeline pipeline;
10
11    // Define source and output
12    auto camRgb = pipeline.create<dai::node::ColorCamera>();
13    auto xoutRgb = pipeline.create<dai::node::XLinkOut>();
14
15    xoutRgb->setStreamName("rgb");
16
17    // Properties
18    camRgb->setPreviewSize(300, 300);
19    camRgb->setBoardSocket(dai::CameraBoardSocket::CAM_A);
20    camRgb->setResolution(dai::ColorCameraProperties::SensorResolution::THE_1080_P);
21    camRgb->setInterleaved(false);
22    camRgb->setColorOrder(dai::ColorCameraProperties::ColorOrder::RGB);
23
24    // Linking
25    camRgb->preview.link(xoutRgb->input);
26
27    // Connect to device and start pipeline
28    dai::Device device(pipeline, dai::UsbSpeed::SUPER);
29
30    cout << "Connected cameras: " << device.getConnectedCameraFeatures() << endl;
31
32    // Print USB speed
33    cout << "Usb speed: " << device.getUsbSpeed() << endl;
34
35    // Bootloader version
36    if(device.getBootloaderVersion()) {
37        cout << "Bootloader version: " << device.getBootloaderVersion()->toString() << endl;
38    }
39
40    // Device name
41    cout << "Device name: " << device.getDeviceName() << " Product name: " << device.getProductName() << endl;
42
43    // Output queue will be used to get the rgb frames from the output defined above
44    auto qRgb = device.getOutputQueue("rgb", 4, false);
45
46    while(true) {
47        auto inRgb = qRgb->get<dai::ImgFrame>();
48
49        // Retrieve 'bgr' (opencv format) frame
50        cv::imshow("rgb", inRgb->getCvFrame());
51
52        int key = cv::waitKey(1);
53        if(key == 'q' || key == 'Q') {
54            break;
55        }
56    }
57    return 0;
58}

管道

需要帮助?

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