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

Revision history [back]

click to hide/show revision 1
initial version

You can use the following function to replace all camera info messages in a certain topic with another given camera info message:

def replace_camera_info_messages(bag, topic, camera_info):                  
    import os.path                                                          
    import rosbag                                                           
    orig = os.path.splitext(bag)[0] + '.orig.bag'                           
    os.rename(bag, orig)                                                    
    inbag = rosbag.Bag(orig, 'r')                                           
    outbag = rosbag.Bag(bag, 'w')                                           
    for t, msg, ts in inbag.read_messages():                                
        if t == topic:                                                      
            camera_info.header.seq = msg.header.seq                         
            camera_info.header.stamp = msg.header.stamp                     
            outbag.write(t, camera_info, ts)                                
        else:                                                               
            outbag.write(t, msg, ts)                                        
    inbag.close()                                                           
    outbag.close()