cythonpython-3.5picklekinectlibfreenect2

How to save libfreenect2.Frame data with python


I'm trying to record kinectv2 data for Image classification problem I am trying to solve. Is there any way to record the kinectv2 data?

I have tried using pickle to save the depth data, however since there is no __reduce__ method in the libfreenect2 library for the Frame class I encountered an error.

frames = listener.waitForNewFrame()

depth = frames["depth"]

with open("captures/frame_" + str(i) + "_depth.obj", 'w') as file:
    pickle.dump(depth, file)

with open("captures/frame_" + str(i) + "_depth.obj", 'r') as file: 
    depth = pickle.load(file)

I encountered the given error:

TypeError: no default __reduce__ due to non-trivial __cinit__


Solution

  • Your two options are:

    1. Make the class pickleable. This involves editing the Cython code of libfreenect2. Probably the most viable way to do it is to add a __reduce__ method, returning the Frame constructor and a tuple of arguments.

    2. Just save the frame data instead - the Frame has an asarray function that can get a Numpy array, and there's loads of options for saving those. This is probably the simplest approach. When you want to load it then just load the Numpy array and call the frame constructor with that.