Idle functionality for camera added. Some Photobooth config added to settings file and dialog.
This commit is contained in:
@@ -19,22 +19,32 @@ from time import time, sleep, localtime, strftime
|
||||
|
||||
output_size = (1920, 1080)
|
||||
min_distance = (10, 10)
|
||||
num_pictures = (2, 2)
|
||||
# num_pictures = (2, 2)
|
||||
pose_time = 2
|
||||
picture_basename = strftime('%Y-%m-%d/photobooth', localtime())
|
||||
|
||||
class Photobooth:
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, config):
|
||||
|
||||
self._cap = Camera()
|
||||
self._cfg = config
|
||||
|
||||
if ( self._cfg.getBool('Photobooth', 'show_preview')
|
||||
and self._cap.hasPreview ):
|
||||
self.showCounter = self.showCounterPreview
|
||||
else:
|
||||
self.showCounter = self.showCounterNoPreview
|
||||
|
||||
self.numPictures = ( self._cfg.getInt('Photobooth', 'num_pictures_x') ,
|
||||
self._cfg.getInt('Photobooth', 'num_pictures_y') )
|
||||
|
||||
@property
|
||||
def getNextFilename(self):
|
||||
|
||||
return self._get_next_filename
|
||||
|
||||
|
||||
@getNextFilename.setter
|
||||
def getNextFilename(self, func):
|
||||
|
||||
@@ -44,6 +54,36 @@ class Photobooth:
|
||||
self._get_next_filename = func
|
||||
|
||||
|
||||
@property
|
||||
def showCounter(self):
|
||||
|
||||
return self._show_counter
|
||||
|
||||
|
||||
@showCounter.setter
|
||||
def showCounter(self, func):
|
||||
|
||||
if not callable(func):
|
||||
raise ValueError('showCounter must be callable')
|
||||
|
||||
self._show_counter = func
|
||||
|
||||
|
||||
@property
|
||||
def numPictures(self):
|
||||
|
||||
return self._num_pictures
|
||||
|
||||
|
||||
@numPictures.setter
|
||||
def numPictures(self, num_pictures):
|
||||
|
||||
if len(num_pictures) != 2:
|
||||
raise ValueError('num_pictures must have two entries')
|
||||
|
||||
self._num_pictures = num_pictures
|
||||
|
||||
|
||||
def run(self, send, recv):
|
||||
|
||||
self._send = send
|
||||
@@ -61,22 +101,35 @@ class Photobooth:
|
||||
self._send.send( Gui.ErrorState('Camera error', str(e)) )
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def setCameraActive(self):
|
||||
|
||||
self._cap.setActive()
|
||||
|
||||
|
||||
def setCameraIdle(self):
|
||||
|
||||
if self._cap.hasIdle:
|
||||
self._cap.setIdle()
|
||||
|
||||
|
||||
def showCounter(self):
|
||||
def showCounterPreview(self):
|
||||
|
||||
if self._cap.hasPreview:
|
||||
tic, toc = time(), 0
|
||||
tic, toc = time(), 0
|
||||
|
||||
while toc < pose_time:
|
||||
self._send.send( Gui.PreviewState(
|
||||
message = str(pose_time - int(toc)),
|
||||
picture = ImageOps.mirror(self._cap.getPreview()) ) )
|
||||
toc = time() - tic
|
||||
else:
|
||||
for i in range(pose_time):
|
||||
self._send.send( Gui.PreviewState(str(i)) )
|
||||
sleep(1)
|
||||
while toc < pose_time:
|
||||
self._send.send( Gui.PreviewState(
|
||||
message = str(pose_time - int(toc)),
|
||||
picture = ImageOps.mirror(self._cap.getPreview()) ) )
|
||||
toc = time() - tic
|
||||
|
||||
|
||||
def showCounterNoPreview(self):
|
||||
|
||||
for i in range(pose_time):
|
||||
self._send.send( Gui.PreviewState(str(i)) )
|
||||
sleep(1)
|
||||
|
||||
|
||||
def captureSinglePicture(self):
|
||||
@@ -88,23 +141,24 @@ class Photobooth:
|
||||
|
||||
def assemblePictures(self, pictures):
|
||||
|
||||
# TODO: determine sizes only once
|
||||
picture_size = pictures[0].size
|
||||
|
||||
resize_factor = min( ( (
|
||||
( output_size[i] - (num_pictures[i] + 1) * min_distance[i] ) /
|
||||
( num_pictures[i] * picture_size[i]) ) for i in range(2) ) )
|
||||
( output_size[i] - (self.numPictures[i] + 1) * min_distance[i] ) /
|
||||
( self.numPictures[i] * picture_size[i]) ) for i in range(2) ) )
|
||||
|
||||
output_picture_size = tuple( int(picture_size[i] * resize_factor)
|
||||
for i in range(2) )
|
||||
output_picture_dist = tuple( ( output_size[i] - num_pictures[i] *
|
||||
output_picture_size[i] ) // (num_pictures[i] + 1)
|
||||
output_picture_dist = tuple( ( output_size[i] - self.numPictures[i] *
|
||||
output_picture_size[i] ) // (self.numPictures[i] + 1)
|
||||
for i in range(2) )
|
||||
|
||||
output_image = Image.new('RGB', output_size, (255, 255, 255))
|
||||
|
||||
idx = 0
|
||||
for img in pictures:
|
||||
pos = (idx % num_pictures[0], idx // num_pictures[0])
|
||||
pos = (idx % self.numPictures[0], idx // self.numPictures[0])
|
||||
img = img.resize(output_picture_size)
|
||||
offset = tuple( (pos[i] + 1) * output_picture_dist[i] +
|
||||
pos[i] * output_picture_size[i] for i in range(2) )
|
||||
@@ -117,13 +171,14 @@ class Photobooth:
|
||||
def capturePictures(self):
|
||||
|
||||
pictures = [self.captureSinglePicture()
|
||||
for i in range(2) for _ in range(num_pictures[i])]
|
||||
for i in range(2) for _ in range(self.numPictures[i])]
|
||||
return self.assemblePictures(pictures)
|
||||
|
||||
|
||||
def trigger(self):
|
||||
|
||||
self._send.send(Gui.PoseState())
|
||||
self.setCameraActive()
|
||||
|
||||
sleep(2)
|
||||
|
||||
@@ -131,16 +186,18 @@ class Photobooth:
|
||||
img.save(self.getNextFilename(), 'JPEG')
|
||||
self._send.send(Gui.PictureState(img))
|
||||
|
||||
self.setCameraIdle()
|
||||
|
||||
sleep(5)
|
||||
|
||||
self._send.send(Gui.IdleState())
|
||||
|
||||
|
||||
def main_photobooth(send, recv):
|
||||
def main_photobooth(config, send, recv):
|
||||
|
||||
picture_list = PictureList(picture_basename)
|
||||
|
||||
photobooth = Photobooth()
|
||||
photobooth = Photobooth(config)
|
||||
photobooth.getNextFilename = picture_list.getNext
|
||||
|
||||
return photobooth.run(send, recv)
|
||||
@@ -153,7 +210,7 @@ def main(argv):
|
||||
event_recv, event_send = Pipe(duplex=False)
|
||||
gui_recv, gui_send = Pipe(duplex=False)
|
||||
|
||||
photobooth = Process(target=main_photobooth, args=(gui_send, event_recv), daemon=True)
|
||||
photobooth = Process(target=main_photobooth, args=(config, gui_send, event_recv), daemon=True)
|
||||
photobooth.start()
|
||||
|
||||
gui = PyQt5Gui(argv, config)
|
||||
|
||||
Reference in New Issue
Block a user