如何用Python的pyrealsense2配置Intel Realsense D405高级深度可视化参数并导入JSON设置
在Python中使用pyrealsense2导入Realsense D405的JSON配置
直接通过pyrealsense2的高级模式API就能加载你从realsense-viewer导出的JSON配置,步骤和代码示例如下:
步骤1:确保依赖安装
先确认已安装pyrealsense2库:
pip install pyrealsense2
如果要做可视化显示,还需要安装opencv-python和numpy:
pip install opencv-python numpy
步骤2:加载JSON配置并启动相机
核心逻辑是通过rs400_advanced_mode接口加载配置,重启pipeline后生效:
import pyrealsense2 as rs import cv2 import numpy as np # 初始化pipeline和配置对象 pipeline = rs.pipeline() config = rs.config() # 临时启动pipeline获取设备实例 temp_profile = pipeline.start(config) device = temp_profile.get_device() # 启用相机的高级模式并加载JSON配置 adv_mode = rs.rs400_advanced_mode(device) # 读取你导出的JSON配置文件内容 with open("d405_config.json", "r") as f: config_json = f.read() adv_mode.load_json(config_json) # 验证配置是否加载成功 print("高级配置加载状态:", adv_mode.is_enabled()) # 停止临时pipeline,重新启动以应用新配置 pipeline.stop() # 配置需要的流(深度流,可按需添加彩色流) config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30) # 如果需要彩色流,取消下面注释 # config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30) # 启动pipeline开始获取帧 profile = pipeline.start(config) # 深度可视化循环 try: while True: frames = pipeline.wait_for_frames() depth_frame = frames.get_depth_frame() if not depth_frame: continue # 使用realsense内置的colorizer将深度图转为彩色可视化图 depth_colorizer = rs.colorizer() depth_color_frame = depth_colorizer.colorize(depth_frame) # 转为numpy数组并显示 depth_color_image = np.asanyarray(depth_color_frame.get_data()) cv2.imshow('D405 Depth Visualization', depth_color_image) # 按q退出 if cv2.waitKey(1) & 0xFF == ord('q'): break finally: # 清理资源 pipeline.stop() cv2.destroyAllWindows()
关键注意事项
- 导出的JSON配置必须是对应D405相机的,realsense-viewer导出时确保当前连接的是D405
- 加载配置后必须重启pipeline,否则新参数不会生效
rs400_advanced_mode适用于包括D405在内的所有RS400系列相机- 如果你的JSON配置中包含了流分辨率、帧率的设置,代码中
config.enable_stream的参数要和配置保持一致,避免冲突
内容的提问来源于stack exchange,提问作者arnecools
相关产品推荐
相关产品推荐

