注意我们的return返回值必须是以下之一,否则会报错
from flask import Flask, render_template, Response
import cv2
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
def gen(camera):
while True:
success, image = camera.read()
if not success:
break
ret, jpeg = cv2.imencode('.jpg', image)
frame = jpeg.tobytes()
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():
camera = cv2.VideoCapture(0)
return Response(gen(camera), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run()
运行后输入路由