Implemented Camera class that handles exceptions
This commit is contained in:
@@ -54,10 +54,19 @@ class GUI_PyGame:
|
|||||||
pygame.display.set_caption(name)
|
pygame.display.set_caption(name)
|
||||||
# Hide mouse cursor
|
# Hide mouse cursor
|
||||||
pygame.mouse.set_visible(False)
|
pygame.mouse.set_visible(False)
|
||||||
# Save objects
|
# Store screen and size
|
||||||
self.size = size
|
self.size = size
|
||||||
self.screen = pygame.display.set_mode(size)
|
self.reset()
|
||||||
|
# self.screen = pygame.display.set_mode(size)
|
||||||
#self.screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
|
#self.screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
|
||||||
|
# Set background
|
||||||
|
# self.background = pygame.Surface(self.screen.get_size())
|
||||||
|
# self.background = self.background.convert()
|
||||||
|
# self.background.fill((250, 250, 250))
|
||||||
|
|
||||||
|
def reset(self):
|
||||||
|
self.screen = pygame.display.set_mode(self.size)
|
||||||
|
self.screen.fill((255,255,255))
|
||||||
|
|
||||||
def get_size(self):
|
def get_size(self):
|
||||||
return self.size
|
return self.size
|
||||||
@@ -66,9 +75,20 @@ class GUI_PyGame:
|
|||||||
if size == (0,0):
|
if size == (0,0):
|
||||||
size = self.get_size()
|
size = self.get_size()
|
||||||
image = pygame.image.load(filename)
|
image = pygame.image.load(filename)
|
||||||
image = pygame.transform.scale(image, size)
|
image = pygame.transform.scale(image, size).convert()
|
||||||
|
# self.sprite.image = image.convert()
|
||||||
self.screen.blit(image, offset)
|
self.screen.blit(image, offset)
|
||||||
pygame.display.flip()
|
|
||||||
|
def show_message(self, msg):
|
||||||
|
font = pygame.font.Font(None, 36)
|
||||||
|
text = font.render(msg, 1, (10, 10, 10))
|
||||||
|
textpos = text.get_rect()
|
||||||
|
textpos.centerx = self.screen.get_rect().centerx
|
||||||
|
self.screen.blit(text, textpos)
|
||||||
|
|
||||||
|
def apply(self):
|
||||||
|
# pygame.display.flip()
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
def mainloop(self, filename):
|
def mainloop(self, filename):
|
||||||
while True:
|
while True:
|
||||||
@@ -76,25 +96,36 @@ class GUI_PyGame:
|
|||||||
if event.type == pygame.QUIT: return
|
if event.type == pygame.QUIT: return
|
||||||
elif event.type == pygame.KEYDOWN: handle_keypress(event.key)
|
elif event.type == pygame.KEYDOWN: handle_keypress(event.key)
|
||||||
self.show_picture(filename)
|
self.show_picture(filename)
|
||||||
|
self.show_message("Hit me!")
|
||||||
|
self.apply()
|
||||||
|
|
||||||
def teardown(self):
|
def teardown(self):
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
||||||
|
class CameraException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
class Camera:
|
class Camera:
|
||||||
"""Camera class providing functionality to take pictures"""
|
"""Camera class providing functionality to take pictures"""
|
||||||
#def __init__(self):
|
def __init__(self):
|
||||||
|
try:
|
||||||
|
print(self.call_gphoto("-a", "/dev/null"))
|
||||||
|
except CameraException as e:
|
||||||
|
handle_exception(e.message)
|
||||||
|
|
||||||
def call_gphoto(self, action, filename):
|
def call_gphoto(self, action, filename):
|
||||||
try:
|
try:
|
||||||
output = subprocess.check_output("gphoto2 --force-overwrite --quiet "
|
output = subprocess.check_output("gphoto2 --force-overwrite --quiet "
|
||||||
+ action + " --filename " + filename,
|
+ action + " --filename " + filename,
|
||||||
shell=True, stderr=subprocess.STDOUT)
|
shell=True, stderr=subprocess.STDOUT)
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
error("Error during preview when calling '" + e.cmd + "'!\nOutput: "
|
raise CameraException("Can't call gphoto2!")
|
||||||
+ e.output, e.returncode)
|
# Check for non-fatal errors
|
||||||
# Error 2019 is: cannot focus
|
if "Canon EOS Capture failed: 2019" in output:
|
||||||
if "Canon EOS Capture failed: 2019" in output: return False
|
raise CameraException("Cannot focus! Move a little bit and try again!")
|
||||||
elif "ERROR" in output: error("Error during preview!\n" + output)
|
elif "ERROR" in output:
|
||||||
return True
|
raise CameraException("Unknown error:\n" + output)
|
||||||
|
return output
|
||||||
|
|
||||||
def preview(self, filename="/tmp/preview.jpg"):
|
def preview(self, filename="/tmp/preview.jpg"):
|
||||||
while not self.call_gphoto("--capture-preview", filename):
|
while not self.call_gphoto("--capture-preview", filename):
|
||||||
@@ -128,8 +159,18 @@ def handle_keypress(key):
|
|||||||
display.show_picture(filename)
|
display.show_picture(filename)
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
|
|
||||||
|
def handle_exception(msg):
|
||||||
|
display.reset()
|
||||||
|
display.show_message("Error: " + msg)
|
||||||
|
display.apply()
|
||||||
|
time.sleep(3)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
display.mainloop(image_idle)
|
while True:
|
||||||
|
try:
|
||||||
|
display.mainloop(image_idle)
|
||||||
|
except CameraException as e:
|
||||||
|
handle_exception(e.message)
|
||||||
display.teardown()
|
display.teardown()
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
@@ -139,7 +180,7 @@ def main():
|
|||||||
|
|
||||||
display = GUI_PyGame('Photobooth', display_size)
|
display = GUI_PyGame('Photobooth', display_size)
|
||||||
camera = Camera()
|
camera = Camera()
|
||||||
images = Images(image_basename)
|
images = Images("pic")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
Reference in New Issue
Block a user