realsense bag文件时间戳获取的方式有两种:
(1)使用ros读取bag文件,ROS支持python和c++,这里使用的是python语言
import rosbag
filename = r'D:\datasets\D415\static_20210914\20210913_165702.bag'
bag = rosbag.Bag(filename, 'r')
info = bag.get_type_and_topic_info()
print(info)
cnt_depth = info.topics.get('/device_0/sensor_0/Depth_0/image/data').message_count
print('深度图数量是:', cnt_depth)
cnt_color = info.topics.get('/device_0/sensor_1/Color_0/image/data').message_count
print('彩色图数量是:', cnt_color)
bag_data = bag.read_messages('/device_0/sensor_0/Depth_0/image/data')
t_tmp = 0
t = 0
for topic, msg, t in bag_data:
print(msg.header.stamp.to_sec(), t.to_nsec() - t_tmp)
t_tmp = t.to_nsec()
(2)使用pyrealsense2,也可以c++版本的librealsense
#####################################################
## Read bag from file ##
#####################################################
# First import library
import pyrealsense2 as rs
# Import Numpy for easy array manipulation
import numpy as np
# Import OpenCV for easy image rendering
import cv2
# Import argparse for command-line options
import argparse
# Import os.path for file path manipulation
import os.path
# Create object for parsing command-line options
parser = argparse.ArgumentParser(description="Read recorded bag file and display depth stream in jet colormap.\
Remember to change the stream fps and format to match the recorded.")
# Add argument which takes path to a bag file as an input
parser.add_argument("-i", "--input", type=str, help="Path to the bag file")
# Parse the command line arguments to an object
args = parser.parse_args()
args.input = r'D:\datasets\20210824\1\20210824_103849.bag'
# Safety if no parameter have been given
if not args.input:
print("No input paramater have been given.")
print("For help type --help")
exit()
# Check if the given file have bag extension
if os.path.splitext(args.input)[1] != ".bag":
print("The given file is not of correct file format.")
print("Only .bag files are accepted")
exit()
try:
# Create pipeline
pipeline = rs.pipeline()
# Create a config object
config = rs.config()
# Tell config that we will use a recorded device from file to be used by the pipeline through playback.
rs.config.enable_device_from_file(config, args.input, repeat_playback=False)
# Configure the pipeline to stream the depth stream
# Change this parameters according to the recorded bag file resolution
config.enable_stream(rs.stream.depth, rs.format.z16, 30)
# Start streaming from file
pipeline.start(config)
# whq
device = pipeline.get_active_profile().get_device()
playback = device.as_playback()
playback.set_real_time(False)
# Create opencv window to render image in
cv2.namedWindow("Depth Stream", cv2.WINDOW_AUTOSIZE)
# Create colorizer object
colorizer = rs.colorizer()
number = 0
timestamp_tmp = 0
# Streaming loop
while True:
# Get frameset of depth
frames = pipeline.wait_for_frames()
number = number + 1
print(number, ' ', frames.timestamp, frames.timestamp - timestamp_tmp)
timestamp_tmp = frames.timestamp
# Get depth frame
depth_frame = frames.get_depth_frame()
# Colorize depth frame to jet colormap
depth_color_frame = colorizer.colorize(depth_frame)
# Convert depth_frame to numpy array to render image in opencv
depth_color_image = np.asanyarray(depth_color_frame.get_data())
# Render image in opencv window
cv2.imshow("Depth Stream", depth_color_image)
key = cv2.waitKey(1)
# if pressed escape exit program
# if key == 27:
# cv2.destroyAllWindows()
# break
except RuntimeError:
print(number)
print("There are no more frames left in the .bag file!")
cv2.destroyAllWindows()
finally:
pass
参考文献:
(1)ROSBAG:用python按时间戳提取bag中的图像