def save_camera_data_to_files(device_id=0, rgb: bool = True, depth: bool = True, ir: bool = False, frames: int = 150, outfile_pattern: str = "output"): ## start the service - also available as context manager with pyrs.Service() as serv: streams = [] rgb_writer, depth_writer, ir_writer = None, None, None fourcc = cv2.VideoWriter_fourcc(*'mp4v') if rgb: rgb_stream = ColorStream() streams.append(rgb_stream) rgb_writer = cv2.VideoWriter(f'{outfile_pattern}_rgb.mp4', fourcc, 30, (rgb_stream.width, rgb_stream.height)) if depth: depth_stream = DepthStream() dac_stream = DACStream() streams.append(depth_stream) streams.append(dac_stream) depth_writer = cv2.VideoWriter( f'{outfile_pattern}_depth.mp4', fourcc, 30, (depth_stream.width, depth_stream.height)) if ir: ir_stream = InfraredStream() streams.append(ir_stream) ir_writer = cv2.VideoWriter(f'{outfile_pattern}_ir.mp4', fourcc, 30, (ir_stream.width, ir_stream.height), False) with serv.Device(device_id=device_id, streams=streams) as cam: for _ in range(frames): cam.wait_for_frames() if rgb_writer: frame = cv2.cvtColor(cam.color, cv2.COLOR_RGB2BGR) rgb_writer.write(frame) if depth_writer: dac = convert_z16_to_bgr(cam.dac) depth_writer.write(dac) if ir_writer: ir_writer.write(cv2.GaussianBlur(cam.infrared, (5, 5), 0)) for writer in [rgb_writer, depth_writer, ir_writer]: if writer: writer.release() cv2.destroyAllWindows()
def _initialize_device( self, device_id, color_frame_size, color_fps, depth_frame_size, depth_fps, device_options=(), ): devices = tuple(self.service.get_devices()) color_frame_size = tuple(color_frame_size) depth_frame_size = tuple(depth_frame_size) self.streams = [ColorStream(), DepthStream(), PointStream()] self.last_color_frame_ts = None self.last_depth_frame_ts = None self._recent_frame = None self._recent_depth_frame = None if not devices: if not self._needs_restart: logger.error( "Camera failed to initialize. No cameras connected.") self.device = None self.update_menu() return if self.device is not None: self.device.stop() # only call Device.stop() if its context if device_id >= len(devices): logger.error( "Camera with id {} not found. Initializing default camera.". format(device_id)) device_id = 0 # use default streams to filter modes by rs_stream and rs_format self._available_modes = self._enumerate_formats(device_id) # make sure that given frame sizes and rates are available color_modes = self._available_modes[rs_stream.RS_STREAM_COLOR] if color_frame_size not in color_modes: # automatically select highest resolution color_frame_size = sorted(color_modes.keys(), reverse=True)[0] if color_fps not in color_modes[color_frame_size]: # automatically select highest frame rate color_fps = color_modes[color_frame_size][0] depth_modes = self._available_modes[rs_stream.RS_STREAM_DEPTH] if self.align_streams: depth_frame_size = color_frame_size else: if depth_frame_size not in depth_modes: # automatically select highest resolution depth_frame_size = sorted(depth_modes.keys(), reverse=True)[0] if depth_fps not in depth_modes[depth_frame_size]: # automatically select highest frame rate depth_fps = depth_modes[depth_frame_size][0] colorstream = ColorStream( width=color_frame_size[0], height=color_frame_size[1], fps=color_fps, color_format="yuv", preset=self.stream_preset, ) depthstream = DepthStream( width=depth_frame_size[0], height=depth_frame_size[1], fps=depth_fps, preset=self.stream_preset, ) pointstream = PointStream(width=depth_frame_size[0], height=depth_frame_size[1], fps=depth_fps) self.streams = [colorstream, depthstream, pointstream] if self.align_streams: dacstream = DACStream(width=depth_frame_size[0], height=depth_frame_size[1], fps=depth_fps) dacstream.name = "depth" # rename data accessor self.streams.append(dacstream) # update with correctly initialized streams # always initiliazes color + depth, adds rectified/aligned versions as necessary self.device = self.service.Device(device_id, streams=self.streams) self.controls = Realsense_Controls(self.device, device_options) self._intrinsics = load_intrinsics(self.g_pool.user_dir, self.name, self.frame_size) self.update_menu() self._needs_restart = False