我正在尝试根据this tutorial中的代码来设置Flask视频流服务器:
from camera_utils import Camera
@app.route('/')
def index():
"""Video streaming home page."""
return render_template('index.html', endpoint='video_feed', source=1)
def gen_feed(camera):
"""Video streaming generator function."""
while True:
frame = camera.get_frame()['frame']
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/video_feed/<path:source>')
def video_feed(source):
"""Video streaming route. Put this in the src attribute of an img tag."""
return Response(gen_feed(Camera(source)),
mimetype='multipart/x-mixed-replace; boundary=frame')
index.html
模板包含一行:
<img src="{{ url_for(endpoint, source=source) }}">
现在,我的函数camera.get_frame()
返回一个dict,其框架为jpg字符串和一些其他信息(例如camera.get_frame()['fps']
)。最好的方法是在网页上显示此信息,以便在每次检索到框架时都会对其进行更新?
EDIT:Return JSON response from Flask view很不错,但是作为Flask的初学者,更具体的答案将不胜感激。如何将字典放入生成器中,使用jsonify处理mimetype='multipart/x-mixed-replace'
并在我的网页中显示json对象?