Search code examples
opencvflaskcomputer-visionrtspip-camera

Flask OpenCV send video stream to extrenal HTML Page


I would like to ask a question regarding the use of flask in the management of the video stream: 1-> I recover the video stream from an Ip camera with the rtsp protocol 2-> with Flask I display the video but in my browser in localHsot

from flask import Flask, render_template, Response
import cv2

app = Flask(__name__)

camera = cv2.VideoCapture("rtsp://--:--@ipAdress/media.amp")


def gen_frames():
    while True:
        # Capture frame-by-frame
        success, frame = camera.read()
        #cv2.imshow('frame', frame)
        print("success-------",success)
        if not success:
            break
        else:
            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')


@app.route('/video_feed')
def video_feed():
    #Video streaming route. Put this in the src attribute of an img tag
    return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')


@app.route('/')
def index():
    """Video streaming home page."""
    return render_template('index.html')


if __name__ == '__main__':
    app.run(debug=True)

I would like to know please, is it possible to do the display not in locahost http://127.0.0.1:5000 but to send it to an external HTML page

Thanks.


Solution

  • Add a parameter to your app.run(). By default it runs on localhost, change it to app.run(host= '0.0.0.0') to run on all your machine's IP addresses. 0.0.0.0 is a special value, you'll need to navigate to the actual IP address.

    if __name__ == '__main__':
        app.run(host= '0.0.0.0',debug=True)
    

    Then a Client Browser need your machine IP and your open port to navigate and view the Stream

    http://your-machine-ip:5000
    

    So the mechanism is that your machine create a file index.html and serve it when a external Browser request the address http://your-machine-ip:5000

    put your index.html in folder templates/index.html

    More details here : Configure Flask dev server to be visible across the network

    I think you need camera.release() also.