源代码
Python
PythonGitHub
1#!/usr/bin/env python3
2"""
3PointCloud Visualizer
4
5Live 3D visualization of stereo-depth point clouds using Open3D.
6Press Q in the Open3D window (or Ctrl-C in the terminal) to quit.
7"""
8
9import time
10
11import cv2
12import numpy as np
13
14try:
15 import open3d as o3d
16except ImportError:
17 raise SystemExit("Please install open3d: pip install open3d")
18
19import depthai as dai
20
21
22def main() -> None:
23 print("PointCloud Visualizer")
24 print("=====================")
25 print("Connecting to device...")
26
27 device = dai.Device()
28 print(f"Device: {device.getDeviceName()} (ID: {device.getDeviceId()})\n")
29
30 with dai.Pipeline(device) as pipeline:
31 # ── Camera + Depth ─────────────────────────────────────
32 colorSockets = device.getConnectedCameras(dai.CameraSensorType.COLOR)
33 colorSocket = colorSockets[0] if colorSockets else dai.CameraBoardSocket.CAM_A
34 color = pipeline.create(dai.node.Camera).build(colorSocket)
35 colorOut = color.requestOutput(
36 (640, 400), type=dai.ImgFrame.Type.RGB888i,
37 resizeMode=dai.ImgResizeMode.CROP, enableUndistortion=True,
38 )
39
40 depth = pipeline.create(dai.node.Depth).build(dai.node.Depth.Algorithm.AUTO, None, (640, 400))
41 depth.setAlignTo(colorOut)
42
43 # ── PointCloud node ───────────────────────────────────────────
44 pc = pipeline.create(dai.node.PointCloud)
45 pc.setRunOnHost(True)
46 pc.initialConfig.setLengthUnit(dai.LengthUnit.METER)
47 depth.depth.link(pc.inputDepth)
48 colorOut.link(pc.inputColor)
49
50 queue = pc.outputPointCloud.createOutputQueue(maxSize=4, blocking=False)
51 qDepth = pc.passthroughDepth.createOutputQueue(maxSize=4, blocking=False)
52
53 # ── Open3D setup ──────────────────────────────────────────────
54 vis = o3d.visualization.Visualizer()
55 vis.create_window("PointCloud Visualizer")
56 pcd = o3d.geometry.PointCloud()
57 coord = o3d.geometry.TriangleMesh.create_coordinate_frame(size=0.1)
58 vis.add_geometry(coord)
59 first = True
60
61 # ── Start pipeline & wait for auto-exposure ───────────────────
62 pipeline.start()
63 print("Waiting for auto-exposure to settle...")
64 time.sleep(1)
65 queue.tryGetAll() # drain stale frames
66 qDepth.tryGetAll()
67
68 print("Streaming... Press Q in the Open3D window to quit.")
69
70 try:
71 while True:
72 pclData = queue.tryGet()
73
74 if pclData is not None:
75 if pclData.isColor():
76 points, colors = pclData.getPointsRGB()
77 if len(points) > 0:
78 pcd.points = o3d.utility.Vector3dVector(points.astype(np.float64))
79 pcd.colors = o3d.utility.Vector3dVector(
80 np.delete(colors / 255.0, 3, 1).astype(np.float64)
81 )
82 else:
83 points = pclData.getPoints()
84 if len(points) > 0:
85 pcd.points = o3d.utility.Vector3dVector(points)
86 pcd.colors = o3d.utility.Vector3dVector()
87
88 if len(pcd.points) > 0:
89 if first:
90 vis.add_geometry(pcd)
91 ctr = vis.get_view_control()
92 ctr.set_front([0, 0, -1])
93 ctr.set_up([0, -1, 0])
94 ctr.set_lookat([0, 0, 1])
95 ctr.set_zoom(0.3)
96 first = False
97 else:
98 vis.update_geometry(pcd)
99
100 # Show colorized depth in an OpenCV window
101 depthMsg = qDepth.tryGet()
102 if depthMsg is not None:
103 cv2.imshow("Depth", dai.utility.colorizeDepthFrame(depthMsg, 300, 12000, cv2.COLORMAP_HOT, useLog=True).getCvFrame())
104
105 if cv2.waitKey(1) == ord("q"):
106 break
107 if not vis.poll_events():
108 break
109 vis.update_renderer()
110
111 except KeyboardInterrupt:
112 print("\nStopping...")
113
114 vis.destroy_window()
115 cv2.destroyAllWindows()
116
117 print("Done.")
118
119
120if __name__ == "__main__":
121 main()需要帮助?
请前往 OAKChina 官网 获取技术支持或解答您的任何疑问。