Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
545 views
in Technique[技术] by (71.8m points)

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
'
                   b'Content-Type: image/jpeg

' + frame + b'
')


@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.

question from:https://stackoverflow.com/questions/65836699/flask-opencv-send-video-stream-to-extrenal-html-page

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...