import argparse
import pyrealsense2 as rs
import numpy as np
import cv2
import os
"""
this is a file for convert .bag files to .png files for RealSense RGB-D data
"""
# every n_skip, save one image
n_skip = 10
def main():
if not os.path.exists(args.depth_directory):
os.mkdir(args.depth_directory)
if not os.path.exists(args.color_directory):
os.mkdir(args.color_directory)
try:
pipeline = rs.pipeline()
config = rs.config()
rs.config.enable_device_from_file(config, args.input, repeat_playback=False)
config.enable_stream(rs.stream.depth, 848, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.rgb8, 30)
pipeline.start(config)
rs.align(rs.stream.color)
# start to convert
i = 0
while i <= 300: # fps = 30, usually, each video lasts around 10 sec.
frames = pipeline.wait_for_frames()
print("num", frames.frame_number)
if i % n_skip == 0:
print("Saving frame:", i)
# saving depths
depth_frame = frames.get_depth_frame()
depth_image = np.asanyarray(depth_frame.get_data())
cv2.imwrite(args.depth_directory + "/" + str(i).zfill(6) + ".png", depth_image)
# saving RGBs
color_frame = frames.get_color_frame()
color_image = np.asanyarray(color_frame.get_data())
print(color_image.shape)
cv2.imwrite(args.color_directory + "/" + str(i).zfill(6) + ".png", color_image)
i += 1
finally:
pass
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-d_d", "--depth_directory", type=str, help="Path to save the depth images")
parser.add_argument("-c_d", "--color_directory", type=str, help="Path to save the RGB images")
parser.add_argument("-i", "--input", type=str, help="Bag file to read")
args = parser.parse_args()
main()