X-Git-Url: https://svn.cri.ensmp.fr/git/minwii.git/blobdiff_plain/128d1283372aa86d58d346569a08a97d64f5acf6..5fd262589f4c98f0f37c7b9806c12446fd1f8e92:/src/kinect/pygamedisplay.py diff --git a/src/kinect/pygamedisplay.py b/src/kinect/pygamedisplay.py index e5b1e59..7b646ad 100755 --- a/src/kinect/pygamedisplay.py +++ b/src/kinect/pygamedisplay.py @@ -15,31 +15,58 @@ SCREEN_SIZE = 640, 480 SCREEN_TITLE = "Kinect debug" FPS = 30 -def capture_rgb(imgGene): - rgb_frame = numpy.fromstring(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 +class RGB : + def __init__(self) : + self.context = Context() + self.context.init() + self.imgGene = ImageGenerator() + self.imgGene.create(self.context) + self.imgGene.set_resolution_preset(RES_VGA) + self.imgGene.fps = FPS + self.context.start_generating_all() + + 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') -def main() : - # init openni - context = Context() - context.init() + return pyimage + + def update(self) : + return self.context.wait_one_update_all(self.imgGene) + + +class RGBSprite(pygame.sprite.DirtySprite, RGB) : + + def __init__(self, alpha=255, size=SCREEN_SIZE) : + pygame.sprite.DirtySprite.__init__(self) + RGB.__init__(self) + self.dirty = 2 # toujours dirty ! + self.size = size + self.image = pygame.Surface(size) + self.workSur = pygame.Surface(SCREEN_SIZE) + self.image.set_alpha(alpha) + self.rect = pygame.Rect((0, 0), (0, 0)) + + def update(self) : + RGB.update(self) + img = self.capture() + self.workSur.blit(img, (0, 0)) + self.workSur = pygame.transform.flip(self.workSur, True, False) # miroir + if self.size != SCREEN_SIZE : + pygame.transform.scale(self.workSur, self.size, self.image) # étirement, blit implicite + else : + self.image.blit(self.workSur, (0, 0)) - #init pygame + +def main() : pygame.init() screen = pygame.display.set_mode(SCREEN_SIZE) pygame.display.set_caption(SCREEN_TITLE) - imgGene = ImageGenerator() - imgGene.create(context) - imgGene.set_resolution_preset(RES_VGA) - imgGene.fps = FPS - - context.start_generating_all() - + rgb = RGB() sur = pygame.Surface((640, 480)) sur.fill((255, 255, 255)) @@ -48,12 +75,13 @@ def main() : for event in pygame.event.get(): pass - context.wait_one_update_all(imgGene) - - rgbImg = capture_rgb(imgGene) + rgb.update() + + rgbImg = rgb.capture() sur.blit(rgbImg, (0, 0)) - screen.blit(sur, (0, 0)) + screen.blit(pygame.transform.flip(sur, True, False), (0, 0)) pygame.display.flip() + if __name__ == "__main__" : main()