此页面由 AI 自动翻译。查看英文原版
DepthAI
  • DepthAI 组件
    • AprilTags
    • 基准测试
    • 相机
    • 校准
    • 深度
    • 检测网络
    • 事件
    • 特征跟踪器
    • 网关
    • 主机节点
    • 图像对齐
    • 图像处理
    • IMU
    • 杂项
    • 模型库
    • 神经深度
    • 神经网络
    • 对象跟踪器
    • 点云
    • 记录回放
    • RGBD
    • 脚本
    • 空间检测网络
    • 空间位置计算器
    • 立体深度
    • 同步
    • ToF
    • 视频编码器
    • 可视化工具
    • VSLAM
    • 扭曲
    • RVC2-特定
  • 高级教程
  • API 参考
  • 工具
软件栈
最后更新

本页目录

  • 演示内容
  • 源代码
  • 类似示例
Claude CodeCodexCursor
Luxonis 智能体工具包为你常用的编程智能体加载 Luxonis 上下文与技能,更快地基于 OAK 开发应用。
安装工具包
最后更新
Supported on:RVC2RVC4
此示例演示了 Depth 节点。在默认设置 (Algorithm.AUTO) 下,该节点会为您的设备选择最佳的深度后端——RVC4 上的 NeuralDepth,其他设备上的 ToF 或 StereoDepth——并可视化 depth 和 confidence 输出。使用 CLI 标志来执行 build() 重载:仅 FPS、算法 + FPS/分辨率,或固定算法 + 配置对。此示例需要 DepthAI v3 API,请参阅 安装说明。

演示内容

  • 使用 AUTO 后端选择创建 Depth 节点
  • 连接后读取 getResolvedAlgorithm() 和 getResolvedConfig()
  • 可选的 --fps、--width/--height、--algorithm 和 --config CLI 配置
  • 可选的 --user-cameras 用于预构建 Depth 重用的立体相机节点

源代码

Python

Python
GitHub
1#!/usr/bin/env python3
2"""
3Unified Depth node demo.
4
5The Depth node picks an algorithm and backend config automatically from the connected device,
6target FPS, and/or stereo resolution (Algorithm.AUTO). You can also pin the algorithm yourself
7via CLI or Depth.build().
8
9Supported algorithms: AUTO, STEREO, NEURAL, NEURAL_ASSISTED_STEREO, TOF, GPU_STEREO.
10
11Configure the node via CLI to exercise Depth.build() overloads (fps-only, algorithm + fps/res,
12algorithm + config) or pre-built user stereo cameras.
13"""
14
15from __future__ import annotations
16
17import argparse
18import sys
19
20import cv2
21import depthai as dai
22import numpy as np
23
24_COLOR_MAP = cv2.applyColorMap(np.arange(256, dtype=np.uint8), cv2.COLORMAP_JET)
25_COLOR_MAP[0] = [0, 0, 0]
26
27_ALGORITHM_CHOICES = {
28    "auto": dai.node.Depth.Algorithm.AUTO,
29    "stereo": dai.node.Depth.Algorithm.STEREO,
30    "neural": dai.node.Depth.Algorithm.NEURAL,
31    "neural_assisted_stereo": dai.node.Depth.Algorithm.NEURAL_ASSISTED_STEREO,
32    "tof": dai.node.Depth.Algorithm.TOF,
33    "gpu_stereo": dai.node.Depth.Algorithm.GPU_STEREO,
34}
35
36
37def colorizeConfidence(frame: np.ndarray) -> np.ndarray:
38    if frame.dtype == np.uint16:
39        vmax = int(np.max(frame))
40        if vmax <= 0:
41            return np.zeros((*frame.shape, 3), dtype=np.uint8)
42        vis = ((frame.astype(np.float32) / vmax) * 255).astype(np.uint8)
43    else:
44        vis = frame
45    return cv2.applyColorMap(vis, _COLOR_MAP)
46
47
48def parseConfig(name: str):
49    if hasattr(dai.DeviceModelZoo, name):
50        return getattr(dai.DeviceModelZoo, name)
51    if hasattr(dai.node.StereoDepth.PresetMode, name):
52        return getattr(dai.node.StereoDepth.PresetMode, name)
53    raise argparse.ArgumentTypeError(
54        f"Unknown config '{name}'. Use a DeviceModelZoo name (e.g. NEURAL_DEPTH_LARGE) "
55        "or StereoDepth.PresetMode name (e.g. DEFAULT)."
56    )
57
58
59def sizeFromArgs(args: argparse.Namespace) -> tuple[int, int] | None:
60    if args.width is None and args.height is None:
61        return None
62    if args.width is None or args.height is None:
63        raise argparse.ArgumentTypeError("--width and --height must be specified together")
64    return args.width, args.height
65
66
67def buildUserStereoCameras(pipeline: dai.Pipeline, args: argparse.Namespace) -> None:
68    device = pipeline.getDefaultDevice()
69    if device is None:
70        raise RuntimeError("Connect a device (host-only pipeline cannot use Depth).")
71
72    stereoPairs = device.getStereoPairs()
73    if not stereoPairs:
74        raise RuntimeError("This device has no stereo pair; Depth cannot run.")
75
76    stereoPair = stereoPairs[0]
77    size = sizeFromArgs(args)
78    pipeline.create(dai.node.Camera).build(stereoPair.left, size, args.fps)
79    pipeline.create(dai.node.Camera).build(stereoPair.right, size, args.fps)
80
81
82def configureDepth(depthNode: dai.node.Depth, args: argparse.Namespace) -> None:
83    fps = None if args.userCameras else args.fps
84    size = None if args.userCameras else sizeFromArgs(args)
85    algorithm = _ALGORITHM_CHOICES[args.algorithm] if args.algorithm is not None else None
86    config = parseConfig(args.config) if args.config is not None else None
87
88    if algorithm is not None and config is not None:
89        depthNode.build(algorithm, config, fps=fps, size=size)
90    elif algorithm is not None:
91        depthNode.build(algorithm, fps=fps, size=size)
92    elif fps is not None and size is not None:
93        depthNode.build(dai.node.Depth.Algorithm.AUTO, fps=fps, size=size)
94    elif fps is not None:
95        depthNode.build(fps=fps)
96    elif size is not None:
97        depthNode.build(dai.node.Depth.Algorithm.AUTO, size=size)
98
99
100def buildParser() -> argparse.ArgumentParser:
101    parser = argparse.ArgumentParser(
102        description=__doc__,
103        formatter_class=argparse.RawDescriptionHelpFormatter,
104        epilog="""\
105Examples:
106  %(prog)s
107  %(prog)s --fps 30
108  %(prog)s --width 640 --height 480
109  %(prog)s --algorithm neural --config NEURAL_DEPTH_LARGE --fps 8
110  %(prog)s --algorithm stereo --config DEFAULT --fps 20
111  %(prog)s --user-cameras --width 1280 --height 800 --fps 30
112  %(prog)s --ip 192.168.1.10
113""",
114    )
115    parser.add_argument(
116        "--ip",
117        metavar="ADDR",
118        default=None,
119        help="Device IP for TCP/IP (e.g. PoE). If omitted, the default device search is used (often first USB).",
120    )
121    parser.add_argument("--fps", type=float, default=None, help="Stereo camera FPS for Depth.build() or user cameras")
122    parser.add_argument("--width", type=int, default=None, help="Stereo frame width (requires --height)")
123    parser.add_argument("--height", type=int, default=None, help="Stereo frame height (requires --width)")
124    parser.add_argument(
125        "--algorithm",
126        choices=sorted(_ALGORITHM_CHOICES.keys()),
127        default=None,
128        help="Depth backend selection (default: AUTO when omitted)",
129    )
130    parser.add_argument(
131        "--config",
132        type=parseConfig,
133        default=None,
134        metavar="NAME",
135        help="Algorithm config: DeviceModelZoo (NEURAL*) or StereoDepth.PresetMode (DEFAULT, ...)",
136    )
137    parser.add_argument(
138        "--user-cameras",
139        dest="userCameras",
140        action="store_true",
141        help="Pre-build stereo Camera nodes; fps/res come from args instead of Depth.build()",
142    )
143    return parser
144
145
146def main() -> int:
147    parser = buildParser()
148    args = parser.parse_args()
149
150    try:
151        sizeFromArgs(args)
152    except argparse.ArgumentTypeError as exc:
153        parser.error(str(exc))
154
155    if args.config is not None and args.algorithm is None:
156        parser.error("--config requires --algorithm")
157
158    if args.ip:
159        device = dai.Device(dai.DeviceInfo(args.ip))
160        pipeline = dai.Pipeline(device)
161    else:
162        pipeline = dai.Pipeline()
163
164    if args.userCameras:
165        buildUserStereoCameras(pipeline, args)
166
167    depthNode = pipeline.create(dai.node.Depth)
168    configureDepth(depthNode, args)
169
170    depthQueue = depthNode.depth.createOutputQueue()
171    confidenceQueue = depthNode.confidence.createOutputQueue()
172
173    pipeline.build()
174
175    print("Resolved algorithm:", depthNode.getResolvedAlgorithm())
176    print("Resolved config:", depthNode.getResolvedConfig())
177
178    with pipeline:
179        pipeline.start()
180        while pipeline.isRunning():
181            depthFrame = depthQueue.get()
182            assert isinstance(depthFrame, dai.ImgFrame)
183            cv2.imshow("depth", dai.utility.colorizeDepthFrame(depthFrame).getCvFrame())
184
185            confidenceFrame = confidenceQueue.get()
186            assert isinstance(confidenceFrame, dai.ImgFrame)
187            cv2.imshow("confidence", colorizeConfidence(confidenceFrame.getFrame()))
188
189            if cv2.waitKey(1) == ord("q"):
190                pipeline.stop()
191                break
192
193        cv2.destroyAllWindows()
194
195    return 0
196
197
198if __name__ == "__main__":
199    sys.exit(main())

C++

1/**
2 * Unified Depth node demo.
3 *
4 * The Depth node picks an algorithm and backend config automatically from the connected device,
5 * target FPS, and/or stereo resolution (Algorithm::AUTO). You can also pin the algorithm yourself
6 * via CLI or Depth::build().
7 *
8 * Supported algorithms: AUTO, STEREO, NEURAL, NEURAL_ASSISTED_STEREO, TOF, GPU_STEREO.
9 *
10 * Configure the node via CLI to exercise Depth::build() overloads (fps-only, algorithm + fps/res,
11 * algorithm + config) or pre-built user stereo cameras.
12 */
13#include <algorithm>
14#include <argparse/argparse.hpp>
15#include <cstdlib>
16#include <iostream>
17#include <map>
18#include <opencv2/opencv.hpp>
19#include <optional>
20#include <string>
21#include <utility>
22#include <variant>
23#include <vector>
24
25#include "depthai/common/DeviceModelZoo.hpp"
26#include "depthai/depthai.hpp"
27#include "depthai/pipeline/node/Camera.hpp"
28#include "depthai/pipeline/node/Depth.hpp"
29#include "depthai/pipeline/node/StereoDepth.hpp"
30
31namespace {
32
33cv::Mat colorizeConfidence(const cv::Mat& frame) {
34    if(frame.empty() || frame.channels() != 1) {
35        return cv::Mat::zeros(frame.size(), CV_8UC3);
36    }
37
38    cv::Mat normalized;
39    if(frame.depth() == CV_16U) {
40        double maxValue = 0.0;
41        cv::minMaxLoc(frame, nullptr, &maxValue);
42        if(maxValue <= 0.0) {
43            return cv::Mat::zeros(frame.size(), CV_8UC3);
44        }
45        cv::Mat scaled;
46        frame.convertTo(scaled, CV_32F, 255.0 / maxValue);
47        scaled.convertTo(normalized, CV_8U);
48    } else {
49        frame.convertTo(normalized, CV_8U);
50    }
51
52    cv::Mat colorized;
53    cv::applyColorMap(normalized, colorized, cv::COLORMAP_JET);
54    return colorized;
55}
56
57const std::map<std::string, dai::node::Depth::Algorithm>& algorithmMap() {
58    static const std::map<std::string, dai::node::Depth::Algorithm> map = {
59        {"auto", dai::node::Depth::Algorithm::AUTO},
60        {"stereo", dai::node::Depth::Algorithm::STEREO},
61        {"neural", dai::node::Depth::Algorithm::NEURAL},
62        {"neural_assisted_stereo", dai::node::Depth::Algorithm::NEURAL_ASSISTED_STEREO},
63        {"tof", dai::node::Depth::Algorithm::TOF},
64        {"gpu_stereo", dai::node::Depth::Algorithm::GPU_STEREO},
65    };
66    return map;
67}
68
69const char* algorithmName(dai::node::Depth::Algorithm algorithm) {
70    for(const auto& [name, value] : algorithmMap()) {
71        if(value == algorithm) {
72            return name.c_str();
73        }
74    }
75    return "unknown";
76}
77
78std::optional<dai::node::Depth::Algorithm> parseAlgorithm(const std::string& name) {
79    const auto it = algorithmMap().find(name);
80    if(it == algorithmMap().end()) {
81        return std::nullopt;
82    }
83    return it->second;
84}
85
86std::optional<dai::node::Depth::Config> parseConfig(const std::string& name) {
87    static const std::map<std::string, dai::DeviceModelZoo> modelMap = {
88        {"NEURAL_DEPTH_1248X780", dai::DeviceModelZoo::NEURAL_DEPTH_1248X780},
89        {"NEURAL_DEPTH_768X480", dai::DeviceModelZoo::NEURAL_DEPTH_768X480},
90        {"NEURAL_DEPTH_576X360", dai::DeviceModelZoo::NEURAL_DEPTH_576X360},
91        {"NEURAL_DEPTH_480X300", dai::DeviceModelZoo::NEURAL_DEPTH_480X300},
92        {"NEURAL_DEPTH_384X240", dai::DeviceModelZoo::NEURAL_DEPTH_384X240},
93        {"NEURAL_DEPTH_1056X660", dai::DeviceModelZoo::NEURAL_DEPTH_1056X660},
94        {"NEURAL_DEPTH_960X600", dai::DeviceModelZoo::NEURAL_DEPTH_960X600},
95        {"NEURAL_DEPTH_864X540", dai::DeviceModelZoo::NEURAL_DEPTH_864X540},
96        {"NEURAL_DEPTH_288X180", dai::DeviceModelZoo::NEURAL_DEPTH_288X180},
97        {"NEURAL_DEPTH_192X120", dai::DeviceModelZoo::NEURAL_DEPTH_192X120},
98        {"NEURAL_DEPTH_EXTRA_LARGE", dai::DeviceModelZoo::NEURAL_DEPTH_EXTRA_LARGE},
99        {"NEURAL_DEPTH_LARGE", dai::DeviceModelZoo::NEURAL_DEPTH_LARGE},
100        {"NEURAL_DEPTH_MEDIUM", dai::DeviceModelZoo::NEURAL_DEPTH_MEDIUM},
101        {"NEURAL_DEPTH_SMALL", dai::DeviceModelZoo::NEURAL_DEPTH_SMALL},
102        {"NEURAL_DEPTH_NANO", dai::DeviceModelZoo::NEURAL_DEPTH_NANO},
103    };
104    static const std::map<std::string, dai::node::StereoDepth::PresetMode> presetMap = {
105        {"FAST_ACCURACY", dai::node::StereoDepth::PresetMode::FAST_ACCURACY},
106        {"FAST_DENSITY", dai::node::StereoDepth::PresetMode::FAST_DENSITY},
107        {"DEFAULT", dai::node::StereoDepth::PresetMode::DEFAULT},
108        {"FACE", dai::node::StereoDepth::PresetMode::FACE},
109        {"HIGH_DETAIL", dai::node::StereoDepth::PresetMode::HIGH_DETAIL},
110        {"ROBOTICS", dai::node::StereoDepth::PresetMode::ROBOTICS},
111        {"DENSITY", dai::node::StereoDepth::PresetMode::DENSITY},
112        {"ACCURACY", dai::node::StereoDepth::PresetMode::ACCURACY},
113    };
114
115    if(const auto modelIt = modelMap.find(name); modelIt != modelMap.end()) {
116        return dai::node::Depth::Config{modelIt->second};
117    }
118    if(const auto presetIt = presetMap.find(name); presetIt != presetMap.end()) {
119        return dai::node::Depth::Config{presetIt->second};
120    }
121    return std::nullopt;
122}
123
124struct CliOptions {
125    std::string ip;
126    std::optional<float> fps;
127    std::optional<uint32_t> width;
128    std::optional<uint32_t> height;
129    std::optional<std::string> algorithm;
130    std::optional<std::string> config;
131    bool userCameras{false};
132};
133
134std::optional<std::pair<uint32_t, uint32_t>> sizeFromOptions(const CliOptions& options) {
135    if(!options.width && !options.height) {
136        return std::nullopt;
137    }
138    if(!options.width || !options.height) {
139        throw std::runtime_error("--width and --height must be specified together");
140    }
141    return std::make_pair(*options.width, *options.height);
142}
143
144void buildUserStereoCameras(dai::Pipeline& pipeline, const CliOptions& options) {
145    const auto device = pipeline.getDefaultDevice();
146    if(device == nullptr) {
147        throw std::runtime_error("Connect a device (host-only pipeline cannot use Depth).");
148    }
149
150    const auto stereoPairs = device->getStereoPairs();
151    if(stereoPairs.empty()) {
152        throw std::runtime_error("This device has no stereo pair; Depth cannot run.");
153    }
154
155    const auto stereoPair = stereoPairs[0];
156    const auto size = sizeFromOptions(options);
157    const std::optional<float> fps = options.fps;
158
159    pipeline.create<dai::node::Camera>()->build(stereoPair.left, size, fps);
160    pipeline.create<dai::node::Camera>()->build(stereoPair.right, size, fps);
161}
162
163void configureDepth(const std::shared_ptr<dai::node::Depth>& depth, const CliOptions& options) {
164    const auto fps = options.userCameras ? std::nullopt : options.fps;
165    const auto size = options.userCameras ? std::nullopt : sizeFromOptions(options);
166    const auto algorithm = options.algorithm ? parseAlgorithm(*options.algorithm) : std::nullopt;
167    const auto config = options.config ? parseConfig(*options.config) : std::nullopt;
168
169    if(options.algorithm && !algorithm) {
170        throw std::runtime_error("Invalid --algorithm value");
171    }
172    if(options.config && !config) {
173        throw std::runtime_error("Unknown --config value. Use a DeviceModelZoo or StereoDepth.PresetMode name.");
174    }
175
176    if(algorithm && config) {
177        depth->build(*algorithm, *config, fps, size);
178    } else if(algorithm) {
179        depth->build(*algorithm, fps, size);
180    } else if(fps && size) {
181        depth->build(dai::node::Depth::Algorithm::AUTO, *fps, size);
182    } else if(fps) {
183        depth->build(*fps);
184    } else if(size) {
185        depth->build(dai::node::Depth::Algorithm::AUTO, std::nullopt, size);
186    }
187}
188
189void printResolvedConfig(const dai::node::Depth::Config& config) {
190    std::visit(
191        [](const auto& value) {
192            using T = std::decay_t<decltype(value)>;
193            if constexpr(std::is_same_v<T, std::monostate>) {
194                std::cout << "none";
195            } else if constexpr(std::is_same_v<T, dai::DeviceModelZoo>) {
196                std::cout << static_cast<int>(value);
197            } else {
198                std::cout << static_cast<int>(value);
199            }
200        },
201        config);
202    std::cout << std::endl;
203}
204
205dai::Pipeline makePipeline(const CliOptions& options) {
206    if(options.ip.empty()) {
207        return dai::Pipeline();
208    }
209    auto device = std::make_shared<dai::Device>(dai::DeviceInfo(options.ip));
210    return dai::Pipeline(device);
211}
212
213}  // namespace
214
215int main(int argc, char** argv) {
216    argparse::ArgumentParser program("unified_depth", "1.0.0");
217    program.add_description(
218        "Unified Depth node demo.\n\n"
219        "The Depth node picks an algorithm and backend config automatically from the connected device, "
220        "target FPS, and/or stereo resolution (Algorithm::AUTO). You can also pin the algorithm yourself "
221        "via CLI or Depth::build().\n\n"
222        "Supported algorithms: AUTO, STEREO, NEURAL, NEURAL_ASSISTED_STEREO, TOF, GPU_STEREO.\n\n"
223        "Configure the node via CLI to exercise Depth::build() overloads (fps-only, algorithm + fps/res, "
224        "algorithm + config) or pre-built user stereo cameras.");
225    program.add_argument("--ip").default_value(std::string("")).help("Device IP for TCP/IP (e.g. PoE)");
226    program.add_argument("--fps").scan<'g', float>().help("Stereo camera FPS for Depth.build() or user cameras");
227    program.add_argument("--width").scan<'u', uint32_t>().help("Stereo frame width (requires --height)");
228    program.add_argument("--height").scan<'u', uint32_t>().help("Stereo frame height (requires --width)");
229    program.add_argument("--algorithm").help("Depth backend: auto, stereo, neural, neural_assisted_stereo, tof, gpu_stereo");
230    program.add_argument("--config").help("DeviceModelZoo (NEURAL_*) or StereoDepth.PresetMode (DEFAULT, ...)");
231    program.add_argument("--user-cameras").flag().help("Pre-build stereo Camera nodes instead of using Depth.build() for fps/res");
232
233    try {
234        program.parse_args(argc, argv);
235    } catch(const std::runtime_error& err) {
236        std::cerr << err.what() << '\n';
237        std::cerr << program;
238        return EXIT_FAILURE;
239    }
240
241    CliOptions options{};
242    options.ip = program.get<std::string>("--ip");
243    if(program.is_used("--fps")) {
244        options.fps = program.get<float>("--fps");
245    }
246    if(program.is_used("--width")) {
247        options.width = program.get<uint32_t>("--width");
248    }
249    if(program.is_used("--height")) {
250        options.height = program.get<uint32_t>("--height");
251    }
252    if(program.is_used("--algorithm")) {
253        options.algorithm = program.get<std::string>("--algorithm");
254    }
255    if(program.is_used("--config")) {
256        options.config = program.get<std::string>("--config");
257    }
258    options.userCameras = program.get<bool>("--user-cameras");
259
260    if(options.config && !options.algorithm) {
261        std::cerr << "--config requires --algorithm\n";
262        return EXIT_FAILURE;
263    }
264
265    try {
266        sizeFromOptions(options);
267    } catch(const std::runtime_error& err) {
268        std::cerr << err.what() << '\n';
269        return EXIT_FAILURE;
270    }
271
272    try {
273        dai::Pipeline pipeline = makePipeline(options);
274
275        if(options.userCameras) {
276            buildUserStereoCameras(pipeline, options);
277        }
278
279        auto depthNode = pipeline.create<dai::node::Depth>();
280        configureDepth(depthNode, options);
281
282        auto depthQueue = depthNode->depth().createOutputQueue();
283        auto confidenceQueue = depthNode->confidence().createOutputQueue();
284
285        pipeline.build();
286
287        std::cout << "Resolved algorithm: " << algorithmName(depthNode->getResolvedAlgorithm()) << std::endl;
288        std::cout << "Resolved config: ";
289        printResolvedConfig(depthNode->getResolvedConfig());
290
291        pipeline.start();
292
293        while(pipeline.isRunning()) {
294            auto depthFrame = depthQueue->get<dai::ImgFrame>();
295            auto confidenceFrame = confidenceQueue->get<dai::ImgFrame>();
296
297            if(depthFrame != nullptr) {
298                cv::imshow("depth", dai::utility::colorizeDepthFrame(*depthFrame).getCvFrame());
299            }
300            if(confidenceFrame != nullptr) {
301                cv::imshow("confidence", colorizeConfidence(confidenceFrame->getFrame()));
302            }
303
304            if(cv::waitKey(1) == 'q') {
305                pipeline.stop();
306                break;
307            }
308        }
309
310        cv::destroyAllWindows();
311    } catch(const std::exception& ex) {
312        std::cerr << ex.what() << '\n';
313        return EXIT_FAILURE;
314    }
315
316    return EXIT_SUCCESS;
317}

类似示例

需要帮助?

请访问讨论论坛以获取技术支持或您可能有的任何其他问题。