#!/usr/bin/env python import freenect import cv cv.NamedWindow('RGB') def display(dev, data, timestamp): image = cv.CreateImageHeader((data.shape[1], data.shape[0]), cv.IPL_DEPTH_8U, 3) # Note: We swap from RGB to BGR here cv.SetData(image, data[:, :, ::-1].tostring(), data.dtype.itemsize * 3 * data.shape[1]) cv.ShowImage('RGB', image) cv.WaitKey(5) freenect.runloop(rgb=lambda *x: display(*freenect.rgb_cb_np(*x)))
#!/usr/bin/env python import freenect from demo_mp_depth_show import display freenect.runloop(rgb=lambda *x: display(*freenect.rgb_cb_np(*x)))
import cv import numpy as np cv.NamedWindow('Depth') cv.NamedWindow('RGB') def display_depth(dev, data, timestamp): data -= np.min(data.ravel()) data *= 65536 / np.max(data.ravel()) image = cv.CreateImageHeader((data.shape[1], data.shape[0]), cv.IPL_DEPTH_16U, 1) cv.SetData(image, data.tostring(), data.dtype.itemsize * data.shape[1]) cv.ShowImage('Depth', image) cv.WaitKey(5) def display_rgb(dev, data, timestamp): image = cv.CreateImageHeader((data.shape[1], data.shape[0]), cv.IPL_DEPTH_8U, 3) # Note: We swap from RGB to BGR here cv.SetData(image, data[:, :, ::-1].tostring(), data.dtype.itemsize * 3 * data.shape[1]) 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)))
# If we get a new frame, put it in the queue def _depth_cb(dev, string, ts): global depthqueue with depthcond: depthqueue = [string] depthcond.notify() # If we get a new frame, put it in the queue def _rgb_cb(dev, string, ts): global rgbqueue with rgbcond: rgbqueue = [string] rgbcond.notify() depth_cb = lambda *x: _depth_cb(*freenect.depth_cb_np(*x)) rgb_cb = lambda *x: _rgb_cb(*freenect.rgb_cb_np(*x)) def get_depth(): """Grabs a new depth frame, blocking until the background thread provides one. Returns: a numpy array, with shape: (480,640), dtype: np.uint16 """ global depthqueue with depthcond: while not depthqueue: depthcond.wait(5) string = depthqueue[0] depthqueue = [] data = np.fromstring(string, dtype=np.uint16) data.resize((480, 640))