Flask读取RTSP视频流,及其简单的一个案例

Flask读取RTSP视频流,及其简单的一个案例

此章节包括通过是使用Flask可以实时地显示RTSP视频流。代码确实非常简单,源于Github,主要为为Nvidia Deepstream极致细节:3. Deepstream Python RTSP视频输出显示这篇博客写的。这里会附上所有代码,如果对其中某些部分不明白的,可以翻看我这个系列文档,很容易使用。

喜欢的朋友收藏点赞哦。

文章目录

app.py

from flask import Flask, render_template, Response
import cv2

app = Flask(__name__)

camera = cv2.VideoCapture('rtsp://localhost:8554/ds-test')

def gen_frames():

    while True:

        success, frame = camera.read()
        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():

    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)

templates/index.html

doctype html>
<html lang="en">
<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
          integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

    <title>Live Streaming Demonstrationtitle>
head>
<body>
<div class="container">
    <div class="row">
        <div class="col-lg-8  offset-lg-2">
            <h3 class="mt-5">Live Streamingh3>
            <img src="{{ url_for('video_feed') }}" width="100%">
        div>
    div>
div>
body>
html>

结果

结果如下(当然,你需要保证有rtsp的数据先):

Flask读取RTSP视频流,及其简单的一个案例

; 运行

需要先安装一下flask以及opencv-python(pip3 install…),然后在主路径下运行 python3 app.py。(这里写的很不专业,因为只是实现一下效果,如对flask感兴趣,请参见我的 Flask &#x6781;&#x81F4;&#x7EC6;&#x8282;系列文档)。

Original: https://blog.csdn.net/zyctimes/article/details/122619514
Author: 破浪会有时
Title: Flask读取RTSP视频流,及其简单的一个案例

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/745930/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球