-
Notifications
You must be signed in to change notification settings - Fork 3
/
PiGlassBetaTesting.py
388 lines (354 loc) · 12.1 KB
/
PiGlassBetaTesting.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
from subprocess import call
import RPi.GPIO as GPIO
import picamera
import time
import sys
import datetime
import cv2
import numpy as np
import KeyboardPoller
import subprocess
import thread
import re
height = 600
width = 800
alphaValue = 75
o = None
recording = 0
buttoncounter = 0
camera = picamera.PiCamera()
global videoFile
global zoomcount
zoomcount=0
globalCounter = 0
global roi
roi = 0
def initialize_camera():
camera.resolution = (width, height)
camera.sharpness = 0
camera.contrast = 0
camera.brightness = 50
camera.saturation = 0
camera.ISO = 0
camera.video_stabilization = True
camera.exposure_compensation = 0
camera.exposure_mode = 'auto'
camera.meter_mode = 'average'
camera.awb_mode = 'auto'
camera.image_effect = 'none'
camera.color_effects = None
camera.rotation = -90
camera.hflip = False
camera.vflip = False
camera.start_preview()
print "Camera is configured and outputting video..."
if (width%32) > 0 or (height%16) > 0:
print "Rounding down set resolution to match camera block size:"
width = width-(width%32)
height = height-(height%16)
print "New resolution: " + str(width) + "x" + str(height)
ovl = np.zeros((height, width, 3), dtype=np.uint8)
globalz = {
'zoom_step' : 0.03,
'zoom_xy_min' : 0.0,
'zoom_xy' : 0.0,
'zoom_xy_max' : 0.4,
'zoom_wh_min' : 1.0,
'zoom_wh' : 1.0,
'zoom_wh_max' : 0.2,
}
def update_zoom():
global roi
#print roi
#print str(roi)[1:-1]
roi = str(globalz['zoom_xy'])[:6], str(globalz['zoom_xy'])[:6], str(globalz['zoom_wh'])[:6], str(globalz['zoom_wh'])[:6]
print roi
camera.zoom = (globalz['zoom_xy'], globalz['zoom_xy'], globalz['zoom_wh'], globalz['zoom_wh'])
print "Camera at (x, y, w, h) = ", camera.zoom
def set_min_zoom():
globalz['zoom_xy'] = globalz['zoom_xy_min']
globalz['zoom_wh'] = globalz['zoom_wh_min']
def set_max_zoom():
globalz['zoom_xy'] = globalz['zoom_xy_max']
globalz['zoom_wh'] = globalz['zoom_wh_max']
def zoom_out():
global zoomcount
if globalz['zoom_xy'] - globalz['zoom_step'] < globalz['zoom_xy_min']:
set_min_zoom()
else:
globalz['zoom_xy'] -= globalz['zoom_step']
globalz['zoom_wh'] += (globalz['zoom_step'] * 2)
zoomcount = zoomcount - 1
update_zoom()
def zoom_in():
global zoomcount
if globalz['zoom_xy'] + globalz['zoom_step'] > globalz['zoom_xy_max']:
set_max_zoom()
else:
zoomcount = zoomcount + 1
globalz['zoom_xy'] += globalz['zoom_step']
globalz['zoom_wh'] -= (globalz['zoom_step'] * 2)
update_zoom()
ovl = np.zeros((height, width, 3), dtype=np.uint8)
# initial config for gpio ports
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
colors = {
'white': (255,255,255),
'red': (255,0,0),
'green': (0,255,0),
'blue': (0,0,255),
'yellow': (255,255,0),
}
def colormap(col):
return colors.get(col, (255,255,255))
col = colormap('white')
font = cv2.FONT_HERSHEY_PLAIN
guivisible = 1
togsw = 1
guiOn = 1
gui = np.zeros((height, width, 3), dtype=np.uint8)
gui1 = 'PiGlass'
gui2 = 'Version 0.5 alpha'
gui3 = 'P Key = take pic'
gui4 = 'V Key = take video'
gui5 = ' '
def get_file_name_pic(): # new
return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.jpg")
def get_file_name_vid(): # new
return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")
def creategui(target):
global gui5
cv2.putText(target, gui1, (10,height-160), font, 10, col, 6)
cv2.putText(target, gui2, (10,height-130), font, 3, col, 3)
cv2.putText(target, gui3, (10,height-90), font, 3, col, 3)
cv2.putText(target, gui4, (10,height-50), font, 3, col, 3)
cv2.putText(target, gui5, (10,height-10), font, 3, colormap("green"), 3)
return
def patternswitch(target,guitoggle):
global o, alphaValue
toggleonoff()
if guitoggle == 1:
creategui(gui)
o = camera.add_overlay(np.getbuffer(target), layer=3, alpha=alphaValue)
return
def patternswitcherRecord(target,guitoggle):
global o, zoomcount, ycenter
if guitoggle == 1:
creategui(gui)
# function
def togglepatternRecord():
global togsw,o,curpat,col,ovl,gui,alphaValue,ycenter,zoomcount
# if overlay is inactive, ignore button:
if togsw == 0:
print "Pattern button pressed, but ignored --- Crosshair not visible."
else:
if guivisible == 0:
ovl = np.zeros((height, width, 3), dtype=np.uint8)
patternswitcherRecord(ovl,0)
else:
gui = np.zeros((height, width, 3), dtype=np.uint8)
creategui(gui)
patternswitcherRecord(gui,1)
return
def togglepattern():
global togsw,o,ovl,gui,alphaValue
# if overlay is inactive, ignore button:
if togsw == 0:
print "Pattern button pressed, but ignored --- Crosshair not visible."
# if overlay is active, drop it, change pattern, then show it again
else:
if guivisible == 0:
# reinitialize array:
ovl = np.zeros((height, width, 3), dtype=np.uint8)
patternswitch(ovl,0)
if o != None:
camera.remove_overlay(o)
o = camera.add_overlay(np.getbuffer(ovl), layer=3, alpha=alphaValue)
else:
# reinitialize array
gui = np.zeros((height, width, 3), dtype=np.uint8)
creategui(gui)
patternswitch(gui,1)
if o != None:
camera.remove_overlay(o)
o = camera.add_overlay(np.getbuffer(gui), layer=3, alpha=alphaValue)
return
def toggleonoff():
global togsw,o,alphaValue
if togsw == 1:
print "Toggle Crosshair OFF"
if o != None:
camera.remove_overlay(o)
togsw = 0
else:
print "Toggle Crosshair ON"
if guivisible == 0:
o = camera.add_overlay(np.getbuffer(ovl), layer=3, alpha=alphaValue)
else:
o = camera.add_overlay(np.getbuffer(gui), layer=3, alpha=alphaValue)
togsw = 1
return
# function
def togglepatternZoomIn():
global togsw,o,curpat,col,ovl,gui,alphaValue,ycenter,zoomcount
# if overlay is inactive, ignore button:
if togsw == 0:
print "Pattern button pressed, but ignored --- Crosshair not visible."
zoom_in()
else:
if guivisible == 0:
zoom_in()
# reinitialize array:
ovl = np.zeros((height, width, 3), dtype=np.uint8)
patternswitcherZoomIn(ovl,0)
else:
# reinitialize array
zoom_in()
gui = np.zeros((height, width, 3), dtype=np.uint8)
creategui(gui)
patternswitcherZoomIn(gui,1)
return
def togglepatternZoomOut():
global togsw,o,curpat,col,ovl,gui,alphaValue
# if overlay is inactive, ignore button:
if togsw == 0:
zoom_out()
else:
if guivisible == 0:
zoom_out()
# reinitialize array:
ovl = np.zeros((height, width, 3), dtype=np.uint8)
patternswitcherZoomOut(ovl,0)
o = camera.add_overlay(np.getbuffer(ovl), layer=3, alpha=alphaValue)
else:
zoom_out()
# reinitialize array
gui = np.zeros((height, width, 3), dtype=np.uint8)
creategui(gui)
patternswitcherZoomOut(gui,1)
o = camera.add_overlay(np.getbuffer(gui), layer=3, alpha=alphaValue)
return
def patternswitcherZoomIn(target,guitoggle):
global o, zoomcount, ycenter
if guitoggle == 1:
creategui(gui)
if globalz['zoom_xy'] == globalz['zoom_xy_max']:
print("zoom at max")
def patternswitcherZoomOut(target,guitoggle):
global o, zoomcount, ycenter
# first remove existing overlay:
if o != None:
camera.remove_overlay(o)
if guitoggle == 1:
creategui(gui)
if globalz['zoom_xy'] == globalz['zoom_xy_min']:
print("zoom at min")
def main():
global buttoncounter, zoomcount, guiOn, recording, gui5, gui, o, ovl, camera
try:
initialize_camera()
zoom_in()
zoom_in()
zoom_in()
zoom_in()
zoom_in()
zoom_in()
zoom_in()
zoom_in()
zoom_in()
patternswitch(gui,1)
guivisible = 1
while True:
if KeyboardPoller.keypressed.isSet():
if KeyboardPoller.key=="z":
togglepatternZoomIn()
if KeyboardPoller.key=="x":
togglepatternZoomOut()
if KeyboardPoller.key=="i":
loopcount = 14 - zoomcount
for x in range(loopcount):
togglepatternZoomIn()
if KeyboardPoller.key=="o":
loopcount = zoomcount + 1
for x in range(loopcount):
togglepatternZoomOut()
if KeyboardPoller.key=="n":
set_min_zoom()
update_zoom()
for x in range(14):
zoom_in()
if KeyboardPoller.key=="p":
global roi
filename = get_file_name_pic()
#pushNotification = "curl --data 'key=XXXXXX&title=Photo Taken&msg='"+filename+" https://api.simplepush.io/send"
print camera.zoom
camera.close()
o = None
roi = str(roi)[1:-1]
roi = re.sub("'","",roi)
roi = re.sub(" ","",roi)
print roi
photo = "raspistill -roi "+roi+" -br 55 -ex auto -o /home/pi/piglass/"+filename+" -rot 270"
subprocess.Popen(photo, shell=True)
time.sleep(1)
photofile = "/home/pi/Dropbox-Uploader/dropbox_uploader.sh upload "+filename+" "+filename
time.sleep(6)
camera = picamera.PiCamera()
subprocess.Popen(photofile, shell=True)
#subprocess.Popen(pushNotification, shell=True)
initialize_camera()
camera.start_preview()
update_zoom()
patternswitch(gui, 1)
gui5 = "uploading"
togglepatternRecord()
toggleonoff()
toggleonoff()
time.sleep(1)
gui5 = ""
togglepatternRecord()
toggleonoff()
toggleonoff()
if KeyboardPoller.key=="v":
if recording == 0:
global videoFile, recording
print("recording")
videoFile = get_file_name_vid()
camera.close()
o = None
vid = "raspivid -t 0 -o /home/pi/piglass/"+videoFile+" -rot 270"
subprocess.Popen(vid, shell=True)
recording = 1
if KeyboardPoller.key=="b":
global videoFile, recording
recording = 0
o = None
kill = "killall raspivid"
subprocess.Popen(kill, shell=True)
#pushNotification = "curl --data 'key=XXXXXX&title=Video Taken&msg='"+videoFile+" https://api.simplepush.io/send"
#subprocess.Popen(pushNotification, shell=True)
#time.sleep(2)
vidfile = "/home/pi/Dropbox-Uploader/dropbox_uploader.sh upload "+videoFile+" "+videoFile
subprocess.Popen(vidfile, shell=True)
camera = picamera.PiCamera()
initialize_camera()
camera.start_preview()
patternswitch(gui, 1)
gui5 = "uploaded"
togglepatternRecord()
toggleonoff()
toggleonoff()
time.sleep(1)
gui5 = ""
togglepatternRecord()
toggleonoff()
toggleonoff()
if KeyboardPoller.key=="t":
toggleonoff()
KeyboardPoller.WaitKey().thread.start()
finally:
camera.close() # clean up camera
GPIO.cleanup() # clean up GPIO
if __name__ == "__main__":
main()