Python uses flask to display video on the web page

Note that our return return value must be one of the following, otherwise an error will be reported
insert image description here

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()

Enter the route after running
insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/qq_16792139/article/details/131449331