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

本页目录

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

单色全分辨率保存器

此示例尽力从单色传感器以最快速度保存 1280x720 的 .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
3from pathlib import Path
4import cv2
5import depthai as dai
6import time
7
8# Create pipeline
9pipeline = dai.Pipeline()
10
11# Define source and output
12monoRight = pipeline.create(dai.node.MonoCamera)
13xoutRight = pipeline.create(dai.node.XLinkOut)
14
15xoutRight.setStreamName("right")
16
17# Properties
18monoRight.setCamera("right")
19monoRight.setResolution(dai.MonoCameraProperties.SensorResolution.THE_720_P)
20
21# Linking
22monoRight.out.link(xoutRight.input)
23
24# Connect to device and start pipeline
25with dai.Device(pipeline) as device:
26
27    # Output queue will be used to get the grayscale frames from the output defined above
28    qRight = device.getOutputQueue(name="right", maxSize=4, blocking=False)
29
30    dirName = "mono_data"
31    Path(dirName).mkdir(parents=True, exist_ok=True)
32
33    while True:
34        inRight = qRight.get()  # Blocking call, will wait until a new data has arrived
35        # Data is originally represented as a flat 1D array, it needs to be converted into HxW form
36        # Frame is transformed and ready to be shown
37        cv2.imshow("right", inRight.getCvFrame())
38
39        # After showing the frame, it's being stored inside a target directory as a PNG image
40        cv2.imwrite(f"{dirName}/{int(time.time() * 1000)}.png", inRight.getFrame())
41
42        if cv2.waitKey(1) == ord('q'):
43            break

C++

1#include <chrono>
2#include <iostream>
3
4// Includes common necessary includes for development using depthai library
5#include "depthai/depthai.hpp"
6#include "utility.hpp"
7
8int main() {
9    using namespace std::chrono;
10    // Create pipeline
11    dai::Pipeline pipeline;
12
13    // Define source and output
14    auto monoRight = pipeline.create<dai::node::MonoCamera>();
15    auto xoutRight = pipeline.create<dai::node::XLinkOut>();
16
17    xoutRight->setStreamName("right");
18
19    // Properties
20    monoRight->setCamera("right");
21    monoRight->setResolution(dai::MonoCameraProperties::SensorResolution::THE_720_P);
22
23    // Linking
24    monoRight->out.link(xoutRight->input);
25
26    // Connect to device and start pipeline
27    dai::Device device(pipeline);
28
29    // Output queue will be used to get the grayscale frames from the output defined above
30    auto qRight = device.getOutputQueue("right", 4, false);
31
32    std::string dirName = "mono_data";
33    createDirectory(dirName);
34
35    while(true) {
36        auto inRight = qRight->get<dai::ImgFrame>();
37        // Data is originally represented as a flat 1D array, it needs to be converted into HxW form
38        // Frame is transformed and ready to be shown
39        cv::imshow("right", inRight->getCvFrame());
40
41        uint64_t time = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
42        std::stringstream videoStr;
43        videoStr << dirName << "/" << time << ".png";
44        // After showing the frame, it's being stored inside a target directory as a PNG image
45        cv::imwrite(videoStr.str(), inRight->getCvFrame());
46
47        int key = cv::waitKey(1);
48        if(key == 'q' || key == 'Q') {
49            return 0;
50        }
51    }
52    return 0;
53}

管道

需要帮助?

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