如何从多台Intel RealSense D435i相机的帧中获取对应相机序列号
RealSense多相机帧区分及序列号获取方案
1 原有代码问题修正
你当前的配置逻辑存在错误:单个rs.pipeline实例仅能绑定一台Intel RealSense设备,对同一个config对象重复调用enable_device不会让单pipeline同时读取两台设备的数据流,你之前获取的两组帧实际为同一台设备的重复采集结果,并非两台设备分别输出的图像。
正确配置逻辑为每台设备单独初始化独立的pipeline与config对象,示例如下:
import pyrealsense2 as rs import numpy as np import cv2 # 设备序列号列表 serial_list = ['938422072752', '902512070386'] pipelines = {} configs = {} # 为每台设备单独初始化配置 for serial in serial_list: pipeline = rs.pipeline() config = rs.config() config.enable_device(serial) 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.start(config) pipelines[serial] = pipeline
2 从帧对象获取所属设备序列号的方法
不管是复合帧frameset对象,还是单独的深度帧、彩色帧对象,都可以通过以下链路查询所属设备的序列号:
- 调用帧对象的
get_profile()方法获取流配置对象 - 调用流配置对象的
device()方法获取对应的设备实例 - 调用设备实例的
get_info(rs.camera_info.serial_number)方法即可得到序列号
示例代码片段:
# 从wait_for_frames返回的复合帧直接查询 frames = pipeline.wait_for_frames() serial = frames.get_profile().device().get_info(rs.camera_info.serial_number) # 也可从单独的深度/彩色帧查询,结果完全一致 depth_frame = frames.get_depth_frame() depth_serial = depth_frame.get_profile().device().get_info(rs.camera_info.serial_number) color_frame = frames.get_color_frame() color_serial = color_frame.get_profile().device().get_info(rs.camera_info.serial_number)
3 完整可运行参考代码
import pyrealsense2 as rs import numpy as np import cv2 # 填写你自己的两台设备序列号 serials = ['938422072752', '902512070386'] pipeline_dict = {} # 初始化所有设备的pipeline for s in serials: pipe = rs.pipeline() cfg = rs.config() cfg.enable_device(s) cfg.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30) cfg.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30) pipe.start(cfg) pipeline_dict[s] = pipe try: while True: all_imgs = [] for serial, pipe in pipeline_dict.items(): frames = pipe.wait_for_frames() depth_frame = frames.get_depth_frame() color_frame = frames.get_color_frame() if not depth_frame or not color_frame: continue # 转numpy数组 depth_img = np.asanyarray(depth_frame.get_data()) color_img = np.asanyarray(color_frame.get_data()) depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_img, alpha=0.5), cv2.COLORMAP_JET) # 可以在图上打印序列号区分 cv2.putText(color_img, f"SN:{serial}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2) all_imgs.extend([color_img, depth_colormap]) # 拼接所有图像 combine_img = np.hstack(all_imgs) cv2.imshow("Multi RealSense", combine_img) if cv2.waitKey(1) & 0xFF == ord('q'): break finally: # 停止所有pipeline for pipe in pipeline_dict.values(): pipe.stop() cv2.destroyAllWindows()
内容的提问来源于stack exchange,提问作者Nitish Latkar
相关产品推荐
相关产品推荐

