Stream responses (Fixes #44)
This commit is contained in:
BIN
examples/1.jpg
Normal file
BIN
examples/1.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.9 KiB |
BIN
examples/2.jpg
Normal file
BIN
examples/2.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.3 KiB |
BIN
examples/3.jpg
Normal file
BIN
examples/3.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.4 KiB |
45
examples/video_stream.py
Normal file
45
examples/video_stream.py
Normal file
@@ -0,0 +1,45 @@
|
||||
try:
|
||||
import utime as time
|
||||
except ImportError:
|
||||
import time
|
||||
|
||||
from microdot import Microdot
|
||||
|
||||
app = Microdot()
|
||||
|
||||
frames = []
|
||||
for file in ['1.jpg', '2.jpg', '3.jpg']:
|
||||
with open(file, 'rb') as f:
|
||||
frames.append(f.read())
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index(request):
|
||||
return '''<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Microdot Video Streaming</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Microdot Video Streaming</h1>
|
||||
<img src="/video_feed">
|
||||
</body>
|
||||
</html>''', 200, {'Content-Type': 'text/html'}
|
||||
|
||||
|
||||
@app.route('/video_feed')
|
||||
def video_feed(request):
|
||||
def stream():
|
||||
yield b'--frame\r\n'
|
||||
while True:
|
||||
for frame in frames:
|
||||
yield b'Content-Type: image/jpeg\r\n\r\n' + frame + \
|
||||
b'\r\n--frame\r\n'
|
||||
time.sleep(1)
|
||||
|
||||
return stream(), 200, {'Content-Type':
|
||||
'multipart/x-mixed-replace; boundary=frame'}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
64
examples/video_stream_async.py
Normal file
64
examples/video_stream_async.py
Normal file
@@ -0,0 +1,64 @@
|
||||
import sys
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
import asyncio
|
||||
|
||||
from microdot_asyncio import Microdot
|
||||
|
||||
app = Microdot()
|
||||
|
||||
frames = []
|
||||
for file in ['1.jpg', '2.jpg', '3.jpg']:
|
||||
with open(file, 'rb') as f:
|
||||
frames.append(f.read())
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index(request):
|
||||
return '''<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Microdot Video Streaming</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Microdot Video Streaming</h1>
|
||||
<img src="/video_feed">
|
||||
</body>
|
||||
</html>''', 200, {'Content-Type': 'text/html'}
|
||||
|
||||
|
||||
@app.route('/video_feed')
|
||||
async def video_feed(request):
|
||||
if sys.implementation.name != 'micropython':
|
||||
# CPython supports yielding async generators
|
||||
async def stream():
|
||||
yield b'--frame\r\n'
|
||||
while True:
|
||||
for frame in frames:
|
||||
yield b'Content-Type: image/jpeg\r\n\r\n' + frame + \
|
||||
b'\r\n--frame\r\n'
|
||||
await asyncio.sleep(1)
|
||||
|
||||
else:
|
||||
# MicroPython can only use class-based async generators
|
||||
class stream():
|
||||
def __init__(self):
|
||||
self.i = 0
|
||||
|
||||
def __aiter__(self):
|
||||
return self
|
||||
|
||||
async def __anext__(self):
|
||||
await asyncio.sleep(1)
|
||||
self.i = (self.i + 1) % len(frames)
|
||||
return b'Content-Type: image/jpeg\r\n\r\n' + \
|
||||
frames[self.i] + b'\r\n--frame\r\n'
|
||||
|
||||
return stream(), 200, {'Content-Type':
|
||||
'multipart/x-mixed-replace; boundary=frame'}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
Reference in New Issue
Block a user