00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 import freenect
00025 import numpy as np
00026 from threading import Condition, Thread
00027
00028 depthcond = Condition()
00029 rgbcond = Condition()
00030 depthqueue = []
00031 rgbqueue = []
00032
00033
00034 def _depth_cb(dev, string, ts):
00035 global depthqueue
00036 with depthcond:
00037 depthqueue = [string]
00038 depthcond.notify()
00039
00040
00041 def _rgb_cb(dev, string, ts):
00042 global rgbqueue
00043 with rgbcond:
00044 rgbqueue = [string]
00045 rgbcond.notify()
00046
00047 depth_cb = lambda *x: _depth_cb(*freenect.depth_cb_np(*x))
00048 rgb_cb = lambda *x: _rgb_cb(*freenect.rgb_cb_np(*x))
00049
00050 def get_depth():
00051 """Grabs a new depth frame, blocking until the background thread provides one.
00052
00053 Returns:
00054 a numpy array, with shape: (480,640), dtype: np.uint16
00055 """
00056 global depthqueue
00057 with depthcond:
00058 while not depthqueue:
00059 depthcond.wait(5)
00060 string = depthqueue[0]
00061 depthqueue = []
00062 data = np.fromstring(string, dtype=np.uint16)
00063 data.resize((480, 640))
00064 return data
00065
00066 def get_rgb():
00067 """Grabs a new RGB frame, blocking until the background thread provides one.
00068
00069 Returns:
00070 a numpy array, with shape: (480,640,3), dtype: np.uint8
00071 """
00072 global rgbqueue
00073 with rgbcond:
00074 while not rgbqueue:
00075 rgbcond.wait(5)
00076 string = rgbqueue[0]
00077 rgbqueue = []
00078 data = np.fromstring(string, dtype=np.uint8)
00079 data.resize((480,640,3))
00080 return data
00081
00082
00083
00084 try:
00085 loopthread == ''
00086 except:
00087 loopthread = Thread(target=freenect.runloop, kwargs=dict(depth=depth_cb, rgb=rgb_cb))
00088 loopthread.start()
00089