Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # updated may8th 2011
- import os, sys, ctypes, thread
- if '..' not in sys.path: sys.path.append( '..' )
- import rpythonic
- rpythonic.set_cache('../cache')
- freenect = rpythonic.module( 'libfreenect_sync' )
- assert freenect
- gui = rpythonic.module( 'highgui' )
- assert gui
- cv = rpythonic.module( 'cv', secondary=gui )
- assert cv
- import time
- IPL_DEPTH_16S = 2147483664
- IPL_DEPTH_8S = 2147483656
- lock = thread.allocate_lock()
- class Threaded(object):
- def start(self):
- self.active = True
- thread.start_new_thread( self.loop, () )
- class Kinect( Threaded ):
- def __init__(self, ocv):
- self.active = False
- self.ocv = ocv
- self._lenpix = 480 * 640 * 2
- self.buffer_type = ctypes.c_void_p #(ctypes.c_char * lenpix)
- self.buffer = self.buffer_type() #_ref = ctypes.cast(buff, ctypes.c_void_p)
- self.pointer = ctypes.pointer( self.buffer )
- print( 'setting leds' )
- freenect.sync_set_led( freenect.LED_YELLOW, 0 )
- def loop(self):
- print( 'starting kinect thread' )
- while self.active:
- status = freenect.sync_get_depth( ctypes.byref(self.pointer), 0, 0,
- freenect.FREENECT_DEPTH_11BIT
- )
- lock.acquire() # seems safe even without locking?
- cv.SetData( self.ocv.depth16raw, self.pointer, 640*2 )
- lock.release()
- freenect.sync_set_led( freenect.LED_OFF, 0 )
- freenect.sync_stop()
- print( 'exit kinect thread' )
- class OpenCV( Threaded ):
- def __init__(self):
- self.active = False
- self.depth16raw = cv.CreateImage((640,480), IPL_DEPTH_16S, 1)
- self.depth16 = cv.CreateImage((640,480), IPL_DEPTH_16S, 1)
- self.depth8 = cv.cvCreateImage((640,480), 8, 1)
- self.sweep_thresh = [ cv.cvCreateImage((640,480), 8, 1) for i in range(12) ]
- self.contours_image = cv.cvCreateImage((640,480), 8, 3)
- self.storage = cv.CreateMemStorage(0)
- cv.NamedWindow('depth', 1)
- cv.NamedWindow('contours', 1)
- cv.SetZero( self.depth16raw )
- def loop(self):
- print( 'starting opencv thread' )
- self.active = True
- while self.active:
- lock.acquire()
- cv.ConvertScale( self.depth16raw, self.depth8, 0.18, 0 )
- lock.release()
- ## thread free ##
- #cv.Smooth( self.depth16raw, self.depth16, cv.CV_BLUR, 7, 7, 0.1, 0.1 )
- cv.SetZero( self.contours_image )
- thresh = 100
- for i, img in enumerate(self.sweep_thresh):
- cv.Threshold( self.depth8, img, thresh, 256, cv.CV_THRESH_BINARY_INV )
- thresh += 4
- seq = cv.CvSeq()
- contours = ctypes.pointer( ctypes.pointer( seq ) )
- cv.FindContours(img, self.storage, contours,
- ctypes.sizeof( cv.Contour ), cv.CV_RETR_TREE, cv.CV_CHAIN_APPROX_SIMPLE, (0,0)
- )
- cv.DrawContours(
- self.contours_image,
- contours.contents,
- (128+thresh,0,128-thresh), # external color
- (255,255,0), # hole color
- 1, # max levels
- 2, # thickness
- 8, # linetype
- (0, 0)
- )
- cv.ShowImage( 'depth', self.depth8 )
- cv.ShowImage( 'contours', self.contours_image )
- cv.WaitKey(1)
- print( 'exit opencv thread' )
- o = OpenCV()
- k = Kinect( o )
- k.start() # thread 1
- time.sleep(1)
- o.start() # thread 2
- while True: # main thread
- try:
- print( 'waiting' )
- time.sleep(10.0)
- except:
- print( 'user exit' )
- o.active = False
- k.active = False
- break
- time.sleep(1)
- print('threaded kinect test done')
Add Comment
Please, Sign In to add comment