diff --git a/photobooth/Photobooth.py b/photobooth/Photobooth.py index 70e118b..8de0cbd 100644 --- a/photobooth/Photobooth.py +++ b/photobooth/Photobooth.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from time import time, sleep, localtime, strftime +# from time import time, localtime, strftime from PIL import Image, ImageOps @@ -10,6 +10,8 @@ from .PictureDimensions import PictureDimensions from . import gui +from.Worker import PictureSaver + class TeardownException(Exception): @@ -25,6 +27,8 @@ class Photobooth: self._conn = conn self._queue = queue + self._worker_list = [PictureSaver(config)] + self.initCamera(config, camera()) self.initGpio(config) @@ -36,9 +40,9 @@ class Photobooth: self._cap = camera self._pic_dims = PictureDimensions(config, self._cap.getPicture().size) - picture_basename = strftime(config.get('Picture', 'basename'), localtime()) - self._pic_list = PictureList(picture_basename) - self._get_next_filename = self._pic_list.getNext + # picture_basename = strftime(config.get('Picture', 'basename'), localtime()) + # self._pic_list = PictureList(picture_basename) + # self._get_next_filename = self._pic_list.getNext if ( config.getBool('Photobooth', 'show_preview') and self._cap.hasPreview ): @@ -103,10 +107,10 @@ class Photobooth: raise TeardownException() - @property - def getNextFilename(self): + # @property + # def getNextFilename(self): - return self._get_next_filename + # return self._get_next_filename @property @@ -202,6 +206,12 @@ class Photobooth: return output_image + def enqueueWorkerTasks(self, picture): + + for task in self._worker_list: + self._queue.put(( task.do, (picture, ) )) + + def trigger(self): self._conn.send(gui.GreeterState()) @@ -214,8 +224,10 @@ class Photobooth: self._conn.send(gui.AssembleState()) img = self.assemblePictures(pics) - img.save(self.getNextFilename(), 'JPEG') + # img.save(self.getNextFilename(), 'JPEG') self._conn.send(gui.PictureState(img)) + + self.enqueueWorkerTasks(img) self.setCameraIdle() diff --git a/photobooth/Worker.py b/photobooth/Worker.py new file mode 100644 index 0000000..61de332 --- /dev/null +++ b/photobooth/Worker.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from time import localtime, strftime + +from .PictureList import PictureList + + +class WorkerTask: + + def __init__(self, **kwargs): + + assert not kwargs + + + def do(self, picture): + + raise NotImplementedError() + + + +class PictureSaver(WorkerTask): + + def __init__(self, config): + + super().__init__() + + picture_basename = strftime(config.get('Picture', 'basename'), localtime()) + self._pic_list = PictureList(picture_basename) + self._get_next_filename = self._pic_list.getNext + + + @property + def getNextFilename(self): + + return self._get_next_filename + + + def do(self, picture): + + print('saving picture') + picture.save(self.getNextFilename(), 'JPEG') + + + +class Worker: + + def __init__(self, config, queue): + + self._queue = queue + + + def run(self): + + for func, args in iter(self._queue.get, ('teardown', None)): + func(*args) + + return 0 diff --git a/photobooth/main.py b/photobooth/main.py index 05b0cc1..b25e771 100644 --- a/photobooth/main.py +++ b/photobooth/main.py @@ -14,6 +14,7 @@ from . import camera, gui from .Config import Config from .Photobooth import Photobooth from .util import lookup_and_import +from .Worker import Worker class CameraProcess(mp.Process): @@ -76,8 +77,7 @@ class WorkerProcess(mp.Process): def run(self): - print('Started Worker') - print('Exit Worker') + sys.exit(Worker(self.cfg, self.queue).run()) class GuiProcess(mp.Process): @@ -118,8 +118,10 @@ def run(argv): gui_conn.close() camera_conn.close() - gui_proc.join() + + worker_queue.put(('teardown', None)) + worker_proc.join() camera_proc.join(5) return gui_proc.exitcode