python - video streaming using flask with raspi-camera -
sorry english skill. want make web page streams video , runs several functions. using python , flask server. but, there problems can't solve alone. have source code. it's perfect.
source code.
import time flask import flask, render_template, response camera import camera app = flask(__name__) @app.route('/') def index(): return render_template('index.html') def gen(camera): while true: frame = camera.get_frame() yield (b'--frame\r\n' b'content-type: image/jpeg\r\n\r\n' + frame + b'\r\n') @app.route('/video_feed') def video_feed(): return response(gen(camera()), mimetype='multipart/x-mixed-replace; boundary=frame') @app.route('/test') def test(): return time.time() if __name__ == '__main__': app.run(host='0.0.0.0', debug=true
and template
<html> <head> <title>video streaming demonstration</title> </head> <body> <h1>video streaming demonstration</h1> <img src="{{ url_for('video_feed') }}"> </body> </html>
in source code, function named gen() using 'yield'. so, never end. take resource. want run function 'test' function while video streaming.
video streaming requires permanent connection client. if doing using flask development web server, can't handle other requests, because server default handles single connection @ time.
if want handle more 1 connection simultaneously have couple of options. simplest (but not robust or efficient) run development server in threaded mode (just add threaded=true
inside app.run()
call). spawn new threads incoming requests. more production-ready solution switch different web server. example, gunicorn can control how many worker processes started. more flexibility server such gevent can handle large number of concurrent client requests.
Comments
Post a Comment