-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmth_webcam.py
363 lines (327 loc) · 8.35 KB
/
mth_webcam.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
import usocket as soc
import _thread as th
import camera
import time
import esp
from machine import Pin
import gc
from Wifi.Sta import Sta
#esp.osdebug(False)
esp.osdebug(True)
hdr = {
# start page for streaming
# URL: /apikey/webcam
'live': """HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
<html>
<head>
<title>Web Camera</title>
</head>
<body>
<center>
<h1>Web Camera</h1>
<img src="/apikey/live" width=720 height=540 />
</center>
</body>
</html>
""",
# live stream -
# URL: /apikey/live
'stream': """HTTP/1.1 200 OK
Content-Type: multipart/x-mixed-replace; boundary=frame
Connection: keep-alive
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Expires: Thu, Jan 01 1970 00:00:00 GMT
Pragma: no-cache
""",
# live stream -
# URL:
'frame': """--frame
Content-Type: image/jpeg
""",
# still picture -
# URL: /apikey/snap
'snap': """HTTP/1.1 200 OK
Content-Type: image/jpeg
Content-Length: """,
# no content error
# URL: all the rest
'none': """HTTP/1.1 204 No Content
Content-Type: text/plain; charset=utf-8
Nothing here!
""",
# bad request error
# URL: /favicon.ico
'favicon': """HTTP/1.1 404
""",
# bad request error
# URL: all the rest
'err': """HTTP/1.1 400 Bad Request
Content-Type: text/plain; charset=utf-8
Hello? Can not compile
""",
# OK
# URL: all the rest
'OK': """HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
OK!
"""
}
def clean_up(cs):
cs.close() # flash buffer and close socket
del cs
gc.collect()
def frame_gen():
while True:
buf = camera.capture()
yield buf
del buf
gc.collect()
def send_frame(pp):
cs, h = pp
while True:
ee = ''
try:
cs.send(b'%s' % h)
cs.send(next(pic))
cs.send(b'\r\n') # send and flush the send buffer
except Exception as e:
ee = str(e)
if ee == '':
time.sleep(0.005) # try as fast as we can
else:
break
clean_up(cs)
return
def servers():
socks = []
# port 80 server - streaming server
s = soc.socket(soc.AF_INET, soc.SOCK_STREAM)
s.setsockopt(soc.SOL_SOCKET, soc.SO_REUSEADDR, 1)
a = ('0.0.0.0', 80)
s.bind(a)
s.listen(2) # queue at most 2 clients
socks.append(s)
# port 81 server - still picture server
s = soc.socket(soc.AF_INET, soc.SOCK_STREAM)
s.setsockopt(soc.SOL_SOCKET, soc.SO_REUSEADDR, 1)
a = ('0.0.0.0', 81)
s.bind(a)
s.listen(2)
socks.append(s)
# port 82 server - cmd server
s = soc.socket(soc.AF_INET, soc.SOCK_STREAM)
s.setsockopt(soc.SOL_SOCKET, soc.SO_REUSEADDR, 1)
a = ('0.0.0.0', 82)
s.bind(a)
s.listen(2)
socks.append(s)
return socks
def port1(cs, rq):
rqp = rq[1].split('/')
if rqp[1] == 'apikey': # Must have /apikey/<REQ>
if rqp[2] == 'webcam':
cs.send(b'%s' % hdr['live'])
clean_up(cs)
elif rqp[2] == 'live': # start streaming
cs.send(b'%s' % hdr['stream'])
# start frame sending
send_frame([cs, hdr['frame']])
else: #
cs.send(b'%s' % hdr['none'])
clean_up(cs)
else:
cs.send(b'%s' % hdr['err'])
clean_up(cs)
def port2(cs, rq):
rqp = rq[1].split('/')
if rqp[1] == 'apikey': # Must have /apikey/<REQ>
if rqp[2] == 'snap':
try:
img=next(pic)
cs.send(b'%s %d\r\n\r\n' % (hdr['snap'], len(img)))
cs.send(img)
cs.send(b'\r\n')
except:
pass
elif rqp[2] == 'blitz':
try:
flash_light.on()
img=next(pic)
flash_light.off()
cs.send(b'%s %d\r\n\r\n' % (hdr['snap'], len(img)))
cs.send(img)
cs.send(b'\r\n')
except:
pass
else:
cs.send(b'%s' % hdr['none'])
else:
cs.send(b'%s' % hdr['err'])
clean_up(cs)
def port3(cs, rq):
rqp = rq[1].split('/')
if rqp[1] == 'apikey': # Must have /apikey/<REQ>
if rqp[2] == 'flash': # /apikey/flash/<what>
if rqp[3] == 'on':
flash_light.on()
else:
flash_light.off()
cs.send(b'%s' % hdr['OK'])
elif rqp[2] == 'fmt':
w = int(rqp[3])
if w>=0 and w<=2:
camera.pixformat(w)
cs.send(b'%s' % hdr['OK'])
elif rqp[2] == 'pix':
w = int(rqp[3])
if w>0 and w<13:
camera.framesize(w)
cs.send(b'%s' % hdr['OK'])
elif rqp[2] == 'qua':
w = int(rqp[3])
if w>9 and w<64:
camera.quality(w)
cs.send(b'%s' % hdr['OK'])
elif rqp[2] == 'con':
w = int(rqp[3])
if w>-3 and w<3:
camera.contrast(w)
cs.send(b'%s' % hdr['OK'])
elif rqp[2] == 'sat':
w = int(rqp[3])
if w>-3 and w<3:
camera.saturation(w)
cs.send(b'%s' % hdr['OK'])
elif rqp[2] == 'bri':
w = int(rqp[3])
if w>-3 and w<3:
camera.brightness(w)
cs.send(b'%s' % hdr['OK'])
elif rqp[2] == 'ael':
w = int(rqp[3])
if w>-3 and w<3:
camera.aelevels(w)
cs.send(b'%s' % hdr['OK'])
elif rqp[2] == 'aev':
w = int(rqp[3])
if w>=0 and w<=1200:
camera.aecvalue(w)
cs.send(b'%s' % hdr['OK'])
elif rqp[2] == 'agc':
w = int(rqp[3])
if w>=0 and w<=30:
camera.agcgain(w)
cs.send(b'%s' % hdr['OK'])
elif rqp[2] == 'spe':
w = int(rqp[3])
if w>=0 and w<7:
camera.speffect(w)
cs.send(b'%s' % hdr['OK'])
elif rqp[2] == 'wbl':
w = int(rqp[3])
if w>=0 and w<5:
camera.whitebalance(w)
cs.send(b'%s' % hdr['OK'])
else:
cs.send(b'%s' % hdr['none'])
else:
cs.send(b'%s' % hdr['err'])
clean_up(cs)
def srv(p):
sa = socks[p] # start server; catch error - loop forever
while True:
try:
ok = True
ee = ''
try:
sa.settimeout(0.05) # in sec NB! default browser timeout (5-15 min)
cs, ca = sa.accept()
cs.settimeout(0.5) # in sec
except Exception as e:
ee = str(e)
if ee != '':
# print('Socket %d accept - %s' % (p, ee.split()[-1]))
#ee = ''
pass
ok = False
if ok: # No soc.accept timeout
ee = ''
try:
# client accepted
r = cs.recv(1024)
# REQ: b'' # may be due to very short timeout
except Exception as e:
ee = str(e)
if ee != '':
print(ee)
ok = False
else:
ms = r.decode('utf-8')
if ms.find('favicon.ico') < 0:
rq = ms.split(' ')
try:
print(rq[0], rq[1], ca)
except:
ok = False
else:
cs.send(b'%s' % hdr['favicon']) # handle favicon request early
clean_up(cs)
ok = False
if ok: # No soc.recv timeout or favicon request
try:
ports[p](cs, rq)
except Exception as e:
ee = str(e)
if ee != '':
print(ee)
except Exception as e:
ee = str(e)
if ee != '':
print(ee)
#------------------
wc = 0
while True:
cr = camera.init()
print("Camera ready?: ", cr)
if cr:
break
time.sleep(2)
wc += 1
if wc >= 5:
break
if not cr:
print("Camera not ready. Can't continue!")
else:
# setup networking
w = Sta()
w.connect()
w.wait()
wc = 0
while not w.wlan.isconnected():
print("WIFI not ready. Wait...")
time.sleep(2)
wc += 1
if wc >= 5:
break
if wc >= 5:
print("WIFI not ready. Can't continue!")
else:
pic = frame_gen()
flash_light = Pin(04,Pin.OUT)
socks = servers()
ports = [port1, port2, port3] # 80, 81, 82
# schedule 2 servers for port 80
# streaming hold socket until client terminate,
# so, we service at most 2 clients on port 80
th.start_new_thread(srv, (0,))
th.start_new_thread(srv, (0,))
# schedule 1 server for port 81
th.start_new_thread(srv, (1,))
#
# schedule other processes here
#
# go for it!
print("Up Up and Away!")
# let the main thread take care of port 82 - block REPL
srv(2)