-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb_streaming.py
executable file
·286 lines (270 loc) · 9.37 KB
/
web_streaming.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
#!/usr/bin/env python3
# Derived from:
# https://github.com/waveform80/picamera/blob/release-1.13/docs/examples/web_streaming.py
#
# Copyright 2021 Chris Derossi <[email protected]>
#
# Copyright 2013-2015 Dave Jones <[email protected]>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import io
import picamera
import logging
import socketserver
from threading import Condition
from http import server
PAGE="""\
<html>
<head>
<title>picamera MJPEG streaming demo</title>
<style>
body {
background-color: #094b86;
}
label {
xcolor: white;
margin-left: 2em;
}
.container {
position: relative;
width: 640px;
margin: auto;
color: white;
}
.width1024 {
width: 1024px;
}
.vline {
background-color: red;
width: 0.2px;
height: 480px;
position: absolute;
top: 0;
left: 320;
}
.vline1024 {
height: 768px;
left: 512;
}
.hline {
background-color: red;
width: 640px;
height: 0.2px;
position: absolute;
top: 240;
left: 0;
}
.hline1024 {
width: 1024;
top: 384;
}
.hidden {
display: none;
}
.fov {
margin-top: 1em;
}
.fov input {
width: 4em;
}
.fovanswer {
margin-top: 1em;
font-size: 150%;
}
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
input[type=number] {
-moz-appearance: textfield;
}
</style>
<script>
var resolution = "640"
function checkCrosshairs()
{
var checkbox = document.getElementById('crosshairs')
if (checkbox.checked)
{
document.getElementById('vline').classList.remove('hidden')
document.getElementById('hline').classList.remove('hidden')
}
else
{
document.getElementById('vline').classList.add('hidden')
document.getElementById('hline').classList.add('hidden')
}
}
function checkFOV()
{
var distance = parseFloat(document.getElementById('distance').value)
var visible = parseFloat(document.getElementById('visible').value)
if (distance > 0 && visible > 0)
{
var angle = Math.atan((visible / 2) / distance) * 2 * 180 / Math.PI
document.getElementById('fovvalue').innerText = "" + angle.toFixed(2)
document.getElementById('fovanswer').classList.remove('hidden')
}
else
{
document.getElementById('fovanswer').classList.add('hidden')
}
}
function changeResolution()
{
var selector = document.getElementById('resolution')
if (selector.value != resolution)
{
resolution = selector.value
var videoImg = document.getElementById('video')
videoImg.src = ""
if (resolution == "640")
{
document.getElementById('vline').classList.remove('vline1024')
document.getElementById('hline').classList.remove('hline1024')
document.getElementById('container').classList.remove('width1024')
}
else
{
document.getElementById('vline').classList.add('vline1024')
document.getElementById('hline').classList.add('hline1024')
document.getElementById('container').classList.add('width1024')
}
setTimeout(() => {
videoImg.src = "stream" + resolution + ".mjpg"
}, 100)
}
}
</script>
</head>
<body>
<div id="container" class="container">
<img id="video" class="camvideo" src="stream640.mjpg" />
<div class="controls">
<label for="resolution">Resolution:</label>
<select id="resolution" onchange="changeResolution()">
<option value="640">640 x 480</option>
<option value="1024">1024 x 768</option>
</select>
<label for="crosshairs">Show Crosshairs:</label>
<input type="checkbox" id="crosshairs" onclick="checkCrosshairs()"/>
</div>
<div class="fov">
<div>
<label for="distance">Distance to lens:</label>
<input id="distance" type="number" oninput="checkFOV()"/>
</div>
<div>
<label for="visible">Total visible length:</label>
<input id="visible" type="number" oninput="checkFOV()"/>
</div>
<div class="fovanswer hidden" id="fovanswer">
<label>Field of view:</label>
<span id="fovvalue"></span>°
</div>
</div>
<div id="vline" class="vline hidden"></div>
<div id="hline" class="hline hidden"></div>
</div>
</body>
</html>
"""
class StreamingOutput(object):
def __init__(self):
self.frame = None
self.buffer = io.BytesIO()
self.condition = Condition()
self.first = True
def write(self, buf):
if buf.startswith(b'\xff\xd8'):
# New frame, copy the existing buffer's content and notify all
# clients it's available
self.buffer.truncate()
with self.condition:
self.frame = self.buffer.getvalue()
if self.first:
self.first = False
else:
self.condition.notify_all()
self.buffer.seek(0)
return self.buffer.write(buf)
class StreamingHandler(server.BaseHTTPRequestHandler):
def stream_common(self):
self.send_response(200)
self.send_header('Age', 0)
self.send_header('Cache-Control', 'no-cache, private')
self.send_header('Pragma', 'no-cache')
self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')
self.end_headers()
try:
while True:
with self.output.condition:
self.output.condition.wait()
frame = self.output.frame
self.wfile.write(b'--FRAME\r\n')
self.send_header('Content-Type', 'image/jpeg')
self.send_header('Content-Length', len(frame))
self.end_headers()
self.wfile.write(frame)
self.wfile.write(b'\r\n')
except Exception as e:
self.camera.stop_recording()
self.camera.close()
logging.warning(
'Removed streaming client %s: %s',
self.client_address, str(e))
def do_GET(self):
if self.path == '/':
self.send_response(301)
self.send_header('Location', '/index.html')
self.end_headers()
elif self.path == '/index.html':
content = PAGE.encode('utf-8')
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.send_header('Content-Length', len(content))
self.end_headers()
self.wfile.write(content)
elif self.path == '/stream640.mjpg':
self.output = StreamingOutput()
self.camera = picamera.PiCamera(resolution='640x480', framerate=24)
self.camera.start_recording(self.output, format='mjpeg')
self.stream_common()
elif self.path == '/stream1024.mjpg':
self.output = StreamingOutput()
self.camera = picamera.PiCamera(resolution='1024x768', framerate=24)
self.camera.start_recording(self.output, format='mjpeg')
self.stream_common()
else:
self.send_error(404)
self.end_headers()
class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):
allow_reuse_address = True
daemon_threads = True
address = ('', 8000)
httpd = StreamingServer(address, StreamingHandler)
httpd.serve_forever()
# vim: set ts=4 sw=4 expandtab: