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

本页目录

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

视差编码

此示例演示如何编码 StereoDepth 的视差输出。请注意,您不应启用子像素模式,因为 VideoEncoder 不支持 UINT16。按 Ctrl+C 将停止录制,然后使用 ffmpeg 将其转换为 mp4 以便播放。请注意,ffmpeg 需要已安装并可运行才能成功转换为 mp4。请注意,此示例会将编码后的视频保存到您的主机存储中。因此,如果您让它一直运行,可能会填满您主机的存储空间。

类似示例:

演示

https://user-images.githubusercontent.com/18037362/138722539-649aef24-266f-4e83-b264-6f80ae896f5b.png

设置

请运行 安装脚本 以下载所有必需的依赖项。请注意,此脚本必须在 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 depthai as dai
4
5# NOTE Because we are encoding disparity values, output video will be a gray, and won't have many pixel levels - either 0..95 or 0..190
6
7# Create pipeline
8pipeline = dai.Pipeline()
9
10# Create left/right mono cameras for Stereo depth
11monoLeft = pipeline.create(dai.node.MonoCamera)
12monoLeft.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
13monoLeft.setCamera("left")
14
15monoRight = pipeline.create(dai.node.MonoCamera)
16monoRight.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
17monoRight.setCamera("right")
18
19# Create a node that will produce the depth map
20depth = pipeline.create(dai.node.StereoDepth)
21depth.setDefaultProfilePreset(dai.node.StereoDepth.PresetMode.HIGH_DENSITY)
22depth.initialConfig.setMedianFilter(dai.MedianFilter.KERNEL_7x7)
23depth.setLeftRightCheck(False)
24depth.setExtendedDisparity(False)
25# Subpixel disparity is of UINT16 format, which is unsupported by VideoEncoder
26depth.setSubpixel(False)
27monoLeft.out.link(depth.left)
28monoRight.out.link(depth.right)
29
30videoEnc = pipeline.create(dai.node.VideoEncoder)
31# Depth resolution/FPS will be the same as mono resolution/FPS
32videoEnc.setDefaultProfilePreset(monoLeft.getFps(), dai.VideoEncoderProperties.Profile.MJPEG)
33depth.disparity.link(videoEnc.input)
34
35xout = pipeline.create(dai.node.XLinkOut)
36xout.setStreamName("enc")
37videoEnc.bitstream.link(xout.input)
38
39# Connect to device and start pipeline
40with dai.Device(pipeline) as device:
41
42    # Output queue will be used to get the encoded data from the output defined above
43    q = device.getOutputQueue(name="enc")
44
45    # The .h265 file is a raw stream file (not playable yet)
46    with open('disparity.mjpeg', 'wb') as videoFile:
47        print("Press Ctrl+C to stop encoding...")
48        try:
49            while True:
50                videoFile.write(q.get().getData())
51        except KeyboardInterrupt:
52            # Keyboard interrupt (Ctrl + C) detected
53            pass
54
55    print("To view the encoded data, convert the stream file (.mjpeg) into a video file (.mp4) using a command below:")
56    print("ffmpeg -framerate 30 -i disparity.mjpeg -c copy video.mp4")

C++

1#include <csignal>
2#include <iostream>
3
4// Includes common necessary includes for development using depthai library
5#include "depthai/depthai.hpp"
6
7static std::atomic<bool> alive{true};
8static void sigintHandler(int signum) {
9    alive = false;
10}
11
12int main() {
13    using namespace std;
14    std::signal(SIGINT, &sigintHandler);
15
16    // Create pipeline
17    dai::Pipeline pipeline;
18
19    // Define sources and outputs
20    auto monoLeft = pipeline.create<dai::node::MonoCamera>();
21    monoLeft->setResolution(dai::MonoCameraProperties::SensorResolution::THE_400_P);
22    monoLeft->setCamera("left");
23
24    auto monoRight = pipeline.create<dai::node::MonoCamera>();
25    monoRight->setResolution(dai::MonoCameraProperties::SensorResolution::THE_400_P);
26    monoRight->setCamera("right");
27
28    auto stereo = pipeline.create<dai::node::StereoDepth>();
29    stereo->setDefaultProfilePreset(dai::node::StereoDepth::PresetMode::HIGH_DENSITY);
30    stereo->initialConfig.setMedianFilter(dai::MedianFilter::KERNEL_7x7);
31    stereo->setLeftRightCheck(false);
32    stereo->setExtendedDisparity(false);
33    // Subpixel disparity is of UINT16 format, which is unsupported by VideoEncoder
34    stereo->setSubpixel(false);
35    monoLeft->out.link(stereo->left);
36    monoRight->out.link(stereo->right);
37
38    auto videoEnc = pipeline.create<dai::node::VideoEncoder>();
39    videoEnc->setDefaultProfilePreset(monoLeft->getFps(), dai::VideoEncoderProperties::Profile::MJPEG);
40    stereo->disparity.link(videoEnc->input);
41
42    auto xout = pipeline.create<dai::node::XLinkOut>();
43    xout->setStreamName("disparity");
44    videoEnc->bitstream.link(xout->input);
45
46    // Connect to device and start pipeline
47    dai::Device device(pipeline);
48
49    // Output queue will be used to get the disparity frames from the outputs defined above
50    auto q = device.getOutputQueue("disparity");
51
52    auto videoFile = std::ofstream("disparity.mjpeg", std::ios::binary);
53    cout << "Press Ctrl+C to stop encoding..." << endl;
54
55    while(alive) {
56        auto h265Packet = q->get<dai::ImgFrame>();
57        videoFile.write((char*)(h265Packet->getData().data()), h265Packet->getData().size());
58    }
59
60    cout << "To view the encoded data, convert the stream file (.mjpeg) into a video file (.mp4) using a command below:" << endl;
61    cout << "ffmpeg -framerate 30 -i disparity.mjpeg -c copy video.mp4" << endl;
62    return 0;
63}

Pipeline

需要帮助?

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