ros / ros_comm

ROS communications-related packages, including core client libraries (roscpp, rospy, roslisp) and graph introspection tools (rostopic, rosnode, rosservice, rosparam).
http://wiki.ros.org/ros_comm
753 stars 911 forks source link

Expected INDEX_DATA record #2347

Open Hypothesis-Z opened 1 year ago

Hypothesis-Z commented 1 year ago

Given a segment number, I tried to split a ros bag into segments by running the following code:

class CommandSplit:
    def __init__(self, args):
        self.filename = args.filename
        self.output = args.output
        if self.output == None:
            self.output = self.get_defalut_output()
        self.number = args.number

    def get_defalut_output(self):
        t_str = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime())
        return os.path.splitext(self.filename)[0] + '_bagtools_' + t_str + '.bag'

    def run(self):
        self.bag = rosbag.Bag(self.filename)
        linspace = np.linspace(self.bag.get_start_time(), self.bag.get_end_time(), self.number+1)
        i = 0
        digit = len(str(self.number))
        outbag_filename = f'{os.path.splitext(self.output)[0]}_{i+1:0{digit}d}.bag'
        self.outbag = rosbag.Bag(outbag_filename, "w")
        print(f"writing file [{i+1}/{self.number}]: {outbag_filename}")

        for topic, raw_msg, t, conn_header in self.bag.read_messages(raw=True, return_connection_header=True):
            if i + 1 < len(linspace)-1 and t >= rospy.Time.from_sec(linspace[i+1]):
                i += 1
                self.outbag.close()
                outbag_filename = f'{os.path.splitext(self.output)[0]}_{i+1:0{digit}d}.bag'
                self.outbag = rosbag.Bag(outbag_filename, "w")
                print(f"writing file [{i+1}/{self.number}]: {outbag_filename}")
            msg_type, serialized_bytes, md5sum, pos, pytype = raw_msg
            msg = pytype()
            msg.deserialize(serialized_bytes)
            self.outbag.write(topic, msg, t, connection_header=conn_header)
            self.outbag.flush()

        self.outbag.close()

I got error when playing these segments:

zzq@DESKTOP:~/data/zhejianglab_evaluation$ rosbag play zjlab_EC_14_rgbdliou_bagtools_2023-09-11-15-04-37_00*.bag --clock
[ INFO] [1694420036.314181310]: Opening zjlab_EC_14_rgbdliou_bagtools_2023-09-11-15-04-37_001.bag
[ INFO] [1694420036.548806657]: Opening zjlab_EC_14_rgbdliou_bagtools_2023-09-11-15-04-37_002.bag
[ INFO] [1694420036.760476766]: Opening zjlab_EC_14_rgbdliou_bagtools_2023-09-11-15-04-37_003.bag
[FATAL] [1694420036.801129445]: Expected INDEX_DATA record

Some of them are corrupted but rosbag reindex dosn't work. need help...