Uncategorized

html – How to stream a video on the website using python?


You can modify you API as following,

from flask import Response, render_template
import cv2

@app.route("/Record", methods=['GET', 'POST'])
def Record():
    def generate_frames():
        vc = cv2.VideoCapture(0)

        if vc.isOpened():  # try to get the first frame
            rval, frame = vc.read()
        else:
            rval = False

        while rval:
            # Encode the frame into JPEG format
            rval, frame = vc.read()
            if rval:
                ret, buffer = cv2.imencode('.jpg', frame)
                frame = buffer.tobytes()
                yield (b'--frame\r\n'
                       b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
            else:
                break

    return Response(generate_frames(), mimetype="multipart/x-mixed-replace; boundary=frame")

Here, I have used yield for every frame and used mimetype multipart/x-mixed-replace; boundary=frame to return the response. Now, you can invoke this API from wherever you want and it will return you video frames. You can design separate HTML page to handle it or by directly open in chrome will also work.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *