Python 如何在列表中保存Intel Realsense映像(pyrealsense2)

Python 如何在列表中保存Intel Realsense映像(pyrealsense2),python,intel,realsense,Python,Intel,Realsense,我正在尝试将Intel Realsense D435i相机的深度图像和彩色图像保存在300幅图像的列表中。然后我将使用多处理将这300个图像块保存到磁盘上。但每次我尝试时,程序都会成功地在列表中添加15个图像,然后我会出现以下错误: Frame didn't arrived within 5000 我确保在Python3.6上安装了64位版本,并且在我不尝试将图像保存在列表中时,相机流非常好。真正意义上的观者也很好用。我也尝试了不同的分辨率和帧速率,但它似乎也不起作用。有趣的是,如果我

我正在尝试将Intel Realsense D435i相机的深度图像和彩色图像保存在300幅图像的列表中。然后我将使用多处理将这300个图像块保存到磁盘上。但每次我尝试时,程序都会成功地在列表中添加15个图像,然后我会出现以下错误:

    Frame didn't arrived within 5000
我确保在Python3.6上安装了64位版本,并且在我不尝试将图像保存在列表中时,相机流非常好。真正意义上的观者也很好用。我也尝试了不同的分辨率和帧速率,但它似乎也不起作用。有趣的是,如果我只保存彩色图像,我不会得到相同的错误,相反,我会在列表中一遍又一遍地得到相同的彩色图像

if __name__ == '__main__':
pipeline = rs.pipeline()
config = rs.config()

config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30)
profile = pipeline.start(config)

depth_sensor = profile.get_device().first_depth_sensor()
depth_sensor.set_option(
    rs.option.visual_preset, 3
)  # Set high accuracy for depth sensor
depth_scale = depth_sensor.get_depth_scale()

align_to = rs.stream.color
align = rs.align(align_to)

#   Init variables
im_count = 0
image_chunk = []
image_chunk2 = []
# sentinel = True
try:
    while True:

        # Wait for a coherent pair of frames: depth and color
        frames = pipeline.wait_for_frames()
        aligned_frames = align.process(frames)
        aligned_depth_frame = aligned_frames.get_depth_frame()
        color_frame = aligned_frames.get_color_frame()


        if not aligned_depth_frame or not color_frame:
            print("problem here")
            raise RuntimeError("Could not acquire depth or color frames.")

        depth_image = np.asanyarray(aligned_depth_frame.get_data())
        color_image = np.asanyarray(color_frame.get_data())

        image_chunk.append(color_image)
        image_chunk2.append(depth_image)




except Exception as e:
    print(e)

finally:
    # Stop streaming
    pipeline.stop()

我只需要它来连续保存300张图像,仅此而已,所以我很担心是什么导致了这个问题。

抓住帧会锁定内存,最终它会达到限制,从而阻止获取更多图像。即使您正在创建图像,数据仍然来自帧。您需要在创建图像后克隆图像,以释放到帧内存的链接

depth_image = np.asanyarray(aligned_depth_frame.get_data())
color_image = np.asanyarray(color_frame.get_data())

depth_image = depth_image.copy()
color_image = color_image.copy()

image_chunk.append(color_image)
image_chunk2.append(depth_image)
在此处阅读有关帧和内存管理的更多信息:

我创建了一个包装器类来从框架集中提取以后无法重新创建的各种元素。它有点重,但显示了一些可能对其他人有用的常见操作:

colorizer = None
align_to_depth = None
align_to_color = None
pointcloud = rs.pointcloud()

class IntelD435ImagePacket:
    """
    Class that contains image and associated processing data.
    """

    @property
    def frame_id(self):
        return self._frame_id

    @property
    def timestamp(self):
        return self._timestamp

    @property
    def image_color(self):
        return self._image_color

    @property
    def image_depth(self):
        return self._image_depth

    @property
    def image_color_aligned(self):
        return self._image_color_aligned

    @property
    def image_depth_aligned(self):
        return self._image_depth_aligned

    @property
    def image_depth_colorized(self):
        if not self._image_depth_colorized:
            self._image_depth_colorized = cv2.applyColorMap(self.image_depth, cv2.COLORMAP_JET);
        return self._image_depth_colorized

    @property
    def intrinsics(self):
        return self._intrinsics

    @property
    def pointcloud(self):
        return self._pointcloud

    @property
    def pointcloud_texture(self):
        return self._pointcloud_texture

    def _rs_intrinsics_to_opencv_matrix(self, rs_intrinsics):
        fx = rs_intrinsics.fx
        fy = rs_intrinsics.fy
        cx = rs_intrinsics.ppx
        cy = rs_intrinsics.ppy
        s = 0  # skew
        return np.array([fx, s, cx,
                         0, fy, cy,
                         0, 0, 1]).reshape(3, 3)

    def __init__(self, frame_set, frame_id=None, timestamp=None, *args, **kwargs):
        global colorizer
        if not colorizer:
            colorizer = rs.colorizer()
            colorizer.set_option(rs.option.color_scheme, 0)

        global align_to_depth
        if not align_to_depth:
            align_to_depth = rs.align(rs.stream.depth)

        global align_to_color
        if not align_to_color:
            align_to_color = rs.align(rs.stream.color)

        global pointcloud
        if not pointcloud:
             pointcloud = rs.pointcloud()

        # Get intrinsics
        profile = frame_set.get_profile()
        video_stream_profile = profile.as_video_stream_profile()
        rs_intrinsics = video_stream_profile.get_intrinsics()
        self._intrinsics = self._rs_intrinsics_to_opencv_matrix(rs_intrinsics)

        # Get pointcloud
        depth_frame = frame_set.get_depth_frame()
        color_frame = frame_set.get_color_frame()
        pointcloud.map_to(color_frame)
        points = pointcloud.calculate(depth_frame)
        vtx = np.asanyarray(points.get_vertices())
        points_arr = vtx.view(np.float32).reshape(vtx.shape + (-1,)).copy()
        self._pointcloud = points_arr

        # Get pointcloud texture mapping
        tex = np.asanyarray(points.get_texture_coordinates())
        color_map_arr = tex.view(np.float32).reshape(tex.shape + (-1,)).copy()
        self._pointcloud_texture = color_map_arr

        # Extract color image
        color_frame = frame_set.get_color_frame()
        self._image_color = np.asanyarray(color_frame.get_data()).copy()

        # Extract depth image
        depth_frame = frame_set.get_depth_frame()
        self._image_depth = np.asanyarray(depth_frame.get_data()).copy()

        # Align the color frame to depth frame and extract color image
        color_frame_aligned = align_to_depth.process(frame_set).get_color_frame()
        self._image_color_aligned = np.asanyarray(color_frame_aligned.get_data()).copy()

        # Align the depth frame to color frame and extract depth image
        depth_frame_aligned = align_to_color.process(frame_set).get_depth_frame()
        self._image_depth_aligned = np.asanyarray(depth_frame_aligned.get_data()).copy()

        self._image_depth_colorized = None
        if frame_id:
            self._frame_id = frame_id
        else:
            self._frame_id = frame_set.frame_number
        if timestamp:
            self._timestamp = timestamp
        else:
            self._timestamp = frame_set.timestamp
        self.__dict__.update(kwargs)

不知何故,
.append()
导致运行时错误。我也遇到了类似的问题。我很高兴知道我不是唯一的一个,另一个选择是将流保存到.bag文件中。我在这里遇到了类似的问题,您不能将帧放入列表中,因为它会锁定内存。从帧创建的numpy数组仍然指向帧的内存。你必须复制(即克隆)它才能断开链接。如果以任何方式(USB缓冲内存,而不是PC内存)存储帧的句柄,最终会耗尽内存。