From 4ebc825781a54e19ad0214aebad03c65516364e0 Mon Sep 17 00:00:00 2001 From: Guillaume Dott Date: Sat, 30 Sep 2017 21:49:20 +0200 Subject: [PATCH] Test camera streaming --- photoboite.py | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/photoboite.py b/photoboite.py index 0d4d437..d1af037 100755 --- a/photoboite.py +++ b/photoboite.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 -import os, sys, time, pygame +import io, os, sys, time, pygame -# import picamera +import picamera class Button: def __init__(self, size, topleft): @@ -108,22 +108,27 @@ class Camera: def __init__(self, rect): self.rect = rect self.img = pygame.transform.scale(pygame.image.load('tournesol.jpg'), (self.rect.width, self.rect.height)) - # self.camera = picamera.PiCamera() - # self.camera.led = False - # self.camera.resolution = (self.rect.width, self.rect.height) + self.camera = picamera.PiCamera() + self.camera.led = False + self.camera.resolution = (self.rect.width, self.rect.height) + self.rgb = bytearray(self.rect.width * self.rect.height * 4) + + def close(self): + self.camera.close() def take(self, name): - # self.camera.capture(name + '.jpg') + self.camera.capture(name + '.jpg') print('Take picture! ' + name) def stream(self): - return self.img - # stream = io.BytesIO() - # camera.capture(stream, use_video_port=True, format='rgb', rezise=(self.rect.width, self.rect.height)) - # img = pygame.image.frombuffer(stream, (self.rect.width, self.rect.height), 'RGB') - # stream.close() + stream = io.BytesIO() + self.camera.capture(stream, use_video_port=True, format='rgba', resize=(self.rect.width, self.rect.height)) + stream.seek(0) + stream.readinto(self.rgb) + stream.close() + img = pygame.image.frombuffer(self.rgb, (self.rect.width, self.rect.height), 'RGBA') - # return img + return pygame.transform.flip(img, True, False) def draw_on(self, screen): screen.blit(self.stream(), self.rect) @@ -136,15 +141,14 @@ class Photoboite: pygame.mouse.set_visible(False) - self.background = 255, 255, 255 self.buttons = [] - button_size = int(self.screen.get_width() / 4) - topleft = self.screen.get_width() - button_size - 20, self.screen.get_height() / 4 - button_size / 2 + button_size = int(self.screen.get_height() / 4) + topleft = (self.screen.get_width() - button_size) / 2, self.screen.get_height() - button_size - 10 self.buttons.append(CaptureButton(button_size, topleft, self)) - camera_rect = pygame.Rect(20, 20, self.screen.get_width() - self.screen.get_width() / 4 - 60, self.screen.get_height() - 40) + camera_rect = pygame.Rect(0, 0, 800, 480) self.camera = Camera(camera_rect) self.font = pygame.font.Font(pygame.font.match_font('calibri'), 180) @@ -156,7 +160,7 @@ class Photoboite: if os.getenv('DISPLAY'): pygame.display.init() display = pygame.display.Info() - return pygame.display.set_mode((min(1280,display.current_w), min(720,display.current_h))) + return pygame.display.set_mode((800, 480)) else: drivers = ['directfb', 'fbcon', 'svgalib'] found = False @@ -179,8 +183,6 @@ class Photoboite: def draw(self): self.clock.tick(60) - self.screen.fill(self.background) - self.camera.draw_on(self.screen) for button in self.buttons: @@ -208,7 +210,7 @@ class Photoboite: pygame.display.flip() def quit(self): - # self.camera.close() + self.camera.close() sys.exit() Photoboite().run()