ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

How to get number of messages in one topic from

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

odelay gravatar image


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

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:


You can also filter for a specific topic name


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


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

Question Tools

1 follower


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

Seen: 7,583 times

Last updated: Apr 04 '16