1
$\begingroup$

Rosanswers logo

How to extract data from *.bag?

I recorded my odomerty and laser data using rosbag. I want to extract (specific) data stream(text format) from recorded bagfiles. (time, step, odometry[x,y,...], laser data, ...) Becuase my unfinshied project are programed by matlab.

Question.

  1. How to extract data from *.bag? (using matlab, C/C++ or ROS API...)

Originally posted by Changhyun Jun on ROS Answers with karma: 231 on 2011-02-16

Post score: 23


Original comments

Comment by mmwise on 2011-02-17:
when you see an answer you like, mark it as an accepted answer

Comment by RB on 2013-10-27:
Ok, is it possible to extract the contents of the bag file time, step, odometry[x,y..] and write our own customize code in cpp or java to move the robot through particular location of the map. Does we need to create the package with our customize code? How to combine all in built packages of ros and our own package?

Comment by mariakatosvich on 2016-09-09:
The script worked well for me, but I didn't test for a bunch of different bags. I'd imagine it wouldn't work if the bag contains data of a type that is not just a simple number

Comment by Ayush Sharma on 2017-03-12:
@changhyun, did you find the relevant solution? I am also trying to retrieve the odom data in terms of each step , data and all. The below "highlighted" answer is not giving me the required output. Did you find the solution? Kindly help

$\endgroup$

7 Answers 7

1
$\begingroup$

Rosanswers logo

One more tip:

For simple message formats, you can convert directly to CSV from the command line:

rostopic echo -b file.bag -p /topic

This won't work well if your bag file contains arrays/images/point clouds/etc... as it renders the entire message to text.


Originally posted by kwc with karma: 12244 on 2011-02-17

This answer was ACCEPTED on the original site

Post score: 34


Original comments

Comment by AndyZe on 2015-08-22:
This is the best answer I've found. Add a carat to save the text from terminal to txt--

rostopic echo -b file.bag -p /topic > data.txt

Comment by pkpp1233 on 2016-10-16:
How might this work if you're trying to extract both structured data into a CSV and images at the same time?

Comment by Ayush Sharma on 2017-03-12:
I am getting a blank txt file after implementing the following command "rostopic echo -b file.bag -p /topic > data.txt" and i am not able to get any output with the command "rostopic echo -b file.bag -p /topic".

Comment by jescasany on 2017-03-13:
Neither I get anything from those commands

Comment by Ayush Sharma on 2017-03-13:
Right @jescasany. Though i can view the "bag" data on "rqt", still the above commands are showing no output. Kindly check my question related to this.

Comment by mariakatosvich on 2017-03-14:
I tried the "rostopic echo -b file.bag -p /topic > data.txt" but I am getting a blank txt file " and i am not able to observe any output with the command "rostopic echo -b file.bag -p /topic.

Comment by Ayush Sharma on 2017-03-14:
If this solution has been highlighted as a correct answer, then there must a way to resolve this issue. Probably the solution provider can help u sin this regard. But i was able to view the bag data via the "rqt" command. Use the commands "rqt" and open the bag file, you will get the whole data.

Comment by mike.min on 2018-01-17:
Thanks, " rostopic echo -b file.bag -p /topic > data.txt " works for me. Do you know where i can find the command for more about the command. For example, could i add other suffix instead of -p or -b?

Comment by AndyZe on 2018-01-17:
Yep, just do rostopic echo -h

Comment by godgang4885 on 2019-01-23:
just do not write /topic down. should write like /camera/camera_raw

$\endgroup$
0
$\begingroup$

Rosanswers logo

Edit, because the answer by Lorenz is better than my previous suggestion: If you want to parse a bagfile yourself this might be interesting: http://www.ros.org/wiki/Bags/Format/2.0

Btw: It would be really cool if there was some kind of converter from bag-files to yaml or xml files.


Originally posted by Felix Endres with karma: 6468 on 2011-02-16

This answer was NOT ACCEPTED on the original site

Post score: 1

$\endgroup$
0
$\begingroup$

Rosanswers logo

You easily can use the rosbag C++ or python API to directly load data from the bag. You find a tiny example on the rosbag C++ API documentation:

http://www.ros.org/doc/api/rosbag/html/c++/


Originally posted by Lorenz with karma: 22731 on 2011-02-16

This answer was NOT ACCEPTED on the original site

Post score: 12

$\endgroup$
0
$\begingroup$

Rosanswers logo

I have written a really simple script to output ros topics to a text file. you can find the script here; https://gist.github.com/831611

for example, we need the timestamp, width and height of sensor_msgs::PointCloud2;

$ ./topics_to_ssv -d " " hoge.ssv /camera/depth/points2/header/stamp/secs /camera/depth/points2/header/stamp/nsecs /camera/depth/points2/width /camera/depth/points2/height
$ cat hoge.ssv
/camera/depth/points2/header/stamp/secs /camera/depth/points2/header/stamp/nsecs /camera/depth/points2/width /camera/depth/points2/height 
1297944909 267601871 640 480 
1297944909 310369177 640 480 
...

I hope it help you...


Originally posted by Ryohei Ueda with karma: 317 on 2011-02-16

This answer was NOT ACCEPTED on the original site

Post score: 5

$\endgroup$
0
$\begingroup$

Rosanswers logo

Not sure if this will be useful for your specific case, but in one of the packages in starmac-ros-pkg there is a tool called bag2mat that reads in bag files for use in Python and can also save to a binary .mat file:

http://ros.org/doc/fuerte/api/starmac_tools/html/bag2mat_8py_source.html


Originally posted by Patrick Bouffard with karma: 2264 on 2011-02-17

This answer was NOT ACCEPTED on the original site

Post score: 1

$\endgroup$
0
$\begingroup$

Rosanswers logo

I could not find an easy way to save all topics to a csv file for post-processing, so I wrote a python script to do it. I hope others will find it useful:

bag2csv.py

Usage1 (for one bag file):

python bag2csv.py filename.bag

Usage 2 (for all bag files in current directory):

python bag2csv.py

I wasn't sure if this would be the right place to share this script for others to use. Can anybody else please advise?

Please let me know if you find this useful, or if you have any suggestions for how to improve it.


Originally posted by NickSpeal with karma: 81 on 2013-05-29

This answer was NOT ACCEPTED on the original site

Post score: 6


Original comments

Comment by atcortes on 2014-04-04:
Hi! I tried to use your script, but I got the following error: File "bag2csv.py", line 94, in values.append(pair[1]) IndexError: list index out of range Did anyone faced this problem before?

Comment by NickSpeal on 2014-04-06:
Hi atcortes. I'm sorry but I'm not sure what the solution to your problem is. I suspect it could be a problem with the bag file. Somebody else emailed me with the same problem, but didn't get back to me with more details about their situation. Do you get this problem with all bag files?

Comment by atcortes on 2014-06-09:
Hi NickSpeal, sorry for the late answer. This problem happens with all my bag files. A folder is created with the bag file and a csv file, however this one is not completed and I think the bag file corrupted (I cannot open it)

Comment by NickSpeal on 2014-06-15:
I'm not sure the solution to your problem. The script worked well for me, but I didn't test for a bunch of different bags. I'd imagine it wouldn't work if the bag contains data of a type that is not just a simple number. Camera images or point clouds, etc, would not work properly in .csv format...

Comment by Miguel Prada on 2014-12-16:
FYI, I just got to try your script since I was trying to make something similar, and I'm getting similar errors to the ones reported by atcortes. I found out that in my case these were due to '/rosout' messages containing the '\n' inside the message string, causing 'nameValuePair's with no ':'.

Comment by georgebrindeiro on 2017-03-12:
I used it and it was sweet

Comment by k2108 on 2021-09-10:
I found the mention of this script here : https://www.clearpathrobotics.com/assets/guides/kinetic/ros/Converting%20ROS%20bag%20to%20CSV.html

and was able to use it perfectly using : python bag2csv.py filename.bag

$\endgroup$
0
$\begingroup$

Rosanswers logo

just for the fun of it, here is a bag2csv one-liner for bash

for topic in `rostopic list -b bagfile.bag` ; do rostopic echo -p -b bagfile.bag $topic >bagfile-${topic//\//_}.csv ; done

Originally posted by x75 with karma: 161 on 2014-02-03

This answer was NOT ACCEPTED on the original site

Post score: 12


Original comments

Comment by kleinsplash on 2014-02-09:
I must be doing something wrong - I cant find where the file is saved? or is it just an oversight of mine that its not saving?

Comment by NickSpeal on 2014-06-15:
Nice! That's a lot quicker than my implementation...

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.