-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprinkles_piFlickr.py
executable file
·273 lines (224 loc) · 8.09 KB
/
sprinkles_piFlickr.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
#!/usr/bin/env python
# created by @drumminhands, edited by @srgsprinkles
# see instructions at http://www.drumminhands.com/2015/05/22/raspberry-pi-and-flickr/
import os
import glob
import time
import traceback
from time import sleep
import RPi.GPIO as GPIO
import picamera
import atexit
import sys
import socket
import pygame
import config
import flickrapi
import webbrowser
from pymouse import PyMouseEvent
from signal import alarm, signal, SIGALRM, SIGKILL
########################
### Variables Config ###
########################
ledPin = 11 # LED 1
btnPin1 = 37 # pin for the big red button
btnPin2 = 18 # pin for button to shutdown the pi
btnPin3 = 22 # pin for button to end the program, but not shutdown the pi
prepDelay = 1 # number of seconds at step 1 as users prep to have photo taken
replayDelay = 2 # how long to show the image before uploading to flickr?
doneDelay = 15 # how long to hold the done screen before restarting the process (in seconds)
testServer = 'www.google.com'
realPath = os.path.dirname(os.path.realpath(__file__))
tagsToTag = 'BrennanAndElla'
#monitorWidth = 1392;
#monitorHeight = 868;
monitorWidth = 720;
monitorHeight = 480;
offsetX = 0 # how far off to left corner to display photos
offsetY = 0 # how far off to left corner to display photos
####################
### Other Config ###
####################
GPIO.setmode(GPIO.BOARD)
GPIO.setup(ledPin,GPIO.OUT) # LED 1
GPIO.setup(btnPin1, GPIO.IN, pull_up_down=GPIO.PUD_UP) # falling edge detection on button 1
GPIO.setup(btnPin2, GPIO.IN, pull_up_down=GPIO.PUD_UP) # falling edge detection on button 2
GPIO.setup(btnPin3, GPIO.IN, pull_up_down=GPIO.PUD_UP) # falling edge detection on button 3
#################
#### Classes ####
#################
class Clickonacci(PyMouseEvent):
def __init__(self):
PyMouseEvent.__init__(self)
def click(self, x, y, button, press):
# Start app when the left click is pressed.
if button == 1:
if press:
startApp()
#################
### Functions ###
#################
def cleanup():
print('Ended abruptly')
GPIO.cleanup()
atexit.register(cleanup)
def shutItDown(channel):
print "Shutting down..."
GPIO.output(ledPin,True);
time.sleep(3)
os.system("sudo halt")
#def exitApp(channel):
def exitApp():
print "Photo booth app ended. RPi still running"
GPIO.output(ledPin,True);
time.sleep(3)
sys.exit()
def isConnected():
try:
# see if we can resolve the host name -- tells us if there is a DNS listening
host = socket.gethostbyname(testServer)
# connect to the host -- tells us if the host is actually reachable
s = socket.create_connection((host, 80), 2)
return True
except:
pass
return False
def initPygame():
pygame.init()
size = (pygame.display.Info().current_w, pygame.display.Info().current_h)
pygame.display.set_caption('Photo Booth Pics')
#pygame.mouse.set_visible(False) #hide the mouse cursor
return pygame.display.set_mode(size, pygame.FULLSCREEN)
def showImage(imagePath):
screen = initPygame()
img=pygame.image.load(imagePath)
img = pygame.transform.scale(img,(monitorWidth,monitorHeight))
screen.blit(img,(offsetX,offsetY))
pygame.display.flip()
def toUnicodeOrBust(obj, encoding='utf-8'):
if isinstance(obj, basestring):
if not isinstance(obj, unicode):
obj = unicode(obj, encoding)
return obj
def waitForBtn(t=0):
sleep(t)#wait a minimum of this many seconds
GPIO.output(ledPin,True) #turn the light on
while True: #wait for a button press
GPIO.wait_for_edge(btnPin1, GPIO.FALLING)
time.sleep(0.2) #debounce
break
def uploadToFlickr(file,tag):
connected = isConnected() #check to see if you have an internet connection
while connected:
try:
flickr = flickrapi.FlickrAPI(config.api_key, config.api_secret)
print('Step 1: authenticate')
# Only do this if we don't have a valid token already
if not flickr.token_valid(perms=u'write'):
# Get a request token
flickr.get_request_token(oauth_callback='oob')
# Open a browser at the authentication URL. Do this however
# you want, as long as the user visits that URL.
authorize_url = flickr.auth_url(perms=u'write')
webbrowser.open_new_tab(authorize_url)
# Get the verifier code from the user. Do this however you
# want, as long as the user gives the application the code.
verifier = toUnicodeOrBust(raw_input('Verifier code: '))
# Trade the request token for an access token
flickr.get_access_token(verifier)
flickr.upload(filename=file, tags=tag)
break
except ValueError:
print "Oops. No internect connection. Upload later."
try: #make a text file as a note to upload the .gif later
file = open(config.file_path + f + "-FILENOTUPLOADED.txt",'w') # Trying to create a new file or open one
file.close()
except:
print('Something went wrong. Could not write file.')
sys.exit(0) # quit Python
# define the photo taking function for when the button is pressed
def startApp():
#show the instructions
#GPIO.output(ledPin,False) #turn the light off
#showImage(realPath + "/slides/intro.png")
#showImage(realPath + "/slides/brennan-and-ella-congrats.jpg")
#waitForBtn(prepDelay)
#get ready to take pics
showImage(realPath + "/slides/blank.png")
#GPIO.output(ledPin,False) #turn the light off
with picamera.PiCamera() as camera: #use the 'with' for faster image taking
camera.resolution = (monitorWidth, monitorHeight)
camera.framerate = 30 #adjusting the framerate affects the preview image quality. Careful.
camera.vflip = True
camera.hflip = False
camera.start_preview()
time.sleep(1) #Let the camera warm up
#iterate the blink of the light in prep, also gives a little time for the camera to warm up
#GPIO.output(ledPin,True); sleep(.25)
#GPIO.output(ledPin,False); sleep(.25)
#GPIO.output(ledPin,True); sleep(.25)
#wait for a button press
# waitForBtn(0)
#countdown 3, 2, 1, and screen flashes
sleep(0.5)
showImage(realPath + "/slides/countdown3.jpg")
camera.stop_preview(); sleep(0.5)
camera.start_preview(); sleep(0.5)
showImage(realPath + "/slides/countdown2.jpg")
camera.stop_preview(); sleep(0.5)
camera.start_preview(); sleep(0.5)
showImage(realPath + "/slides/countdown1.jpg")
camera.stop_preview(); sleep(0.5)
camera.start_preview(); sleep(0.5)
showImage(realPath + "/slides/white.jpg")
camera.stop_preview(); sleep(0.5)
#take one picture
now = time.strftime("%Y-%m-%d-%H_%M_%S") #get the current date and time for the start of the filename
fileToUpload = config.file_path + now + '.jpg'
try: #take the photos
#GPIO.output(ledPin,False) #turn off the LED
camera.capture(fileToUpload);
finally:
camera.stop_preview()
camera.close()
#show the image
showImage(fileToUpload) #show the one image until flickr upload complete
time.sleep(replayDelay) #pause for a minimum amount of time
#upload to flickr
uploadToFlickr(fileToUpload,tagsToTag)
#display final screen
showImage(realPath + "/slides/brennan-and-ella-link.jpg");
time.sleep(doneDelay)
exitApp()
#start over
#GPIO.output(ledPin,True) #turn on the LED
showImage(realPath + "/slides/brennan-and-ella-congrats.jpg")
####################
### Main Program ###
####################
# when a falling edge is detected on btnPin2 and btnPin3, regardless of whatever
# else is happening in the program, their function will be run
GPIO.add_event_detect(btnPin2, GPIO.FALLING, callback=shutItDown, bouncetime=300)
#choose one of the two following lines to be un-commented
GPIO.add_event_detect(btnPin3, GPIO.FALLING, callback=exitApp, bouncetime=300) #use third button to exit python. Good while developing
# delete files in folder on startup
files = glob.glob(config.file_path + '*')
for f in files:
os.remove(f)
print "Photo booth app running..."
#light up the lights to show the app is running
#j = 1
#while j<4:
# GPIO.output(ledPin,False);
# time.sleep(0.25)
# GPIO.output(ledPin,True);
# time.sleep(0.25)
# j+=1
showImage(realPath + "/slides/brennan-and-ella-congrats.jpg")
#start the mouse click event detection
C = Clickonacci()
C.run()
#while True:
# GPIO.wait_for_edge(btnPin1, GPIO.FALLING)
# time.sleep(0.2) #debounce
# startApp()