Algorithm.AUTO) 下,该节点会为您的设备选择最佳的深度后端——RVC4 上的 NeuralDepth,其他设备上的 ToF 或 StereoDepth——并可视化 depth 和 confidence 输出。使用 CLI 标志来执行 build() 重载:仅 FPS、算法 + FPS/分辨率,或固定算法 + 配置对。此示例需要 DepthAI v3 API,请参阅 安装说明。演示内容
- 使用
AUTO后端选择创建Depth节点 - 连接后读取
getResolvedAlgorithm()和getResolvedConfig() - 可选的
--fps、--width/--height、--algorithm和--configCLI 配置 - 可选的
--user-cameras用于预构建 Depth 重用的立体相机节点
源代码
Python
PythonGitHub
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())类似示例
需要帮助?
请访问讨论论坛以获取技术支持或您可能有的任何其他问题。