Flask OpenCV 将视频流发送到外部 HTML 页面

时间:2021-01-21 22:27:04

标签: opencv flask computer-vision rtsp ip-camera

我想问一个关于flask在视频流管理中的使用问题: 1-> 我使用 rtsp 协议从 Ip 摄像机恢复视频流 2-> 使用 Flask 我显示视频但在我的浏览器中使用 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)

我想知道,是否可以不在 locahost http://127.0.0.1:5000 中进行显示,而是将其发送到外部 HTML 页面

谢谢。

1 个答案:

答案 0 :(得分:0)

向您的 app.run() 添加参数。默认情况下,它在 localhost 上运行,将其更改为 app.run(host= '0.0.0.0') 以在您机器的所有 IP 地址上运行。 0.0.0.0 是一个特殊值,您需要导航到实际 IP 地址。

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

然后客户端浏览器需要您的机器 IP 和您的开放端口来导航和查看流

http://your-machine-ip:5000

所以机制是你的机器创建一个文件 index.html 并在外部浏览器请求地址时提供它 http://your-machine-ip:5000

将您的 index.html 放入文件夹 templates/index.html

此处有更多详细信息:Configure Flask dev server to be visible across the network

我认为您还需要 camera.release()

相关问题