So I've posted about this before with my work on getting deserialize to work in Python, but I'm started to get frustrated with the load times (~10-60 seconds), especially since C++ does it in a blink of an eye. The sample code is below.
My problem is I can not figure out a way to load data into the LeapPython byte_array
type in any reasonable speed. It would be great if there was a fromstring()
function like there is for the builtin array.array
, then I can skip the extra step -- array.array
performs this very quickly, the element by element copy is VERY slow and I've confirmed in profiling that this takes up at least 90% of the time. Another option would be to inherit bytearray
so that readinto file_.readinto(frame_data_ptr)
could work -- I'm not sure how this would work with swig.
Or possibly another option is to utilize the .frompointer()
function properly, but I could not find a way to do this, it always complained with TypeError. I played around with a bunch of ctypes with no luck
import Leap
Leap.Controller()
frames_list = []
with open(filename, 'rb') as file_:
frame_size_data = file_.read(4)
while frame_size_data:
frame_size = struct.unpack('i', frame_size_data)[0]
# Load the data into a python array then copy element by element...
frame_data_ptr = Leap.byte_array(frame_size)
frame_data = array.array("B")
frame_data.fromstring(file_.read(frame_size))
# This is the slow part!!
for i in xrange(frame_size):
frame_data_ptr[i] = frame_data[i]
frame = Leap.Frame()
frame.deserialize((frame_data_ptr, frame_size))
frames_list.append(frame)
frame_size_data = file_.read(4)
Does anyone have any idea or is there someone at LeapMotion who designed this python API that knows the secret to this?