Package pyvision :: Package types :: Module kinect
[hide private]
[frames] | no frames]

Source Code for Module pyvision.types.kinect

 1  ''' 
 2  Created on Jan 28, 2012 
 3   
 4  @author: bolme 
 5  ''' 
 6  #import freenect 
 7  import cv 
 8  import numpy as np 
 9  import pyvision as pv 
10   
11   
12  # Here is a nice pythonic interface to the kinect sensor for pyvision 
13 -class Kinect(object):
14 ''' 15 This class provides a simple interface to the kinect sensor. When operating 16 properly the device produces an color RGB video frame (8bit 640x480) and a 17 depth image (11-bit, 640x480) at around 20-24 frames per second with low latency. 18 19 20 The kinect drivers to not appear to be robust. Some trial and error may be 21 needed to determine how to best interface with the device. Here are some problem 22 behaviours that I have seen: # 23 * The Kinect does not seem to start properly. 24 * Calling some functions in the freenect module results in segmentation 25 faults 26 * Connecting to the sensor temporarily or perminatly interupts the usb 27 bus. (The keyboard and mouse stop working.) 28 * The device seems to work better when connected to one of the ports on 29 my laptop but not the other. 30 * The device sometimes prints out error/warning messages which do not seem 31 to interfere with the operation of the device. 32 "[Stream 70] Expected 1748 data bytes, but got 948" # 33 34 After connecting the device to the usb port it may be helpful to initialize 35 the device using ipython. Try running these commands before connecting through 36 pyvision or attempting to grab frames: 37 38 import freenect 39 freenect.init() # start the connection 40 exit 41 42 The current implementation initializes the device and returns RGB and depth 43 frames. No advanced features such as LED, Audio, or Tilt are supported because 44 they cause sementation violations. 45 46 ''' 47
48 - def __init__(self):
49 ''' 50 Initialize a Kinect device. 51 ''' 52 # Try importing the freenect library. 53 import freenect as fn #@UnresolvedImport 54 self.fn = fn 55 self.fn.init()
56
57 - def __iter__(self):
58 ''' Return an iterator for this video ''' 59 return self
60
61 - def next(self):
62 ''' 63 Get the next frame and depth image from the sensor. 64 ''' 65 # This is an 11bit numpy array 66 depth = self.fn.sync_get_depth()[0] 67 68 # This is an 8 bit numpy array 69 frame = self.fn.sync_get_video()[0] 70 71 r,c,chan = frame.shape 72 73 cvframe = cv.CreateImageHeader((c,r),cv.IPL_DEPTH_8U,chan) 74 cv.SetData(cvframe,frame.tostring(),3*c) 75 cv.CvtColor(cvframe,cvframe,cv.CV_RGB2BGR) 76 return pv.Image(cvframe),pv.Image(np.array(depth.T,dtype=np.float))
77 78 # The main function that runs some tests 79 if __name__ == '__main__': 80 print "Testing the kinect sensor." 81 print "Press any key to quit" 82 83 kinect = pv.Kinect() 84 for frame, depth in kinect: 85 # Display the images 86 k = max( 87 depth.show(window="Depth",delay=1,pos=(100,100)), 88 frame.show(window="Video",delay=1,pos=(740,100)) 89 ) 90 91 # Check to see if a key was pressed 92 if k != -1: 93 break 94