00001
00002 """
00003 Copyright (c) 2012,
00004 Systems, Robotics and Vision Group
00005 University of the Balearican Islands
00006 All rights reserved.
00007
00008 Redistribution and use in source and binary forms, with or without
00009 modification, are permitted provided that the following conditions are met:
00010 * Redistributions of source code must retain the above copyright
00011 notice, this list of conditions and the following disclaimer.
00012 * Redistributions in binary form must reproduce the above copyright
00013 notice, this list of conditions and the following disclaimer in the
00014 documentation and/or other materials provided with the distribution.
00015 * Neither the name of Systems, Robotics and Vision Group, University of
00016 the Balearican Islands nor the names of its contributors may be used to
00017 endorse or promote products derived from this software without specific
00018 prior written permission.
00019
00020 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00021 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00024 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00027 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 """
00031
00032
00033 PKG = 'bag_tools'
00034
00035 import roslib; roslib.load_manifest(PKG)
00036 import rospy
00037 import rosbag
00038 import os
00039 import sys
00040 import argparse
00041 import yaml
00042 import sensor_msgs.msg
00043
00044 def change_camera_info(inbag,outbag,replacements):
00045 rospy.loginfo(' Processing input bagfile: %s', inbag)
00046 rospy.loginfo(' Writing to output bagfile: %s', outbag)
00047
00048 maps = {}
00049 for k, v in replacements.items():
00050 rospy.loginfo('Changing topic %s to contain following info (header will not be changed):\n%s',k,v)
00051
00052 outbag = rosbag.Bag(outbag,'w')
00053 for topic, msg, t in rosbag.Bag(inbag,'r').read_messages():
00054 if topic in replacements:
00055 new_msg = replacements[topic]
00056 new_msg.header = msg.header
00057 msg = new_msg
00058 outbag.write(topic, msg, t)
00059 rospy.loginfo('Closing output bagfile and exit...')
00060 outbag.close();
00061
00062 def replacement(replace_string):
00063 pair = replace_string.split('=', 1)
00064 if len(pair) != 2:
00065 raise argparse.ArgumentTypeError("Replace string must have the form /topic=calib_file.yaml")
00066 if pair[0][0] != '/':
00067 pair[0] = '/'+pair[0]
00068 stream = file(pair[1], 'r')
00069 calib_data = yaml.load(stream)
00070 cam_info = sensor_msgs.msg.CameraInfo()
00071 cam_info.width = calib_data['image_width']
00072 cam_info.height = calib_data['image_height']
00073 cam_info.K = calib_data['camera_matrix']['data']
00074 cam_info.D = calib_data['distortion_coefficients']['data']
00075 cam_info.R = calib_data['rectification_matrix']['data']
00076 cam_info.P = calib_data['projection_matrix']['data']
00077 cam_info.distortion_model = calib_data['distortion_model']
00078 cam_info.binning_x = calib_data['binning_x']
00079 cam_info.binning_y = calib_data['binning_y']
00080 cam_info.roi.width = calib_data['roi_image_width']
00081 cam_info.roi.height = calib_data['roi_image_height']
00082 cam_info.roi.x_offset = calib_data['roi_x_offset']
00083 cam_info.roi.y_offset = calib_data['roi_y_offset']
00084 return pair[0], cam_info
00085
00086 if __name__ == "__main__":
00087 rospy.init_node('change_camera_info')
00088 parser = argparse.ArgumentParser(description='Change camera info messages in a bagfile.')
00089 parser.add_argument('inbag', help='input bagfile')
00090 parser.add_argument('outbag', help='output bagfile')
00091 parser.add_argument('replacement', type=replacement, nargs='+', help='replacement in form "TOPIC=CAMERA_INFO_FILE", e.g. /stereo/left/camera_info=my_new_info.yaml')
00092 args = parser.parse_args()
00093 replacements = {}
00094 for topic, calib_file in args.replacement:
00095 replacements[topic] = calib_file
00096 try:
00097 change_camera_info(args.inbag, args.outbag, replacements)
00098 except Exception, e:
00099 import traceback
00100 traceback.print_exc()