librealsense: Unable to open ros file after saving using pyrealsense2

  • Before opening a new issue, we wanted to provide you with some useful suggestions (Click “Preview” above for a better view):

  • All users are welcomed to report bugs, ask questions, suggest or request enhancements and generally feel free to open new issue, even if they haven’t followed any of the suggestions above 😃


Required Info
Camera Model D435
Firmware Version 05.10.13.00
Operating System & Version Linux (Ubuntu 16)
Kernel Version (Linux Only) 4.15.0-43
Platform PC
SDK Version 2.17.1
Language python 3.5.6

Issue Description

I’m having an issue with opening up a bag file with the realsense viewer after recording it in Python. I get this error:

Failed to load file … test.bag. Reason: Io in rs2_context_add_device(ctx:0x1274510, file:/): Failed to create ros reader: Bag unindexed

This looks similar but not quite the same as issue 2326 but I’m unable to resolve it.

The code is as follows:

import pyrealsense2 as rs
import time

pipeline = rs.pipeline()
config = rs.config()

config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.rgb8, 30)
config.enable_record_to_file('test.bag')
pipeline.start(config)

try:
    start=time.time()
    while time.time() - start < 20:
        pipeline.wait_for_frames()
finally:
    pipeline.stop()

Oddly, I’m able to open the bag file if I run this code, and then re-run it and change pipeline.start(config) to pipeline.start()

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17

Most upvoted comments

Thanks so much @lamuyang for sharing what worked for you!

I don’t have constant access to the Realsense cameras, as we are using them to collect data for a study. This is a relatively low priority issue for me, because – as expected – destruction of the objects was a sufficient work-around. As @jkenney9a and I noted, a call to .stop did not write the bag properly, but a call to .stop plus the deliberate destruction of all Realsense objects does result in a working bag. I still consider this an issue, but not one that I can spend more time on.

Did you try @jkenney9a 's code in IPython? That seems like the simplest way to reproduce the error. IPython is an important part of the equation, as it is likely preventing the wrapper from cleaning up. As dorodnic noted, the issue is likely caused by wrappers that do not completely clean up Realsense objects. As @jkenney9a and I noted, this does not seem like expected or appropriate behavior. When we call .stop, we have the expectation that we’ve written a valid bag file. But the persistence of the Realsense objects (due to IPython in @jkenney9a 's case and persistent class variables in mine) seems to prevent this.

I can’t test this right now, but the following code probably results in an improperly indexed bag:

import shutil, time, pyrealsense2 as rs2

class Pipeline:
    
    def __init__(self, filepath='test.bag'):
        self.filepath = filepath
        context       = rs2.context()
        self.device   = device = context.devices[0]
        serial_number = device.get_info(rs2.camera_info.serial_number)
        get_sensor    = lambda s: s.get_info(rs2.camera_info.name)
        self.sensors  = {get_sensor(s): s for s in device.sensors}        
        self.config   = config = rs2.config()
        self.pipeline = rs2.pipeline(context)
        config.enable_device(serial_number)
        config.enable_record_to_file('tmp.bag')
        
    def start(self): self.pipeline.start(self.config)
        
    def stop(self):
        self.pipeline.stop()
        
        # When the lines below are un-commented, the bag seems to write
        # properly. When they are commented, the bag produces an index error.
        
        #del self.pipeline
        #del self.config
        #del self.sensors
        #del self.device
        #self.pipeline = None
        #self.config = None
        #self.sensors = None
        #self.device = None
        
        shutil.move('tmp.bag', self.filepath)
        

if __name__ == '__main__':
    pipeline = Pipeline()
    pipeline.start()
    time.sleep(1)
    pipeline.stop()

Well shouldn’t running the command pipeline.stop() be sufficient to properly close and index the bag file? Sounds like this isn’t python specific, a similar issue has been brought up with C++ here.

Just wanted to give a brief update. I’m using Anaconda and Spyder for code development. This issue happens when I’m running the code in Spyder and executing in the IPython console. However, if I execute it from the shell (i.e. python video_recording.py), then there is no issue with opening the resulting file. This is a little annoying for code development, but at the end of the day this is how we plan on making use of the file so not as major an issue for us.

I am using C++ and experiencing similar issues. After a recording, I cannot open the file directly as I will get the same error. I’ve got to close and open my application (restart) in order to successfully open the bag file.

I think there is something wrong during the desconstruction of enable_record_to_file function/config. Some resources of some sort are not properly closed.

Hopefully there will be some traction in this issue which might help my case as well. Fingers crossed