82621eac7dc241f53f2b8f6b243dfe74fe72a7f0
[minwii.git] / src / kinect / pygamedisplay.py
1 # -*- coding: utf-8 -*-
2 """
3 Affichage vidéo (et autres) de la kinect pour expérimentations / debug.
4
5 $Id$
6 $URL$
7 """
8
9 from openni import *
10 import numpy
11 import cv
12 import pygame
13
14 SCREEN_SIZE = 640, 480
15 SCREEN_TITLE = "Kinect debug"
16 FPS = 30
17
18
19 class RGB :
20 def __init__(self) :
21 self.context = Context()
22 self.context.init()
23 self.imgGene = ImageGenerator()
24 self.imgGene.create(self.context)
25 self.imgGene.set_resolution_preset(RES_VGA)
26 self.imgGene.fps = FPS
27 self.context.start_generating_all()
28
29 def capture(self) :
30 rgb_frame = numpy.fromstring(self.imgGene.get_raw_image_map_bgr(), dtype=numpy.uint8).reshape(480, 640, 3)
31 image = cv.fromarray(rgb_frame)
32 cv.CvtColor(cv.fromarray(rgb_frame), image, cv.CV_BGR2RGB)
33 pyimage = pygame.image.frombuffer(image.tostring(), cv.GetSize(image), 'RGB')
34
35 return pyimage
36
37 def update(self) :
38 return self.context.wait_one_update_all(self.imgGene)
39
40
41
42 def main() :
43 pygame.init()
44 screen = pygame.display.set_mode(SCREEN_SIZE)
45 pygame.display.set_caption(SCREEN_TITLE)
46
47 rgb = RGB()
48
49 sur = pygame.Surface((640, 480))
50 sur.fill((255, 255, 255))
51
52 while True :
53 for event in pygame.event.get():
54 pass
55
56 rgb.update()
57
58 rgbImg = rgb.capture()
59 sur.blit(rgbImg, (0, 0))
60 screen.blit(pygame.transform.flip(sur, True, False), (0, 0))
61 pygame.display.flip()
62
63
64 if __name__ == "__main__" :
65 main()