Expérimentations (concluantes) de hand tracking.
[minwii.git] / src / kinect / pygamedisplay.py
index 7b646ad..17ab173 100755 (executable)
@@ -15,7 +15,6 @@ SCREEN_SIZE = 640, 480
 SCREEN_TITLE = "Kinect debug"
 FPS = 30
 
-
 class RGB :
     def __init__(self) :
         self.context = Context()
@@ -24,18 +23,78 @@ class RGB :
         self.imgGene.create(self.context)
         self.imgGene.set_resolution_preset(RES_VGA)
         self.imgGene.fps = FPS
+        
+        self.depthGene = DepthGenerator()
+        self.depthGene.create(self.context)
+        self.depthGene.set_resolution_preset(RES_VGA)
+        self.depthGene.fps = FPS
+        
+        self.handsGene = HandsGenerator()
+        self.handsGene.create(self.context)
+        self.handsGene.register_hand_cb(self.handCreateCB, self.handUpdateCB, self.handDestroyCB)
+        
+        self.gestGene = GestureGenerator()
+        self.gestGene.create(self.context)
+        self.gestGene.add_gesture('Click')
+        self.gestGene.register_gesture_cb(self.gestureRecognizedCB, self.gestureProgressCB)
+        
+
+        self.userGene = UserGenerator()
+        self.userGene.create(self.context)
+        self.userGene.register_user_cb(self.newUserCB, self.lostUserCB)
+        
         self.context.start_generating_all()
+        
+        screen = pygame.display.get_surface()
+        self.xratio = float(screen.get_size()[0]) / SCREEN_SIZE[0]
+        self.yratio = float(screen.get_size()[1]) / SCREEN_SIZE[1]
+    
+    def getProjPos(self, realPos) :
+        return self.depthGene.to_projective([realPos])[0]
+    
+    def setMousePosition(self, realPos) :
+        x, y, z = self.getProjPos(realPos)
+        # mirror
+        x = SCREEN_SIZE[0] - x
+        # scale
+        x, y = x * self.xratio, y * self.yratio
+        pygame.mouse.set_pos(int(x), int(y))
+
+    def handCreateCB(self, src, id, pos, time):
+        print 'Create ', id, pos
+
+    def handUpdateCB(self, src, id, pos, time):
+        self.setMousePosition(pos)
+
+    def handDestroyCB(self, src, id, time):
+        print 'Destroy ', id
+
+    def gestureRecognizedCB(self, src, gesture, id, end_point) :
+        print 'gestureDetected', src, gesture, id, end_point
+
+    def gestureProgressCB(self, src, gesture, point, progress) :
+        print 'gestureProgress', src, gesture, point, progress
+        self.handsGene.start_generating()
+        self.handsGene.start_tracking(point)
+
+    def newUserCB(self, *args, **kw) :
+        print 'newUserCB', args, kw
+
+    def lostUserCB(self, *args, **kw) :
+        print 'lostUserCB', args, kw
+    
     
     def capture(self) :
         rgb_frame = numpy.fromstring(self.imgGene.get_raw_image_map_bgr(), dtype=numpy.uint8).reshape(480, 640, 3)
         image = cv.fromarray(rgb_frame)
         cv.CvtColor(cv.fromarray(rgb_frame), image, cv.CV_BGR2RGB)
         pyimage = pygame.image.frombuffer(image.tostring(), cv.GetSize(image), 'RGB')
-
         return pyimage
     
     def update(self) :
-        return self.context.wait_one_update_all(self.imgGene)
+        self.context.wait_any_update_all()
+        #self.context.wait_and_update_all()
+        #return self.context.wait_one_update_all(self.imgGene)
 
 
 class RGBSprite(pygame.sprite.DirtySprite, RGB) :