Ask Your Question
1

How to get number of messages in one topic from rosbag.py

asked 2014-10-24 03:28:21 -0500

odelay gravatar image

Hello!

I was wondering whether with the Python module rosbag there is any chance to get the number of recorded messages in one topic. The Bag.read_messages method returns a generator and of course I could iterate over all messages and count. But is there a way to tell the number in advance? That way I could preallocate the right amount of lists to handle the messages.

Thank you!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
4

answered 2016-04-04 03:55:49 -0500

Felix Duvallet gravatar image

The rosbag python module has several ways to count the number of messages in a rosbag file. Unfortunately there doesn't seem to be a public API, you often have to just read the comments in the code.

To get the total number of messages:

bag.get_message_count()

You can also filter for a specific topic name

bag.get_message_count('/tf')

will count the number of messages on the '/tf' topic. Note this is the topic name, not the message type.


If you want slightly more information, you can use

bag.get_type_and_topic_info().topics

and you will get a whole lot more information, including the number of messages per topic, the frequency, etc...

{'/joint_states': TopicTuple(msg_type='sensor_msgs/JointState', message_count=2500, connections=1, frequency=500.99187768752984),
 '/rosout': TopicTuple(msg_type='rosgraph_msgs/Log', message_count=35, connections=1, frequency=67108.864),
 '/tf': TopicTuple(msg_type='tf2_msgs/TFMessage', message_count=7697, connections=1, frequency=32768.0)}
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2014-10-24 03:28:21 -0500

Seen: 3,434 times

Last updated: Apr 04 '16