DepthAI
软件栈

本页目录

  • Pipeline
  • 源代码

Neural Depth 基础示例

Supported on:RVC4
展示 NeuralDepth 基本用法及视差输出可视化的最小示例。

Pipeline

源代码

Python

Python
GitHub
1#!/usr/bin/env python3
2
3import cv2
4import depthai as dai
5
6FPS = 25
7
8# Create pipeline
9with dai.Pipeline() as pipeline:
10    cameraLeft = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_B, sensorFps=FPS)
11    cameraRight = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_C, sensorFps=FPS)
12    leftOutput = cameraLeft.requestFullResolutionOutput()
13    rightOutput = cameraRight.requestFullResolutionOutput()
14
15    neuralDepth = pipeline.create(dai.node.NeuralDepth).build(leftOutput, rightOutput, dai.DeviceModelZoo.NEURAL_DEPTH_LARGE)
16
17    depthQueue = neuralDepth.depth.createOutputQueue()
18
19    # Connect to device and start pipeline
20    pipeline.start()
21    while pipeline.isRunning():
22        depthData = depthQueue.get()
23        assert isinstance(depthData, dai.ImgFrame)
24        colorizedDepth = dai.utility.colorizeDepthFrame(depthData).getCvFrame()
25        cv2.imshow("depth", colorizedDepth)
26
27        key = cv2.waitKey(1)
28        if key == ord('q'):
29            pipeline.stop()
30            break
31
32        if cv2.waitKey(1) == ord('q'):
33            break

C++

1#include <atomic>
2#include <csignal>
3#include <iostream>
4#include <opencv2/opencv.hpp>
5#include <vector>
6
7#include "depthai/depthai.hpp"
8
9// Global flag to allow for a graceful shutdown
10std::atomic<bool> quitEvent(false);
11
12void signalHandler(int signum) {
13    quitEvent = true;
14}
15
16int main() {
17    // Set up signal handlers for clean exit on Ctrl+C
18    signal(SIGTERM, signalHandler);
19    signal(SIGINT, signalHandler);
20
21    constexpr float FPS = 25.0f;
22
23    // Create the DepthAI pipeline
24    dai::Pipeline pipeline;
25
26    // Define camera sources for the stereo pair
27    auto cameraLeft = pipeline.create<dai::node::Camera>();
28    cameraLeft->build(dai::CameraBoardSocket::CAM_B, std::nullopt, FPS);
29
30    auto cameraRight = pipeline.create<dai::node::Camera>();
31    cameraRight->build(dai::CameraBoardSocket::CAM_C, std::nullopt, FPS);
32
33    // Request full resolution output from each camera
34    auto* leftOutput = cameraLeft->requestFullResolutionOutput();
35    auto* rightOutput = cameraRight->requestFullResolutionOutput();
36
37    // Create and build the NeuralDepth node, linking the camera outputs to it
38    auto neuralDepth = pipeline.create<dai::node::NeuralDepth>();
39    neuralDepth->build(*leftOutput, *rightOutput, dai::DeviceModelZoo::NEURAL_DEPTH_LARGE);
40
41    // Create an output queue to get the depth frames from the node
42    auto depthQueue = neuralDepth->depth.createOutputQueue();
43
44    // Start the pipeline
45    pipeline.start();
46
47    while(!quitEvent && pipeline.isRunning()) {
48        auto depthData = depthQueue->get<dai::ImgFrame>();
49        cv::imshow("depth", dai::utility::colorizeDepthFrame(*depthData).getCvFrame());
50
51        // Check for keyboard input to quit
52        int key = cv::waitKey(1);
53        if(key == 'q') {
54            break;
55        }
56    }
57
58    // The pipeline is stopped automatically when the 'pipeline' object goes out of scope
59    // at the end of the main function.
60    return 0;
61}

需要帮助?

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