Python读取rosbag数据

zxl19 2021-10-30

使用Python读取rosbag数据。

获取rosbag摘要信息

方法1

import subprocess
import yaml

bagfile = 'input.bag'
info_dict = yaml.load(subprocess.Popen(['rosbag', 'info', '--yaml', bagfile], stdout=subprocess.PIPE).communicate()[0])
duration = info_dict['duration']
start_time = info_dict['start']

方法2

import yaml
from rosbag.bag import Bag

bagfile = 'input.bag'
info_dict = yaml.load(Bag(bagfile, 'r')._get_yaml_info())
duration = info_dict['duration']
start_time = info_dict['start']

获取rosbag消息信息

import rosbag

bag = rosbag.Bag('test.bag')
for topic, msg, t in bag.read_messages(topics=['chatter', 'numbers']):
# for topic, msg, t in bag.read_messages(): # 也可,遍历所有话题
    print(msg)
bag.close()

参考

  1. rosbag/Cookbook-ROS Wiki
  2. rosbag/Code API-ROS Wiki