使用Intel RealSense回放bag文件时遇RuntimeError问题求助
解决RealSense Bag回放"Couldn't resolve requests"错误
这个错误的核心原因是你代码中手动指定的深度/彩色流参数(分辨率、格式、帧率)与bag文件实际录制的流参数不匹配,导致pipeline无法解析请求。
解决方案1:自动加载bag流配置(推荐)
直接删除代码中手动指定流的两行,让config从bag文件自动读取所有流参数,避免参数冲突:
修改后的完整代码:
import pyrealsense2 as rs import numpy as np import cv2 # Path to your .bag file (downloaded dataset or recorded yourself) bag_file = "stairs.bag" # Configure pipeline for playback pipeline = rs.pipeline() config = rs.config() config.enable_device_from_file(bag_file, repeat_playback=False) # False = stop when file ends # 移除手动指定流的代码,让config自动加载bag中的流配置 # Start pipeline pipeline.start(config) try: while True: frames = pipeline.wait_for_frames() depth_frame = frames.get_depth_frame() color_frame = frames.get_color_frame() if not depth_frame or not color_frame: break # Convert images to numpy arrays depth_image = np.asanyarray(depth_frame.get_data()) color_image = np.asanyarray(color_frame.get_data()) # Colorize depth for visualization depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET) # 调整深度图尺寸与彩色图一致,避免hstack报错 depth_colormap = cv2.resize(depth_colormap, (color_image.shape[1], color_image.shape[0])) # Show both depth and color images = np.hstack((color_image, depth_colormap)) cv2.imshow("RealSense Playback", images) # Exit on ESC key if cv2.waitKey(1) == 27: break finally: pipeline.stop() cv2.destroyAllWindows()
解决方案2:匹配bag实际流参数
如果你需要手动指定流参数,先运行以下代码获取bag文件的实际流配置:
import pyrealsense2 as rs bag_file = "stairs.bag" ctx = rs.context() device = ctx.load_device(bag_file) # 遍历所有传感器和流配置 for sensor in device.query_sensors(): for profile in sensor.get_stream_profiles(): if profile.is_video_stream(): video_profile = profile.as_video_stream_profile() print(f"流类型: {profile.stream_type()}, 格式: {profile.format()}, 分辨率: {video_profile.width()}x{video_profile.height()}, 帧率: {video_profile.fps()}")
运行后会输出类似这样的信息:
流类型: stream.depth, 格式: format.z16, 分辨率: 1280x720, 帧率: 30 流类型: stream.color, 格式: format.bgr8, 分辨率: 1920x1080, 帧率: 30
然后把原代码中的enable_stream参数替换为上述输出的实际值即可。
内容的提问来源于stack exchange,提问作者Freeman
相关产品推荐
相关产品推荐

