-
Notifications
You must be signed in to change notification settings - Fork 0
/
slave-cam.py
133 lines (106 loc) · 4.12 KB
/
slave-cam.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
import web, socket
import io
import time
import picamera
import RPi.GPIO as GPIO
master = (socket.gethostname() == 'master' or socket.gethostname() == 'master2' )
readyPin = 15
shutterInput = 14
if(master):
print('running server as master')
from webGallery import Gallery, GetImage
import os
readyPin = 26
shutterInput = 19
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
#pwm = GPIO.PWM(pwmPin, 100) # Initialize PWM on pwmPin 100Hz frequency
GPIO.setup(shutterInput, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w/ pull-up
GPIO.setup(readyPin, GPIO.OUT) # LED pin set as output
ok = False
urls = (
'/capture', 'Capture',
'/status', 'Status',
'/update', 'Update',
'/shutdown', 'Shutdown',
)
if(master):
masterUrls = (
'/gallery', 'Gallery',
'/get-image/(.+)/', 'GetImage',
)
urls = urls + masterUrls
# start with default values for resolution
width = 1000
height = 1000
if __name__ == "__main__":
app = web.application(urls, globals())
GPIO.output(readyPin, GPIO.HIGH)
time.sleep(0.15)
GPIO.output(readyPin, GPIO.LOW)
app.run()
ok = True
class Status:
def GET(self):
web.header('Content-Type', 'text/html')
return str(ok)
class Capture:
def GET(self):
startTime = time.time()
input = web.input(w="0", h="0", delay="0") # reading user input of resolution values for
input.w = int(input.w) # to integer
input.h = int(input.h)
input.delay = float(input.delay)
if(input.delay != 0):
print('delaying image caputure by' + str(input.delay))
time.sleep(input.delay/1000)
if input.w == 0:
input.w = width # no width given, use saved one
else:
input.w = max(0,min(2592, input.w)) # clamp to range of valid values
if input.h == 0:
input.h = height # no height given, use saved one
else:
input.h = max(0,min(1944, input.h)) # clamp to range of valid values
stream = io.BytesIO()
print('Ready in ' + str(time.time()-startTime) + ' secs ')
with picamera.PiCamera() as camera:
print('Camera init took ' + str(time.time()-startTime) + ' secs')
camera.rotation = 180
camera.resolution = (input.w, input.h)
camera.start_preview()
print('preview now running after ' + str(time.time()-startTime) + ' secs')
imageCaptured = False
GPIO.output(readyPin, GPIO.HIGH)
try:
while imageCaptured == False:
if (GPIO.input(shutterInput) == GPIO.HIGH): # button is released
GPIO.output(readyPin, GPIO.LOW) # illustrate ready state
# print('exp: 1/' + str(1000.0 / (camera.exposure_speed / 1000)))
camera.capture(stream, format='jpeg')
print('Button released, capturing...')
imageCaptured = True
GPIO.output(readyPin, GPIO.HIGH)
time.sleep(0.0005) # 1/2000 shutter time accuracy
GPIO.output(readyPin, GPIO.LOW)
break
# else:
#time.sleep(0.005) # 1/200 shutter time accuracy
except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly:
GPIO.cleanup() # cleanup all GPIO
pwm.stop() # stop PWM
# except Exception as e:
# print(e)
# pass
# "Rewind" the stream to the beginning so we can read its content
stream.seek(0)
web.header('Content-Type', 'image/jpg')
print('Done. It took ' + str(time.time()-startTime) + ' to capture image')
return stream.read()
class Update:
def GET(self):
os.system('git pull && sudo reboot now')
return "error could not restart"
class Shutdown:
def GET(self):
os.system('sudo shutdown')