DepthAI v2 has been superseded by DepthAI v3. You are viewing legacy documentation.
DepthAI 教程
DepthAI API 参考

本页目录

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

编码最大限制

此示例展示了如何设置编码器节点来同时编码 DepthAI/OAK-D 的 RGB 摄像头和两个灰度摄像头,所有编码器参数设置为最高质量和最高帧率。 RGB 设置为 4K (3840x2160),灰度设置为每个 1280x720,全部在 25FPS 下。 每个编码视频流通过 XLINK 传输并保存到相应文件中。按下 Ctrl+C 将停止录制,然后使用 ffmpeg 将其转换为 mp4 以实现可播放性。请注意,需要安装并运行 ffmpeg 才能成功转换为 mp4。请注意,此示例将编码视频保存到您的主机存储中。因此,如果让它持续运行,可能会填满主机的存储空间。此示例是 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 depthai as dai
4
5# Create pipeline
6pipeline = dai.Pipeline()
7
8# Define sources and outputs
9camRgb = pipeline.create(dai.node.ColorCamera)
10monoLeft = pipeline.create(dai.node.MonoCamera)
11monoRight = pipeline.create(dai.node.MonoCamera)
12ve1 = pipeline.create(dai.node.VideoEncoder)
13ve2 = pipeline.create(dai.node.VideoEncoder)
14ve3 = pipeline.create(dai.node.VideoEncoder)
15
16ve1Out = pipeline.create(dai.node.XLinkOut)
17ve2Out = pipeline.create(dai.node.XLinkOut)
18ve3Out = pipeline.create(dai.node.XLinkOut)
19
20ve1Out.setStreamName('ve1Out')
21ve2Out.setStreamName('ve2Out')
22ve3Out.setStreamName('ve3Out')
23
24# Properties
25camRgb.setBoardSocket(dai.CameraBoardSocket.CAM_A)
26camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_4_K)
27monoLeft.setCamera("left")
28monoRight.setCamera("right")
29
30# Setting to 26fps will trigger error
31ve1.setDefaultProfilePreset(25, dai.VideoEncoderProperties.Profile.H264_MAIN)
32ve2.setDefaultProfilePreset(25, dai.VideoEncoderProperties.Profile.H265_MAIN)
33ve3.setDefaultProfilePreset(25, dai.VideoEncoderProperties.Profile.H264_MAIN)
34
35# Linking
36monoLeft.out.link(ve1.input)
37camRgb.video.link(ve2.input)
38monoRight.out.link(ve3.input)
39
40ve1.bitstream.link(ve1Out.input)
41ve2.bitstream.link(ve2Out.input)
42ve3.bitstream.link(ve3Out.input)
43
44# Connect to device and start pipeline
45with dai.Device(pipeline) as dev:
46
47    # Output queues will be used to get the encoded data from the output defined above
48    outQ1 = dev.getOutputQueue('ve1Out', maxSize=30, blocking=True)
49    outQ2 = dev.getOutputQueue('ve2Out', maxSize=30, blocking=True)
50    outQ3 = dev.getOutputQueue('ve3Out', maxSize=30, blocking=True)
51
52    # Processing loop
53    with open('mono1.h264', 'wb') as fileMono1H264, open('color.h265', 'wb') as fileColorH265, open('mono2.h264', 'wb') as fileMono2H264:
54        print("Press Ctrl+C to stop encoding...")
55        while True:
56            try:
57                # Empty each queue
58                while outQ1.has():
59                    outQ1.get().getData().tofile(fileMono1H264)
60
61                while outQ2.has():
62                    outQ2.get().getData().tofile(fileColorH265)
63
64                while outQ3.has():
65                    outQ3.get().getData().tofile(fileMono2H264)
66            except KeyboardInterrupt:
67                break
68
69    print("To view the encoded data, convert the stream file (.h264/.h265) into a video file (.mp4), using commands below:")
70    cmd = "ffmpeg -framerate 25 -i {} -c copy {}"
71    print(cmd.format("mono1.h264", "mono1.mp4"))
72    print(cmd.format("mono2.h264", "mono2.mp4"))
73    print(cmd.format("color.h265", "color.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
7// Keyboard interrupt (Ctrl + C) detected
8static std::atomic<bool> alive{true};
9static void sigintHandler(int signum) {
10    alive = false;
11}
12
13int main() {
14    using namespace std;
15    using namespace std::chrono;
16    std::signal(SIGINT, &sigintHandler);
17
18    // Create pipeline
19    dai::Pipeline pipeline;
20
21    // Define sources and outputs
22    auto camRgb = pipeline.create<dai::node::ColorCamera>();
23    auto monoLeft = pipeline.create<dai::node::MonoCamera>();
24    auto monoRight = pipeline.create<dai::node::MonoCamera>();
25    auto ve1 = pipeline.create<dai::node::VideoEncoder>();
26    auto ve2 = pipeline.create<dai::node::VideoEncoder>();
27    auto ve3 = pipeline.create<dai::node::VideoEncoder>();
28
29    auto ve1Out = pipeline.create<dai::node::XLinkOut>();
30    auto ve2Out = pipeline.create<dai::node::XLinkOut>();
31    auto ve3Out = pipeline.create<dai::node::XLinkOut>();
32
33    ve1Out->setStreamName("ve1Out");
34    ve2Out->setStreamName("ve2Out");
35    ve3Out->setStreamName("ve3Out");
36
37    // Properties
38    camRgb->setBoardSocket(dai::CameraBoardSocket::CAM_A);
39    camRgb->setResolution(dai::ColorCameraProperties::SensorResolution::THE_4_K);
40    monoLeft->setCamera("left");
41    monoRight->setCamera("right");
42
43    // Setting to 26fps will trigger error
44    ve1->setDefaultProfilePreset(25, dai::VideoEncoderProperties::Profile::H264_MAIN);
45    ve2->setDefaultProfilePreset(25, dai::VideoEncoderProperties::Profile::H265_MAIN);
46    ve3->setDefaultProfilePreset(25, dai::VideoEncoderProperties::Profile::H264_MAIN);
47
48    // Linking
49    monoLeft->out.link(ve1->input);
50    camRgb->video.link(ve2->input);
51    monoRight->out.link(ve3->input);
52
53    ve1->bitstream.link(ve1Out->input);
54    ve2->bitstream.link(ve2Out->input);
55    ve3->bitstream.link(ve3Out->input);
56
57    // Connect to device and start pipeline
58    dai::Device device(pipeline);
59
60    // Output queues will be used to get the encoded data from the output defined above
61    auto outQ1 = device.getOutputQueue("ve1Out", 30, true);
62    auto outQ2 = device.getOutputQueue("ve2Out", 30, true);
63    auto outQ3 = device.getOutputQueue("ve3Out", 30, true);
64
65    // The .h264 / .h265 files are raw stream files (not playable yet)
66    auto videoFile1 = ofstream("mono1.h264", ios::binary);
67    auto videoFile2 = ofstream("color.h265", ios::binary);
68    auto videoFile3 = ofstream("mono2.h264", ios::binary);
69    cout << "Press Ctrl+C to stop encoding..." << endl;
70
71    while(alive) {
72        auto out1 = outQ1->get<dai::ImgFrame>();
73        videoFile1.write((char*)out1->getData().data(), out1->getData().size());
74        auto out2 = outQ2->get<dai::ImgFrame>();
75        videoFile2.write((char*)out2->getData().data(), out2->getData().size());
76        auto out3 = outQ3->get<dai::ImgFrame>();
77        videoFile3.write((char*)out3->getData().data(), out3->getData().size());
78    }
79
80    cout << "To view the encoded data, convert the stream file (.h264/.h265) into a video file (.mp4), using a command below:" << endl;
81    cout << "ffmpeg -framerate 25 -i mono1.h264 -c copy mono1.mp4" << endl;
82    cout << "ffmpeg -framerate 25 -i mono2.h264 -c copy mono2.mp4" << endl;
83    cout << "ffmpeg -framerate 25 -i color.h265 -c copy color.mp4" << endl;
84
85    return 0;
86}

管道

需要帮助?

请前往 OAKChina 官网 获取技术支持或解答您的任何疑问。