ethz-asl / maplab

A Modular and Multi-Modal Mapping Framework
https://maplab.asl.ethz.ch
Apache License 2.0
2.63k stars 723 forks source link

Rosbag input #192

Closed EmreOzkose closed 4 years ago

EmreOzkose commented 4 years ago

Generating rosbag file from video.


Hi ! How can a rosbag file is generated from a video? I know tango ros streamer, but I have already some videos and I am trying to convert them to rosbag files because in maplab console, we could not generate a map if we do not have rosbag files?

Additionally, I know Tango-ros-streamer, but this app requires special device which I could not reach for now. Hence, I guess that I have to create rosbag files from videos to create a map with this code:

roscore& rosrun rovioli tutorial_euroc save_folder rosbag_file_name.bag

Thanks in advance.

UPDATE: I found a script from https://stackoverflow.com/questions/31432870/how-do-i-convert-a-video-or-a-sequence-of-images-to-a-bag-file.

import time, sys, os
import cv2

import ros
from ros import rosbag
import roslib, rospy
from cv_bridge import CvBridge

roslib.load_manifest('sensor_msgs')
from sensor_msgs.msg import Image

TOPIC = 'camera/image_raw'

def CreateVideoBag(videopath, bagname):
    '''Creates a bag file with a video file'''
    bag = rosbag.Bag(bagname, 'w')
    cap = cv2.VideoCapture(videopath)
    cb = CvBridge()
    prop_fps = cap.get(cv2.CAP_PROP_FPS)
    if prop_fps != prop_fps or prop_fps <= 1e-2:
        print ("Warning: can't get FPS. Assuming 24.")
        prop_fps = 24
    ret = True
    frame_id = 0
    while(ret):
        ret, frame = cap.read()
        print(frame_id)
        if not ret:
            break
        stamp = rospy.rostime.Time.from_sec(float(frame_id) / prop_fps)
        frame_id += 1
        image = cb.cv2_to_imgmsg(frame, encoding='bgr8')
        image.header.stamp = stamp
        image.header.frame_id = "camera"
        bag.write(TOPIC, image, stamp)
    cap.release()
    bag.close()

if __name__ == "__main__":
    if len(sys.argv) == 3:
        CreateVideoBag(*sys.argv[1:])
    else:
        print("Usage: video2bag videofilename bagfilename")

But I got this erro message when run rosrun rovioli tutorial_euroc save_folder /home/MY_NAME/PycharmProjects/empyt/bagfile.bag:

Error: Index entry for topic camera/image_raw contains invalid time. This message will not be loaded.

with this output:

Error: Index entry for topic camera/image_raw contains invalid time. This message will not be loaded. at line 653 in /tmp/binarydeb/ros-melodic-rosbag-storage-1.14.3/src/bag.cpp I1216 10:50:24.058601 12265 rovio-factory.cc:169] Loading ROVIO configuration template: /home/eramas/maplab_ws/src/maplab/applications/rovioli/share//rovio_default_config.info Set distortion parameters (Radtan) to: k1(-0.283408), k2(0.0739591), k3(0), p1(0.000193595), p2(1.76187e-05) Registered filter state update callback. I1216 10:50:24.507083 12265 map-builder-flow.cc:31] Set VIMap folder to: save_folder_0 I1216 10:50:24.512887 12265 rovioli-node.cc:105] Starting data source... I1216 10:50:24.512924 12295 datasource-rosbag.cc:194] Rosbag playback finished! I1216 10:50:24.512919 12265 rovioli-app.cc:136] Message delivery queues: subscriber-node queue-topic queue-id num elements
MapBuilderFlow LOCALIZATION_RESULT 09be..0000 0
MapBuilderFlow ROVIO_ESTIMATES b5a5..0000 0
DataPublisherFlow LOCALIZATION_RESULT b185..0000 0
MapBuilderFlow VIO_UPDATES 8846..0000 0
DataPublisherFlow RAW_VIMAP db47..0000 0
DataPublisherFlow VIO_UPDATES 3cb0..0000 0
SyncedNFrameThrottlerFlow TRACKED_NFRAMES_AND_IMU 02ea..0000 0
FeatureTrackingFlow ROVIO_ESTIMATES 4bad..0000 0
RovioFlow IMAGE_MEASUREMENTS 952f..0000 0
RovioFlow IMU_MEASUREMENTS 41f8..0000 0
RovioFlow LOCALIZATION_RESULT 693e..0000 0
MapBuilderFlow TRACKED_NFRAMES_AND_IMU 9086..0000 0
DataPublisherFlow ROVIO_ESTIMATES 9494..0000 0
ImuCameraSynchronizerFlow IMAGE_MEASUREMENTS 9ca2..0000 0
FeatureTrackingFlow SYNCED_NFRAMES_AND_IMU a18b..0000 0
ImuCameraSynchronizerFlow IMU_MEASUREMENTS b4aa..0000 0
I1216 10:50:25.513213 12265 rovioli-node.cc:110] Closing data source... W1216 10:50:25.513284 12265 map-builder-flow.cc:101] Map is empty; nothing will be saved. I1216 10:50:25.513608 12265 rovioli-node.cc:110] Closing data source...

So, the script save a .bag file, but there is an invalid time issue. How can I solve this?

EmreOzkose commented 4 years ago

I solve this issue with changing stamp line: before: stamp = rospy.rostime.Time.from_sec(float(frame_id) / prop_fps) after: stamp = rospy.Time.from_sec(time.time())

Abdob commented 4 years ago

did you need to configure any scripts to get this working? the ros topics in the rosbag should be consistent with the configuration right