Fixed displaying of pictures and other changes

This commit is contained in:
Balthasar Reuter
2015-06-18 00:59:32 +02:00
parent 400210b8c5
commit 21b35c6be4
3 changed files with 37 additions and 21 deletions

View File

@@ -9,17 +9,17 @@ try:
except ImportError: except ImportError:
cv_enabled = False cv_enabled = False
class CameraException(Exception): class CameraException(Exception):
"""Custom exception class to handle camera class errors""" """Custom exception class to handle camera class errors"""
pass pass
class Camera_cv: class Camera_cv:
def __init__(self): def __init__(self, picture_size):
if cv_enabled: if cv_enabled:
self.cap = cv.VideoCapture(0) self.cap = cv.VideoCapture(0)
self.cap.set(3, 640) self.cap.set(3, picture_size[0])
self.cap.set(4, 480) self.cap.set(4, picture_size[1])
def take_picture(self, filename="/tmp/picture.jpg"): def take_picture(self, filename="/tmp/picture.jpg"):
if cv_enabled: if cv_enabled:
@@ -27,13 +27,14 @@ class Camera_cv:
cv.imwrite(filename, frame) cv.imwrite(filename, frame)
return filename return filename
else: else:
return "/dev/null" raise CameraException("OpenCV not available!")
class Camera_gPhoto: class Camera_gPhoto:
"""Camera class providing functionality to take pictures using gPhoto 2""" """Camera class providing functionality to take pictures using gPhoto 2"""
# def __init__(self): def __init__(self, picture_size):
self.picture_size = picture_size
# Print the abilities of the connected camera # Print the abilities of the connected camera
# print(self.call_gphoto("-a", "/dev/null")) # print(self.call_gphoto("-a", "/dev/null"))

16
gui.py
View File

@@ -6,6 +6,8 @@
# - incorporate render_textrect # - incorporate render_textrect
# - restructure mainloop # - restructure mainloop
from __future__ import division
import pygame import pygame
try: try:
@@ -142,8 +144,11 @@ class GUI_PyGame:
def clear(self, color=(0,0,0)): def clear(self, color=(0,0,0)):
self.screen.fill(color) self.screen.fill(color)
self.surface_list = []
def apply(self): def apply(self):
for surface in self.surface_list:
self.screen.blit(surface[0], surface[1])
pygame.display.update() pygame.display.update()
def get_size(self): def get_size(self):
@@ -167,16 +172,21 @@ class GUI_PyGame:
offset = tuple(a+int((b-c)/2) for a,b,c in zip(offset, size, new_size)) offset = tuple(a+int((b-c)/2) for a,b,c in zip(offset, size, new_size))
# Apply scaling and display picture # Apply scaling and display picture
image = pygame.transform.scale(image, new_size).convert() image = pygame.transform.scale(image, new_size).convert()
self.screen.blit(image, offset) # Create surface and blit the image to it
surface = pygame.Surface(new_size)
surface.blit(image, (0,0))
self.surface_list.append((surface, offset))
def show_message(self, msg, color=(245,245,245), bg=(0,0,0)): def show_message(self, msg, color=(245,245,245), bg=(0,0,0), transparency=True):
# Choose font # Choose font
font = pygame.font.Font(None, 144) font = pygame.font.Font(None, 144)
# Create rectangle for text # Create rectangle for text
rect = pygame.Rect((0, 0, self.size[0], self.size[1])) rect = pygame.Rect((0, 0, self.size[0], self.size[1]))
# Render text # Render text
text = render_textrect(msg, font, rect, color, bg, 1, 1) text = render_textrect(msg, font, rect, color, bg, 1, 1)
self.screen.blit(text, rect.topleft) if transparency:
text.set_colorkey(bg)
self.surface_list.append((text, rect.topleft))
def wait_for_event(self): def wait_for_event(self):
# Repeat until a relevant event happened # Repeat until a relevant event happened

View File

@@ -1,21 +1,16 @@
#!/usr/bin/env python #!/usr/bin/env python
# Created by br@re-web.eu, 2015 # Created by br@re-web.eu, 2015
from __future__ import division
import os import os
from datetime import datetime from datetime import datetime
from glob import glob from glob import glob
from sys import exit from sys import exit
from time import sleep from time import sleep, clock
from PIL import Image from PIL import Image
from gui import GUI_PyGame as GuiModule from gui import GUI_PyGame as GuiModule
from camera import CameraException, Camera_gPhoto as CameraModule
from camera import Camera_gPhoto as CameraModule
from camera import CameraException
from events import Rpi_GPIO as GPIO from events import Rpi_GPIO as GPIO
##################### #####################
@@ -90,7 +85,7 @@ class Photobooth:
trigger_channel, shutdown_channel, lamp_channel): trigger_channel, shutdown_channel, lamp_channel):
self.display = GuiModule('Photobooth', display_size) self.display = GuiModule('Photobooth', display_size)
self.pictures = PictureList(picture_basename) self.pictures = PictureList(picture_basename)
self.camera = CameraModule() self.camera = CameraModule(picture_size)
self.pic_size = picture_size self.pic_size = picture_size
self.pose_time = pose_time self.pose_time = pose_time
@@ -105,6 +100,10 @@ class Photobooth:
self.gpio = GPIO(self.handle_gpio, input_channels, output_channels) self.gpio = GPIO(self.handle_gpio, input_channels, output_channels)
def teardown(self): def teardown(self):
self.display.clear()
self.display.show_message("Shutting down...")
self.display.apply()
sleep(1)
self.display.teardown() self.display.teardown()
self.gpio.teardown() self.gpio.teardown()
exit(0) exit(0)
@@ -172,7 +171,7 @@ class Photobooth:
sleep(3) sleep(3)
def assemble_pictures(self, input_filenames, output_filename): def assemble_pictures(self, input_filenames):
"""Assembles four pictures into a 2x2 grid""" """Assembles four pictures into a 2x2 grid"""
# Thumbnail size of pictures # Thumbnail size of pictures
@@ -191,7 +190,9 @@ class Photobooth:
output_image.paste(img, offset) output_image.paste(img, offset)
# Save resized image # Save resized image
output_filename = self.pictures.get_next()
output_image.save(output_filename, "JPEG") output_image.save(output_filename, "JPEG")
return output_filename
def take_picture(self): def take_picture(self):
"""Implements the picture taking routine""" """Implements the picture taking routine"""
@@ -223,7 +224,11 @@ class Photobooth:
# Take pictures # Take pictures
filenames = [i for i in range(4)] filenames = [i for i in range(4)]
for x in range(4): for x in range(4):
tic = clock()
filenames[x] = self.camera.take_picture("/tmp/photobooth_%02d.jpg" % x) filenames[x] = self.camera.take_picture("/tmp/photobooth_%02d.jpg" % x)
toc = clock() - tic
if toc < 1.0:
sleep(1.0 - toc)
# Show 'Wait' # Show 'Wait'
self.display.clear() self.display.clear()
@@ -231,8 +236,7 @@ class Photobooth:
self.display.apply() self.display.apply()
# Assemble them # Assemble them
outfile = self.pictures.get_next() outfile = self.assemble_pictures(filenames)
self.assemble_pictures(filenames, outfile)
# Show pictures for 10 seconds # Show pictures for 10 seconds
self.display.clear() self.display.clear()
@@ -254,7 +258,8 @@ def main():
photobooth = Photobooth(picture_basename, image_size, pose_time, display_time, photobooth = Photobooth(picture_basename, image_size, pose_time, display_time,
gpio_trigger_channel, gpio_shutdown_channel, gpio_lamp_channel) gpio_trigger_channel, gpio_shutdown_channel, gpio_lamp_channel)
photobooth.run() photobooth.run()
return photobooth.teardown() photobooth.teardown()
return 0
if __name__ == "__main__": if __name__ == "__main__":
exit(main()) exit(main())