Skip to content

Commit

Permalink
1. Changed from exposing strings to just using numpy as practically t…
Browse files Browse the repository at this point in the history
…he strings are useless in python and they make it messier to use numpy.

2. Added a 'body' function pointer to the runloop that is called between event checks
3. Added a 'Kill' exception that can be used to disable the runloop from inside the body
4. Updated all demos
Signed-off-by: Brandyn A. White <[email protected]>
  • Loading branch information
Brandyn A. White committed Nov 28, 2010
1 parent e0f3458 commit 8537f56
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 23 deletions.
4 changes: 2 additions & 2 deletions wrappers/python/demo_cv_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ def display_rgb(dev, data, timestamp):
cv.ShowImage('RGB', image)
cv.WaitKey(5)

freenect.runloop(depth=lambda *x: display_depth(*freenect.depth_cb_np(*x)),
rgb=lambda *x: display_rgb(*freenect.rgb_cb_np(*x)))
freenect.runloop(depth=display_depth,
rgb=display_rgb)
4 changes: 2 additions & 2 deletions wrappers/python/demo_cv_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
cv.NamedWindow('RGB')

while 1:
depth, timestamp = freenect.sync_get_depth_np()
rgb, timestamp = freenect.sync_get_rgb_np()
depth, timestamp = freenect.sync_get_depth()
rgb, timestamp = freenect.sync_get_rgb()
cv.ShowImage('Depth', depth.astype(np.uint8))
cv.ShowImage('RGB', rgb.astype(np.uint8))
cv.WaitKey(10)
14 changes: 14 additions & 0 deletions wrappers/python/demo_kill_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env python
import freenect
import time


def body(dev, ctx):
if time.time() - start_time > 1.:
print('1 sec passed, killing')
raise freenect.Kill

start_time = time.time()
freenect.runloop(body=body)
start_time = time.time()
freenect.runloop(body=body)
4 changes: 2 additions & 2 deletions wrappers/python/demo_mp_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ def display_rgb(dev, data, timestamp):
image_rgb = mp.imshow(data, interpolation='nearest', animated=True)
mp.draw()

freenect.runloop(depth=lambda *x: display_depth(*freenect.depth_cb_np(*x)),
rgb=lambda *x: display_rgb(*freenect.rgb_cb_np(*x)))
freenect.runloop(depth=display_depth),
rgb=display_rgb)
8 changes: 4 additions & 4 deletions wrappers/python/demo_mp_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

mp.ion()
mp.figure(1)
image_depth = mp.imshow(freenect.sync_get_depth_np()[0], interpolation='nearest', animated=True)
image_depth = mp.imshow(freenect.sync_get_depth()[0], interpolation='nearest', animated=True)
mp.figure(2)
image_rgb = mp.imshow(freenect.sync_get_rgb_np()[0], interpolation='nearest', animated=True)
image_rgb = mp.imshow(freenect.sync_get_rgb()[0], interpolation='nearest', animated=True)

while 1:
mp.figure(1)
image_depth.set_data(freenect.sync_get_depth_np()[0])
image_depth.set_data(freenect.sync_get_depth()[0])
mp.figure(2)
image_rgb.set_data(freenect.sync_get_rgb_np()[0])
image_rgb.set_data(freenect.sync_get_rgb()[0])
mp.draw()
38 changes: 25 additions & 13 deletions wrappers/python/freenect.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ cdef void depth_cb(void *dev, char *data, int timestamp):
dev_out = DevPtr()
dev_out._ptr = dev
if _depth_cb:
_depth_cb(dev_out, PyString_FromStringAndSize(data, nbytes), timestamp)
_depth_cb(*_depth_cb_np(dev_out, PyString_FromStringAndSize(data, nbytes), timestamp))


cdef void rgb_cb(void *dev, char *data, int timestamp):
Expand All @@ -193,7 +193,11 @@ cdef void rgb_cb(void *dev, char *data, int timestamp):
dev_out = DevPtr()
dev_out._ptr = dev
if _rgb_cb:
_rgb_cb(dev_out, PyString_FromStringAndSize(data, nbytes), timestamp)
_rgb_cb(*_rgb_cb_np(dev_out, PyString_FromStringAndSize(data, nbytes), timestamp))


class Kill(Exception):
"""This kills the runloop, raise from the body only"""


def runloop(depth=None, rgb=None, body=None):
Expand Down Expand Up @@ -229,9 +233,16 @@ def runloop(depth=None, rgb=None, body=None):
freenect_start_rgb(devp)
freenect_set_depth_callback(devp, depth_cb)
freenect_set_rgb_callback(devp, rgb_cb)
while freenect_process_events(ctxp) >= 0:
if body:
body(dev, ctx)
try:
while freenect_process_events(ctxp) >= 0:
if body:
body(dev, ctx)
except Kill:
pass
freenect_stop_depth(devp)
freenect_stop_rgb(devp)
freenect_close_device(devp)
freenect_shutdown(ctxp)


def _load_numpy():
Expand All @@ -243,7 +254,7 @@ def _load_numpy():
raise e


def depth_cb_np(dev, string, timestamp):
def _depth_cb_np(dev, string, timestamp):
"""Converts the raw depth data into a numpy array for your function
Args:
Expand All @@ -260,7 +271,7 @@ def depth_cb_np(dev, string, timestamp):
return dev, data, timestamp


def rgb_cb_np(dev, string, timestamp):
def _rgb_cb_np(dev, string, timestamp):
"""Converts the raw depth data into a numpy array for your function
Args:
Expand All @@ -277,7 +288,7 @@ def rgb_cb_np(dev, string, timestamp):
return dev, data, timestamp


def sync_get_depth():
def _sync_get_depth_str():
"""Get the next available depth frame from the kinect.
Returns:
Expand All @@ -295,7 +306,7 @@ def sync_get_depth():
return depth_str, timestamp


def sync_get_rgb():
def _sync_get_rgb_str():
"""Get the next available rgb frame from the kinect.
Returns:
Expand All @@ -319,7 +330,7 @@ def sync_stop():
freenect_sync_stop()


def sync_get_rgb_np():
def sync_get_rgb():
"""Get the next available RGB frame from the kinect, as a numpy array.
Returns:
Expand All @@ -329,15 +340,15 @@ def sync_get_rgb_np():
"""
np = _load_numpy()
try:
string, timestamp = sync_get_rgb()
string, timestamp = _sync_get_rgb_str()
except TypeError:
return
data = np.fromstring(string, dtype=np.uint8)
data.resize((480, 640, 3))
return data, timestamp


def sync_get_depth_np():
def sync_get_depth():
"""Get the next available depth frame from the kinect, as a numpy array.
Returns:
Expand All @@ -347,9 +358,10 @@ def sync_get_depth_np():
"""
np = _load_numpy()
try:
string, timestamp = sync_get_depth()
string, timestamp = _sync_get_depth_str()
except TypeError:
return
data = np.fromstring(string, dtype=np.uint16)
data.resize((480, 640))
return data, timestamp

0 comments on commit 8537f56

Please sign in to comment.