Skip to content

Commit 54ae3ab

Browse files
authored
internal conversion to bytes from numpy array
1 parent b6dea65 commit 54ae3ab

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

pylibfreenect2/libfreenect2.pyx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,9 @@ cdef class Frame:
217217
frame_type : int, optional
218218
Underlying frame type. Default is -1. Used by ``asarray`` method.
219219
220-
byte_data : byte_data, optional
221-
Numpy array of depth or ir data in byte format,
220+
numpy_array : numpy.ndarray, optional
221+
Numpy array of depth or ir data with ndim=2,
222222
that will be converted to a frame class.
223-
Use byte_data = numpy_array.tobytes('C') for conversion to bytes.
224223
Default is None.
225224
See also
226225
--------
@@ -233,7 +232,7 @@ cdef class Frame:
233232
cdef int frame_type
234233

235234
def __cinit__(self, width=None, height=None, bytes_per_pixel=None,
236-
int frame_type=-1, byte_data=None):
235+
int frame_type=-1, np.ndarray[np.float32_t, ndim=2, mode="c"] numpy_array=None):
237236
w,h,b = width, height, bytes_per_pixel
238237
all_none = (w is None) and (h is None) and (b is None)
239238
all_not_none = (w is not None) and (h is not None) and (b is not None)
@@ -243,15 +242,21 @@ cdef class Frame:
243242

244243
if all_not_none:
245244
self.take_ownership = True
246-
if byte_data is not None:
245+
if numpy_array is None:
247246
self.ptr = new libfreenect2.Frame(
248-
width, height, bytes_per_pixel, byte_data)
247+
width, height, bytes_per_pixel, NULL)
249248
else:
250-
self.ptr = new libfreenect2.Frame(
251-
width, height, bytes_per_pixel, NULL)
249+
self.__instantiate_frame_with_bytes(
250+
width, height, bytes_per_pixel, numpy_array.reshape(-1))
252251
else:
253252
self.take_ownership = False
254253

254+
cdef __instantiate_frame_with_bytes(self, int width, int height,
255+
int bytes_per_pixel, np.ndarray[np.float32_t, ndim=1, mode="c"] numpy_array):
256+
cdef uint8_t* bytes_ptr = reinterpret_cast[uint8_pt](&numpy_array[0])
257+
self.ptr = new libfreenect2.Frame(
258+
width, height, bytes_per_pixel, bytes_ptr)
259+
255260
def __dealloc__(self):
256261
if self.take_ownership and self.ptr is not NULL:
257262
del self.ptr

0 commit comments

Comments
 (0)