def sortbags(inbag, outbag): rebag = grosbag.Bag(outbag, 'w') try: schedule = [(t, i) for i, ( topic, msg, t) in enumerate(grosbag.Bag(inbag).read_messages(raw=True))] schedule = [i for (t, i) in sorted(schedule)] print(schedule) stage = {} for i, (topic, msg, t) in enumerate(grosbag.Bag(inbag).read_messages(raw=True)): stage[i] = (topic, msg, t) while (len(schedule) > 0) and (schedule[0] in stage): (topic, msg, t) = stage[schedule[0]] rebag.write(topic, msg, t, raw=True) del stage[schedule[0]] schedule = schedule[1:] assert schedule == [] assert stage == {} finally: rebag.close()
def fixbags(md5file, inbag, outbag): d = {} for line in fileinput.input(md5file): sp = line.split() d[sp[1]] = [sp[0], sp[2], sp[3]] rebag = grosbag.Bag(outbag, 'w') for topic, msg, t in grosbag.Bag(inbag).read_messages(raw=True): type, bytes, md5 = msg[0], msg[1], msg[2] if md5 in d: if type != d[md5][0]: print('WARNING: found matching md5, but non-matching name') continue msg = (d[md5][1], msg[1], d[md5][2]) rebag.write(topic, msg, t, raw=True) rebag.close()
def fix_md5sums(inbags): for b in inbags: print('Trying to migrating file: %s' % b) outbag = b + '.tmp' rebag = grosbag.Bag(outbag, 'w') try: for i, (topic, msg, t) in enumerate(grosbag.Bag(b).read_messages(raw=True)): rebag.write(topic, msg, t, raw=True) rebag.close() except grosbag.ROSBagException as e: print(' Migration failed: %s' % str(e)) os.remove(outbag) continue oldnamebase = b + '.old' oldname = oldnamebase i = 1 while os.path.isfile(oldname): i += 1 oldname = oldnamebase + str(i) os.rename(b, oldname) os.rename(outbag, b) print(' Migration successful. Original stored as: %s' % oldname)
'-b', '--bagfiles', action='store', dest='bagfile', default=None, help='Save message from a bagfile rather than system definition') (options, args) = parser.parse_args() if len(args) < 1: parser.error('Message type not specified.') if options.bagfile is None: sys_class = roslib.message.get_message_class(args[0]) if sys_class is None: print('Could not find message %s.' % args[0], file=sys.stderr) else: print('[%s]:' % args[0]) print(sys_class._full_text) else: for topic, msg, t in grosbag.Bag( options.bagfile).read_messages(raw=True): if msg[0] == args[0]: print('[%s]:' % args[0]) print(msg[4]._full_text) exit(0) print('Could not find message %s in bag %s.' % (args[0], options.bagfile), file=sys.stderr)
def fastrebag(inbag, outbag): rebag = grosbag.Bag(outbag, 'w') for i, (topic, msg, t) in enumerate(grosbag.Bag(inbag).read_messages(raw=True)): rebag.write(topic, msg, t, raw=True) rebag.close()
# # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # %Tag(WRITE_PY)% import grosbag from std_msgs.msg import Int32, String bag = grosbag.Bag('test.bag', 'w') str = String() str.data = 'foo' i = Int32() i.data = 42 bag.write('chatter', str) bag.write('numbers', i) bag.close() # %EndTag(WRITE_PY)%
def rename_topic(intopic, inbag, outtopic, outbag): rebag = grosbag.Bag(outbag, 'w') for topic, msg, t in grosbag.Bag(inbag).read_messages(raw=True): rebag.write(outtopic if topic == intopic else topic, msg, t, raw=True) rebag.close()
"""Take an sensor_msgs/Image and return a PIL image""" if len(msg.uint8_data.data) == 0 and len(msg.int16_data.data) == 0: return None if msg.depth == 'uint8': ma, image_data = msg.uint8_data, ma.data else: ma, image_data = msg.int16_data, int16_str(ma.data) dim = dict([(d.label, d.size) for d in ma.layout.dim]) mode = { ('uint8', 1): "L", ('uint8', 3): "RGB", ('int16', 1): "L" }[msg.depth, dim['channel']] (w, h) = (dim['width'], dim['height']) return Image.fromstring(mode, (w, h), image_data) counter = 0 for topic, msg, t in grosbag.Bag(sys.argv[1]).read_messages(): if topic.endswith('stereo/raw_stereo'): for (mi, c) in [(msg.left_image, 'L'), (msg.right_image, 'R'), (msg.disparity_image, 'D')]: im = msg2im(mi) if im: ext = {'L': 'png', 'RGB': 'png', 'F': 'tiff'}[im.mode] im.save('%06d%s.%s' % (counter, c, ext)) counter += 1