/
boothcam.py
194 lines (169 loc) · 7.49 KB
/
boothcam.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
from subprocess import call
import tkFileDialog
import glob
import os
import os.path
import time
try:
import picamera as mycamera
except ImportError:
import cv2_camera as mycamera
from time import sleep
if False:
import gdata
import gdata.photos.service
import gdata.media
import gdata.geo
import gdata.gauth
import webbrowser
from datetime import datetime, timedelta
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from credentials import OAuth2Login
from PIL import Image
import serial
import config
import custom
import httplib2
from constants import SCREEN_W, SCREEN_H, WHITE, BLACK
FONTSIZE=100
font = ('Times', FONTSIZE)
def safe_set_led(camera, state):
try:
camera.led = state
except:
pass
def setup_google():
global client
out = True
try:
# Create a client class which will make HTTP requests with Google Docs server.
configdir = os.path.expanduser('./')
client_secrets = os.path.join(configdir, 'OpenSelfie.json')
credential_store = os.path.join(configdir, 'credentials.dat')
client = OAuth2Login(client_secrets, credential_store, config.username)
except KeyboardInterrupt:
raise
except Exception, e:
print 'could not login to Google, check .credential file\n %s' % e
out = False
# raise ### uncomment to debug google oauth shiz
return out
def countdown(camera, can, countdown1):
camera.start_preview()
# camera.start_preview(fullscreen=False,
# crop=(50, 150, 800, 480),
# window=(0, 0, 800, 480),
# hflip=True)
can.delete("image")
led_state = False
safe_set_led(camera, led_state)
camera.preview_alpha = 100
camera.preview_window = (0, 0, SCREEN_W, SCREEN_H)
camera.preview_fullscreen = False
can.delete("all")
for i in range(countdown1):
can.delete("text")
can.update()
can.create_text(SCREEN_W/2 - 0, 200, text=str(countdown1 - i), font=font, tags="text")
can.update()
if i < countdown1 - 2:
time.sleep(1)
led_state = not led_state
safe_set_led(camera, led_state)
else:
for j in range(5):
time.sleep(.2)
led_state = not led_state
safe_set_led(camera, led_state)
can.delete("text")
can.update()
camera.stop_preview()
def setLights(r, g, b):
# ser = findser()
rgb_command = 'c%s%s%s' % (chr(r), chr(g), chr(b))
# ser.write(rgb_command)
def snap(can, countdown1, effect='None'):
global image_idx
try:
if custom.ARCHIVE and os.path.exists(custom.archive_dir) and os.path.exists(custom.PROC_FILENAME):
### copy image to archive
image_idx += 1
new_filename = os.path.join(custom.archive_dir, '%s_%05d.%s' % (custom.PROC_FILENAME[:-4], image_idx, custom.EXT))
command = (['cp', custom.PROC_FILENAME, new_filename])
call(command)
camera = mycamera.PiCamera()
countdown(camera, can, countdown1)
if effect == 'None':
camera.capture(custom.RAW_FILENAME, resize=(1366, 768))
snapshot = Image.open(custom.RAW_FILENAME)
elif effect == 'Warhol':
# set light to R, take photo, G, take photo, B, take photo, Y, take photo
# merge results into one image
setLights(255, 0, 0) ## RED
camera.capture(custom.RAW_FILENAME[:-4] + '_1.' + custom.EXT, resize=(683, 384))
setLights(0, 255, 0) ## GREEN
camera.capture(custom.RAW_FILENAME[:-4] + '_2.' + custom.EXT, resize=(683, 384))
setLights(0, 0, 255) ## BLUE
camera.capture(custom.RAW_FILENAME[:-4] + '_3.' + custom.EXT, resize=(683, 384))
setLights(180, 180, 0) ## yellow of same intensity
camera.capture(custom.RAW_FILENAME[:-4] + '_4.' + custom.EXT, resize=(683, 384))
snapshot = Image.new('RGBA', (1366, 768))
snapshot.paste(Image.open(custom.RAW_FILENAME[:-4] + '_1.' + custom.EXT).resize((683, 384)), ( 0, 0, 683, 384))
snapshot.paste(Image.open(custom.RAW_FILENAME[:-4] + '_2.' + custom.EXT).resize((683, 384)), (683, 0, 1366, 384))
snapshot.paste(Image.open(custom.RAW_FILENAME[:-4] + '_3.' + custom.EXT).resize((683, 384)), ( 0, 384, 683, 768))
snapshot.paste(Image.open(custom.RAW_FILENAME[:-4] + '_4.' + custom.EXT).resize((683, 384)), (683, 384, 1366, 768))
elif effect == "Four":
# take 4 photos and merge into one image.
camera.capture(custom.RAW_FILENAME[:-4] + '_1.' + custom.EXT, resize=(683, 384))
countdown(camera, can, custom.countdown2)
camera.capture(custom.RAW_FILENAME[:-4] + '_2.' + custom.EXT, resize=(683, 384))
countdown(camera, can, custom.countdown2)
camera.capture(custom.RAW_FILENAME[:-4] + '_3.' + custom.EXT, resize=(683, 384))
countdown(camera, can, custom.countdown2)
camera.capture(custom.RAW_FILENAME[:-4] + '_4.' + custom.EXT, resize=(683, 384))
snapshot = Image.new('RGBA', (1366, 768))
snapshot.paste(Image.open(custom.RAW_FILENAME[:-4] + '_1.' + custom.EXT).resize((683, 384)), ( 0, 0, 683, 384))
snapshot.paste(Image.open(custom.RAW_FILENAME[:-4] + '_2.' + custom.EXT).resize((683, 384)), (683, 0, 1366, 384))
snapshot.paste(Image.open(custom.RAW_FILENAME[:-4] + '_3.' + custom.EXT).resize((683, 384)), ( 0, 384, 683, 768))
snapshot.paste(Image.open(custom.RAW_FILENAME[:-4] + '_4.' + custom.EXT).resize((683, 384)), (683, 384, 1366, 768))
camera.close()
if custom.logo is not None:
# snapshot.paste(logo,(0,SCREEN_H -lysize ),logo)
# snapshot.paste(custom.logo,(SCREEN_W/2 - custom.logo.size[0]/2,
# SCREEN_H -custom.lysize ),
# custom.logo)
size = snapshot.size
logo_size = custom.logo.size
yoff = size[1] - logo_size[1]
xoff = (size[0] - logo_size[0]) // 2
snapshot.paste(custom.logo,(xoff, yoff),
custom.logo)
snapshot.save(custom.PROC_FILENAME)
except Exception, e:
print e
snapshot = None
return snapshot
snap.active = False
if custom.ARCHIVE: ### commented out... use custom.customizer instead
# custom.archive_dir = tkFileDialog.askdirectory(title="Choose archive directory.", initialdir='/media/')
if not os.path.exists(custom.archive_dir):
print 'Directory not found. Not archiving'
custom.ARCHIVE = False
elif not os.path.exists(custom.archive_dir): ## not used
os.mkdir(custom.archive_dir)
image_idx = len(glob.glob(os.path.join(custom.archive_dir, '%s_*.%s' % (custom.PROC_FILENAME[:-4], custom.EXT))))
SERIAL = None
def findser():
global SERIAL
if SERIAL is None: ## singleton
SERIAL = serial.Serial('/dev/ttyS0',19200, timeout=.1)
print 'using AlaMode'
return SERIAL
def googleUpload(filen):
#upload to picasa album
if custom.albumID != 'None':
album_url ='/data/feed/api/user/%s/albumid/%s' % (config.username, custom.albumID)
photo = client.InsertPhotoSimple(album_url,'NoVa Snap',custom.photoCaption, filen ,content_type='image/jpeg')
else:
raise ValueError("albumID not set")