read_bag_example.py
Go to the documentation of this file.
1 #####################################################
2 ## Read bag from file ##
3 #####################################################
4 
5 
6 # First import library
7 import pyrealsense2 as rs
8 # Import Numpy for easy array manipulation
9 import numpy as np
10 # Import OpenCV for easy image rendering
11 import cv2
12 # Import argparse for command-line options
13 import argparse
14 # Import os.path for file path manipulation
15 import os.path
16 
17 # Create object for parsing command-line options
18 parser = argparse.ArgumentParser(description="Read recorded bag file and display depth stream in jet colormap.\
19  Remember to change the stream fps and format to match the recorded.")
20 # Add argument which takes path to a bag file as an input
21 parser.add_argument("-i", "--input", type=str, help="Path to the bag file")
22 # Parse the command line arguments to an object
23 args = parser.parse_args()
24 # Safety if no parameter have been given
25 if not args.input:
26  print("No input paramater have been given.")
27  print("For help type --help")
28  exit()
29 # Check if the given file have bag extension
30 if os.path.splitext(args.input)[1] != ".bag":
31  print("The given file is not of correct file format.")
32  print("Only .bag files are accepted")
33  exit()
34 try:
35  # Create pipeline
36  pipeline = rs.pipeline()
37 
38  # Create a config object
39  config = rs.config()
40 
41  # Tell config that we will use a recorded device from file to be used by the pipeline through playback.
42  rs.config.enable_device_from_file(config, args.input)
43 
44  # Configure the pipeline to stream the depth stream
45  # Change this parameters according to the recorded bag file resolution
46  config.enable_stream(rs.stream.depth, rs.format.z16, 30)
47 
48  # Start streaming from file
49  pipeline.start(config)
50 
51  # Create opencv window to render image in
52  cv2.namedWindow("Depth Stream", cv2.WINDOW_AUTOSIZE)
53 
54  # Create colorizer object
55  colorizer = rs.colorizer()
56 
57  # Streaming loop
58  while True:
59  # Get frameset of depth
60  frames = pipeline.wait_for_frames()
61 
62  # Get depth frame
63  depth_frame = frames.get_depth_frame()
64 
65  # Colorize depth frame to jet colormap
66  depth_color_frame = colorizer.colorize(depth_frame)
67 
68  # Convert depth_frame to numpy array to render image in opencv
69  depth_color_image = np.asanyarray(depth_color_frame.get_data())
70 
71  # Render image in opencv window
72  cv2.imshow("Depth Stream", depth_color_image)
73  key = cv2.waitKey(1)
74  # if pressed escape exit program
75  if key == 27:
76  cv2.destroyAllWindows()
77  break
78 
79 finally:
80  pass
static std::string print(const transformation &tf)
static const textual_icon exit
Definition: model-views.h:254


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:39