Slow Rosbag Image Extraction.

asked 2019-07-02 08:37:16 -0600

RyanOxon gravatar image

updated 2019-07-09 10:35:04 -0600

I am having issues extracting images from the bag files with the Python API, the extraction process is very slow. An example would be extracting frames from a bag containing video messages, the recording is 120 seconds long, but it takes close to 40 seconds to extract the data for just one image topic, which isn't ideal.

For context we use a stereo cameras that create 720p RGB images at 15FPS.

Current Data Flow:

Open Image Message -> ROS CVBridge to convert ROS image to OpenCV Image -> Use the tostring method to convert the image to byte array -> Send the byte Array to another service to create and store the image.

Below are the timings for the different parts of the program:

  • Lopping over the bag without doing anything. 10 seconds
  • Looping and converting to OpenCV Image Only. 12 Seconds
  • Looping and converting using ROS CVBridge and converting image to Numpy Array. 30 seconds

Below is the bit of code I am working with:

bag = rosbag.Bag('blackflys_left_2019-05-02-13-40-46.bag')

for topic, msg, t in bag.read_messages(topics=['/stereo/left/image_raw']):

    #Convert to a real image
    cv2_img = bridge.imgmsg_to_cv2(msg, desired_encoding="passthrough")

   #Encode the images as a bytes array
    img_str = cv2.imencode('.jpg', cv2_img)[1].tostring()


Am I missing something or are these sort of speeds normal? We would ideally like to extract and store the images as fast as possible.

Thank you.

edit retag flag offensive close merge delete


I am also running into this same issue right now. Did you find a solution?

kaalessi gravatar image kaalessi  ( 2020-05-06 08:38:01 -0600 )edit