|
| 1 | +#!/usr/bin/python |
| 2 | +""" |
| 3 | +Copyright (c) 2012, |
| 4 | +Systems, Robotics and Vision Group |
| 5 | +University of the Balearican Islands |
| 6 | +All rights reserved. |
| 7 | +
|
| 8 | +Redistribution and use in source and binary forms, with or without |
| 9 | +modification, are permitted provided that the following conditions are met: |
| 10 | + * Redistributions of source code must retain the above copyright |
| 11 | + notice, this list of conditions and the following disclaimer. |
| 12 | + * Redistributions in binary form must reproduce the above copyright |
| 13 | + notice, this list of conditions and the following disclaimer in the |
| 14 | + documentation and/or other materials provided with the distribution. |
| 15 | + * Neither the name of Systems, Robotics and Vision Group, University of |
| 16 | + the Balearican Islands nor the names of its contributors may be used to |
| 17 | + endorse or promote products derived from this software without specific |
| 18 | + prior written permission. |
| 19 | +
|
| 20 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 21 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 22 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 23 | +DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY |
| 24 | +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 25 | +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 27 | +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | +""" |
| 31 | + |
| 32 | + |
| 33 | +PKG = 'bag_tools' # this package name |
| 34 | + |
| 35 | +import roslib; roslib.load_manifest(PKG) |
| 36 | +import rospy |
| 37 | +import rosbag |
| 38 | +import os |
| 39 | +import sys |
| 40 | +import argparse |
| 41 | +import yaml |
| 42 | +import sensor_msgs.msg |
| 43 | + |
| 44 | +def change_camera_info(inbag,outbag,replacements): |
| 45 | + rospy.loginfo(' Processing input bagfile: %s', inbag) |
| 46 | + rospy.loginfo(' Writing to output bagfile: %s', outbag) |
| 47 | + # parse the replacements |
| 48 | + maps = {} |
| 49 | + for k, v in replacements.items(): |
| 50 | + rospy.loginfo('Changing topic %s to contain following info (header will not be changed):\n%s',k,v) |
| 51 | + |
| 52 | + outbag = rosbag.Bag(outbag,'w') |
| 53 | + for topic, msg, t in rosbag.Bag(inbag,'r').read_messages(): |
| 54 | + if topic in replacements: |
| 55 | + new_msg = replacements[topic] |
| 56 | + new_msg.header = msg.header |
| 57 | + msg = new_msg |
| 58 | + outbag.write(topic, msg, t) |
| 59 | + rospy.loginfo('Closing output bagfile and exit...') |
| 60 | + outbag.close(); |
| 61 | + |
| 62 | +def replacement(replace_string): |
| 63 | + pair = replace_string.split('=', 1) |
| 64 | + if len(pair) != 2: |
| 65 | + raise argparse.ArgumentTypeError("Replace string must have the form /topic=calib_file.yaml") |
| 66 | + if pair[0][0] != '/': |
| 67 | + pair[0] = '/'+pair[0] |
| 68 | + stream = open(pair[1], 'r') |
| 69 | + calib_data = yaml.load(stream, Loader=yaml.FullLoader) |
| 70 | + cam_info = sensor_msgs.msg.CameraInfo() |
| 71 | + cam_info.width = calib_data['image_width'] |
| 72 | + cam_info.height = calib_data['image_height'] |
| 73 | + cam_info.K = calib_data['camera_matrix']['data'] |
| 74 | + cam_info.D = calib_data['distortion_coefficients']['data'] |
| 75 | + cam_info.R = calib_data['rectification_matrix']['data'] |
| 76 | + cam_info.P = calib_data['projection_matrix']['data'] |
| 77 | + cam_info.distortion_model = calib_data['distortion_model'] |
| 78 | + return pair[0], cam_info |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + |
| 82 | + ''' |
| 83 | + CALL : python3 change_camera_info_folder.py --path_in path/to/in/folder --path_out path/to/out/folder --replacement /stereo_down/left/camera_info=path/to/left.yaml /stereo_down/right/camera_info=path/to/right.yaml |
| 84 | + |
| 85 | + troubleshooting: |
| 86 | + - pip3 install rospkg |
| 87 | + - pip3 install pycryptodomex |
| 88 | + - pip3 install gnupg |
| 89 | +
|
| 90 | + ''' |
| 91 | + rospy.init_node('change_camera_info') |
| 92 | + parser = argparse.ArgumentParser(description='Change camera info messages in a bagfile.') |
| 93 | + parser.add_argument('--path_in', help='input folder') |
| 94 | + parser.add_argument('--path_out', help='output folder') |
| 95 | + 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') |
| 96 | + args = parser.parse_args() |
| 97 | + replacements = {} |
| 98 | + |
| 99 | + path_in = args.path_in # get in folder |
| 100 | + path_out = args.path_out # get out folder |
| 101 | + |
| 102 | + |
| 103 | + for file in sorted(os.listdir(path_in)): |
| 104 | + print("working on file:" + str(file)) |
| 105 | + |
| 106 | + file_path_in = os.path.join(path_in, file) |
| 107 | + file_path_out = os.path.join(path_out, file) |
| 108 | + |
| 109 | + for topic, calib_file in args.replacement: |
| 110 | + replacements[topic] = calib_file |
| 111 | + try: |
| 112 | + change_camera_info(file_path_in, file_path_out, replacements) |
| 113 | + except Exception as e: |
| 114 | + import traceback |
| 115 | + traceback.print_exc() |
0 commit comments