forked from cgart/photobooth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·606 lines (449 loc) · 17.9 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
#!/usr/bin/python
from kivy.config import Config
# ----------------------------------------------------------------------
# Settings
# ----------------------------------------------------------------------
# Debug level - This is used by Logger. Level can be critical, error, warning, info debug, notset
debuglvl = "INFO"
# Folder where all full-res captures from camera goes
#captureFilePath = "/media/odroid/B5BD-FED7/photobooth/captures/"
captureFilePath = "captures/"
# Folder with resized captures (smaller for faster loading)
#captureSnapshotPath = "/media/odroid/B5BD-FED7/photobooth/snapshots/"
captureSnapshotPath = "snapshots/"
# Filename for the preview image from the camera to be updated
capturePreviewFile = "preview.jpg"
# Width of the snapshot files
captureSnapshowWidth = 600
# Maximal texture size of the pictures displayed on the screen
pictureMaxTexSize=(512,512)
# Width of the captued image, when previewing on the screen (height is chosen according to the aspect ratio)
inspectImageWidth = 1200
# Time for the camera shutter [sec]
cameraShutterLatency = 1.2
# Path to the Imagemagick's convert executable
convertCmd = "/usr/bin/convert"
# settings of the window
#Config.set('graphics', 'fullscreen', '0')
#Config.set('graphics', 'width', '1800')
#Config.set('graphics', 'height', '800')
Config.set('graphics', 'width', '1920')
Config.set('graphics', 'height', '1080')
Config.set('graphics', 'fbo', 'hardware')
Config.set('graphics', 'fullscreen', '1')
Config.set('graphics', 'show_cursor', 0)
Config.set('graphics', 'borderless', '1')
# global values
# ----------------------------------------------------------------------
# Libraries
# ----------------------------------------------------------------------
# system librareis
import glob
from os.path import join, dirname
import piggyphoto
import time
import datetime
from threading import Thread, Lock
import numpy as np
import imp
from random import randrange, uniform, randint
from subprocess import call
# Kivy libraries
import kivy
kivy.require('1.9.1')
from kivy.app import App
from kivy.logger import Logger
from kivy.uix.widget import Widget
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.animation import Animation
from kivy.cache import Cache
from kivy.uix.effectwidget import EffectWidget, EffectBase
# Photobooth application
from mainapp.fbolayout import FboFloatLayout
from mainapp.preview import Preview
from mainapp.slothandler import CapturedSlots
from mainapp.picture import Picture
from mainapp.counter import CounterNum
from mainapp.helpers import WhiteBillboard
from mainapp.effects import ColorGlowEffect, FullScreenEffect
# Raspberry GPIO
HasRpiGPIO = False
try:
imp.find_module('RPi')
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
KEY_PIN = 40
HasRpiGPIO = True
GPIO.setup(KEY_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
print 'RaspberryPi GPIO library found'
except ImportError:
pass
class ColorGlowEffect(EffectBase):
def __init__(self, *args, **kwargs):
super(ColorGlowEffect, self).__init__(*args, **kwargs)
self.source = 'data/color_glow.glsl'
# ----------------------------------------------------------------------
class EKeyState:
PRESSED = 'pressed'
RELEASED = 'released'
# ----------------------------------------------------------------------
class EState:
LOADING = 'loading'
PREVIEW = 'preview'
COUNTER = 'countdown'
CAPTURING = 'capture'
INSPECTION = 'inspect'
SLIDESHOW = 'slideshow'
STOPSHOW = 'stop slideshow'
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
class CaptureApp(App):
touch = None
keyboard = None
camera = None
whiteBillboard = None
previewImage = None
slotImages = None
state = EState.LOADING
Logger.setLevel(debuglvl)
Logger.info('Debug Mode is %s', debuglvl)
Logger.info('State is %s', state) #Add default loading state
latestCapturedPicture = None
mutex = Lock()
keyState = EKeyState.RELEASED
backgroundEffect = None
# slideshow part
slideShowAvailableFiles = []
slideShowAvailableFileProbabilities = []
slideShowCurrentPictures = []
slideShowLastTimestamp = 0
slideShowPictureSpeed = []
# ------------------------------------------------------------------
def __init__(self, **kwargs):
super(CaptureApp, self).__init__(**kwargs)
if HasRpiGPIO == False:
self.keyboard = Window.request_keyboard(self.onKeyboardClosed, self)
self.keyboard.bind(on_key_down = self.onKeyDown)
try:
self.camera = piggyphoto.camera()
except:
self.camera = None
pass
# ------------------------------------------------------------------
def countDown(self, N, old_label, callback):
# remove old number
if old_label != None:
self.root.remove_widget(old_label)
# we reached the 0, hence start picture taking
if N == 0:
callback()
return
# add counter label
label = CounterNum(N)
label.animate()
self.root.add_widget(label)
N = N-1
if N >= 0:
Clock.schedule_once(lambda dt: self.countDown(N, label, callback), 1.0)
# ------------------------------------------------------------------
# show a flash on the screen in parallel to the capture process
def fadeIn(self, dt = 0.075, onComplete = None):
anim = Animation(alpha=1, duration=dt)
anim.start(self.whiteBillboard)
if onComplete != None:
anim.bind(on_complete = lambda a,w: onComplete())
def fadeOut(self, dt = 0.1, onComplete = None):
anim = Animation(alpha=0, duration=dt)
anim.start(self.whiteBillboard)
if onComplete != None:
anim.bind(on_complete = lambda a,w: onComplete())
# ------------------------------------------------------------------
def build(self):
self.title = 'Photobooth'
self.previewImage = self.root.ids.camera_image
self.previewImage.setCamera(capturePreviewFile, self.camera)
self.backgroundEffect = self.root.ids.background_effect
self.backgroundEffect.hide()
self.whiteBillboard = self.root.ids.white_overlay
self.slotImages = self.root.ids.picture_slots
self.slotImages.root = self.root
self.slotImages.setImageFilePath(captureSnapshotPath)
self.slotImages.picMaxTexSize = pictureMaxTexSize
# todo - this should be better called after scene graph is created and not just after some amount of time
Clock.schedule_once(lambda dt: self.preloadSlots())
if HasRpiGPIO == True:
Clock.schedule_interval(lambda dt: self.checkGPIO(), 1./20.)
pass
# ------------------------------------------------------------------
def checkGPIO(self):
# no need to debounce, since this method is called every 100ms anyway
# and storing the last state gives us a debouncing automagically
if self.keyState == EKeyState.RELEASED and GPIO.input(KEY_PIN) == 0:
self.keyState = EKeyState.PRESSED
self.userEvent() #onKeyDown(None, (32,0), None, None)
if self.keyState == EKeyState.PRESSED and GPIO.input(KEY_PIN) == 1:
self.keyState = EKeyState.RELEASED
pass
# ------------------------------------------------------------------
def onKeyboardClosed(self):
pass
# ------------------------------------------------------------------
def onKeyDown(self, keyboard, keycode, text, modifiers):
Cache.print_usage()
if keycode[0] == 32:
Logging.info('INPUT: Keyboard input detected')
self.userEvent()
Logging.info('Invoking userEvent')
pass
def on_touch_down(self, touch):
Logging.info('HANDLER: Touch received!')
# ---------------------- State Machine -----------------------------
# ------------------------------------------------------------------
# User event, performs state transitions based on the current state
# ------------------------------------------------------------------
def userEvent(self):
Logging.info('UserEvent Received. Current State is', state) # Adding logging to current state
self.mutex.acquire()
# check if we can connect to the camera
if self.camera == None:
try:
self.camera = piggyphoto.camera()
self.previewImage.setCamera(capturePreviewFile, self.camera)
except:
self.camera = None
if self.state == EState.PREVIEW:
self.mutex.release()
self.runCounter()
Logging.info('Transitioning to runCounter method')
elif self.state == EState.INSPECTION:
self.mutex.release()
self.removeLatestImage()
elif self.state == EState.SLIDESHOW:
self.mutex.release()
self.stopSlideShow()
else:
self.mutex.release()
pass
# ------------------------------------------------------------------
# Animate latest captured image to the background
# ------------------------------------------------------------------
def removeLatestImage(self):
if self.latestCapturedPicture != None:
self.root.remove_widget(self.latestCapturedPicture)
self.slotImages.addExistingImage(self.latestCapturedPicture, lambda anim,pic: self.startPreview())
self.latestCapturedPicture = None
# clear up cache
Cache.remove('kv.image')
Cache.remove('kv.texture')
Cache.remove('kv.loader')
pass
# ------------------------------------------------------------------
# Image was captured and we inspect it
# ------------------------------------------------------------------
def inspectImage(self, picture):
self.state = EState.INSPECTION
# make picture visible
picture.size = (float(inspectImageWidth), float(inspectImageWidth) * picture.aspectRatio)
picture.center_x = self.root.width / 2
picture.center_y = self.root.height / 2
self.latestCapturedPicture = picture
self.root.add_widget(picture, 1)
# animate image to the background - event
Clock.schedule_once(lambda dt: self.removeLatestImage(), 3.0)
pass
# ------------------------------------------------------------------
# capture the actual image to a file
# ------------------------------------------------------------------
def captureImageThread(self, camera, onLoadCallback):
# generate filename of the new file
timestamp = time.time()
st = datetime.datetime.fromtimestamp(timestamp).strftime('%Y%m%d_%H%M%S')
if camera == None: st = "test"
filename = captureFilePath + st + ".jpg"
print "capture image to " + filename
# capture file if camera is available
if camera != None: camera.capture_image(filename)
# generate a reduced version of the image in the snapshots folder
filename_small = captureSnapshotPath + st + ".jpg"
cmd = [convertCmd, '-geometry', str(captureSnapshowWidth) + 'x', filename, filename_small]
print "resize image: " + str(cmd)
call(cmd)
# load image and show it in the center
def _addCapturedImage():
picture = Picture(filename_small, onLoadCallback, pictureMaxTexSize)
#picture.center_x = self.root.width / 2
#picture.center_y = self.root.height / 2
if camera != None:
Clock.schedule_once(lambda dt: _addCapturedImage(), 0)
else:
Clock.schedule_once(lambda dt: _addCapturedImage(), cameraShutterLatency)
# ------------------------------------------------------------------
# Start counter for capture
# ------------------------------------------------------------------
def captureImage(self):
self.mutex.acquire()
self.state = EState.CAPTURING
Logging.info('Current state is: %s', self.state)
# disable preview images
self.previewImage.disablePreview()
# start a new thread with the actual capturing process
Thread(target=self.captureImageThread, args=(self.camera,self.inspectImage,)).start()
# start whiteout effect slightly later to fit to the shutter of the camera
Clock.schedule_once(lambda dt: self.fadeIn(0.1, self.previewImage.hide()), cameraShutterLatency)
Clock.schedule_once(lambda dt: self.fadeOut(), cameraShutterLatency + 0.2)
self.mutex.release()
pass
# ------------------------------------------------------------------
# Start counter for capture
# ------------------------------------------------------------------
def runCounter(self):
self.state = EState.COUNTER
Logger.info('Current state is %', self.state)
self.countDown(3, None, self.captureImage)
pass
# ------------------------------------------------------------------
# Show preview image from camera
# ------------------------------------------------------------------
def startPreview(self, doFadeIn = True):
# update loop for the preview frame
def _updatePreview():
self.mutex.acquire()
self.previewImage.updateFrame()
ret = True
if self.state != EState.PREVIEW and self.state != EState.COUNTER:
ret = False
self.mutex.release()
return ret
# set state and start frame updates
def _setState():
self.mutex.acquire()
self.state = EState.PREVIEW
Logger.info('State is %s', self.state) # Added logging transitition to PREVIEW
self.previewImage.show()
self.previewImage.enablePreview()
Clock.schedule_interval(lambda dt: _updatePreview(), 1.0 / 20.0)
self.mutex.release()
if doFadeIn:
anim = Animation(alpha=1.0, t='in_cubic', duration=1.0)
anim.bind(on_complete = lambda a,w: _setState())
anim.start(self.previewImage)
else:
_setState()
Clock.schedule_once(lambda dt: self.startSlideShow(), 10.)
pass
# ------------------------------------------------------------------
# Populate background with all available images
# ------------------------------------------------------------------
def preloadSlots(self):
self.state = EState.LOADING
Logger.info('State is %s', self.state) #Add default loading state
self.startPreview()
Clock.schedule_once(lambda dt: self.slotImages.preloadSlots(), 1.0)
pass
# ------------------------------------------------------------------
# Start slide show
# ------------------------------------------------------------------
def startSlideShow(self):
self.mutex.acquire()
# we can only start from a preview state
if self.state == EState.PREVIEW:
# read in all available images and assign to them uniform probabilities
self.slideShowAvailableFiles = glob.glob(captureSnapshotPath + "*.jpg")
self.slideShowAvailableFileProbabilities = [1.0 for i in xrange(len(self.slideShowAvailableFiles))]
if len(self.slideShowAvailableFiles) == 0:
self.mutex.release()
return
self.state = EState.SLIDESHOW
self.backgroundEffect.show()
# just because python does not support assignments in the ambda
def _setAlpha():
Clock.schedule_once(lambda dt: self.fadeOut(0.2), 0.2)
self.slotImages.alpha = 0
self.previewImage.hide()
self.previewImage.disablePreview()
Clock.schedule_once(lambda dt: self.fadeOut(0.2), 0.2)
# add all picture widgets
for (pic,speed) in self.slideShowCurrentPictures:
self.root.add_widget(pic,1)
Clock.schedule_interval(self.updateSlideShow, 1.0 / 40.0)
pass
Clock.schedule_once(lambda dt: self.fadeIn(0.4, _setAlpha))
self.mutex.release()
pass
# ------------------------------------------------------------------
# Stop slide show
# ------------------------------------------------------------------
def stopSlideShow(self):
self.mutex.acquire()
# slide show can only be stopped if we are in slideshow state
if self.state == EState.SLIDESHOW:
self.state = EState.STOPSHOW
# just because python does not support assignments in the lambda
def _setAlpha():
Clock.schedule_once(lambda dt: self.fadeOut(0.1))
self.slotImages.alpha = 1
#self.previewImage.show()
self.startPreview(False)
self.backgroundEffect.hide()
Clock.schedule_once(lambda dt: self.fadeOut(0.1), 0.05)
# remove all picture widgets
for (pic,speed) in self.slideShowCurrentPictures:
self.root.remove_widget(pic)
pass
Clock.schedule_once(lambda dt: self.fadeIn(0.3, _setAlpha))
self.mutex.release()
pass
# ------------------------------------------------------------------
# Update slide show
# ------------------------------------------------------------------
def updateSlideShow(self, dtime):
# do not continue with updates, if stop requested
if self.state == EState.STOPSHOW:
return False
# if enough time passed, since the last change of the image
# then add a new image into the list of currently visible images
currentTime = Clock.get_time()
if currentTime > self.slideShowLastTimestamp:
# select randomly a file from all available files
# the selected files get it's probability reduced
idx = np.random.choice(len(self.slideShowAvailableFiles), 1, self.slideShowAvailableFileProbabilities)
self.slideShowAvailableFileProbabilities[idx] *= 0.75
# create a new image from the selected file, this will be shown
def _addImageStartScrolling(pic):
width = randint(float(self.root.width)/2.5, float(self.root.width)/1.5)
pic.keep_aspect = True
pic.size = (width, width)
pic.x = uniform(-width/3., self.root.width - width + width/3.)
pic.y = -pic.size[1]
pic.rotation = uniform(-25,25)
self.slideShowCurrentPictures.append( (pic, uniform(100,200)) )
self.root.add_widget(pic,1)
pic = Picture(self.slideShowAvailableFiles[idx], _addImageStartScrolling, pictureMaxTexSize)
# repeat the process in X seconds
self.slideShowLastTimestamp = currentTime + 2.0
pass
# all images which are currently available, are scrolled through the screen
validPictures = []
for (pic,speed) in self.slideShowCurrentPictures:
pic.y += dtime * speed
if pic.y < self.root.height:
validPictures.append( (pic,speed) )
else:
def _removeImage(w):
Cache.remove('kv.image')
Cache.remove('kv.texture')
Cache.remove('kv.loader')
self.root.remove_widget(w)
anim = Animation(alpha=0, duration=0.5)
anim.bind(on_complete = lambda a,w: _removeImage(w))
anim.start(pic)
self.slideShowCurrentPictures = validPictures
# continue updates
return True
pass
# ------------------------------------------------------------------
if __name__ == '__main__':
CaptureApp().run()
if HasRpiGPIO == True:
GPIO.cleanup()