Test camera streaming
parent
031796401a
commit
4ebc825781
|
@ -1,8 +1,8 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import os, sys, time, pygame
|
import io, os, sys, time, pygame
|
||||||
|
|
||||||
# import picamera
|
import picamera
|
||||||
|
|
||||||
class Button:
|
class Button:
|
||||||
def __init__(self, size, topleft):
|
def __init__(self, size, topleft):
|
||||||
|
@ -108,22 +108,27 @@ class Camera:
|
||||||
def __init__(self, rect):
|
def __init__(self, rect):
|
||||||
self.rect = rect
|
self.rect = rect
|
||||||
self.img = pygame.transform.scale(pygame.image.load('tournesol.jpg'), (self.rect.width, self.rect.height))
|
self.img = pygame.transform.scale(pygame.image.load('tournesol.jpg'), (self.rect.width, self.rect.height))
|
||||||
# self.camera = picamera.PiCamera()
|
self.camera = picamera.PiCamera()
|
||||||
# self.camera.led = False
|
self.camera.led = False
|
||||||
# self.camera.resolution = (self.rect.width, self.rect.height)
|
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):
|
def take(self, name):
|
||||||
# self.camera.capture(name + '.jpg')
|
self.camera.capture(name + '.jpg')
|
||||||
print('Take picture! ' + name)
|
print('Take picture! ' + name)
|
||||||
|
|
||||||
def stream(self):
|
def stream(self):
|
||||||
return self.img
|
stream = io.BytesIO()
|
||||||
# stream = io.BytesIO()
|
self.camera.capture(stream, use_video_port=True, format='rgba', resize=(self.rect.width, self.rect.height))
|
||||||
# camera.capture(stream, use_video_port=True, format='rgb', rezise=(self.rect.width, self.rect.height))
|
stream.seek(0)
|
||||||
# img = pygame.image.frombuffer(stream, (self.rect.width, self.rect.height), 'RGB')
|
stream.readinto(self.rgb)
|
||||||
# stream.close()
|
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):
|
def draw_on(self, screen):
|
||||||
screen.blit(self.stream(), self.rect)
|
screen.blit(self.stream(), self.rect)
|
||||||
|
@ -136,15 +141,14 @@ class Photoboite:
|
||||||
|
|
||||||
pygame.mouse.set_visible(False)
|
pygame.mouse.set_visible(False)
|
||||||
|
|
||||||
self.background = 255, 255, 255
|
|
||||||
self.buttons = []
|
self.buttons = []
|
||||||
|
|
||||||
button_size = int(self.screen.get_width() / 4)
|
button_size = int(self.screen.get_height() / 4)
|
||||||
topleft = self.screen.get_width() - button_size - 20, self.screen.get_height() / 4 - button_size / 2
|
topleft = (self.screen.get_width() - button_size) / 2, self.screen.get_height() - button_size - 10
|
||||||
|
|
||||||
self.buttons.append(CaptureButton(button_size, topleft, self))
|
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.camera = Camera(camera_rect)
|
||||||
|
|
||||||
self.font = pygame.font.Font(pygame.font.match_font('calibri'), 180)
|
self.font = pygame.font.Font(pygame.font.match_font('calibri'), 180)
|
||||||
|
@ -156,7 +160,7 @@ class Photoboite:
|
||||||
if os.getenv('DISPLAY'):
|
if os.getenv('DISPLAY'):
|
||||||
pygame.display.init()
|
pygame.display.init()
|
||||||
display = pygame.display.Info()
|
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:
|
else:
|
||||||
drivers = ['directfb', 'fbcon', 'svgalib']
|
drivers = ['directfb', 'fbcon', 'svgalib']
|
||||||
found = False
|
found = False
|
||||||
|
@ -179,8 +183,6 @@ class Photoboite:
|
||||||
def draw(self):
|
def draw(self):
|
||||||
self.clock.tick(60)
|
self.clock.tick(60)
|
||||||
|
|
||||||
self.screen.fill(self.background)
|
|
||||||
|
|
||||||
self.camera.draw_on(self.screen)
|
self.camera.draw_on(self.screen)
|
||||||
|
|
||||||
for button in self.buttons:
|
for button in self.buttons:
|
||||||
|
@ -208,7 +210,7 @@ class Photoboite:
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
def quit(self):
|
def quit(self):
|
||||||
# self.camera.close()
|
self.camera.close()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
Photoboite().run()
|
Photoboite().run()
|
||||||
|
|
Loading…
Reference in New Issue