写一个python代码将opencv库读取的rtsp协议视频流展示到前端html页面
时间: 2023-05-17 09:06:16 浏览: 35
非常感谢您的提问!以下是一个简单的Python代码示例,可以将OpenCV库读取的RTSP协议视频流展示到前端HTML页面:
```python
import cv2
import numpy as np
from flask import Flask, render_template, Response
app = Flask(__name__)
cap = cv2.VideoCapture('rtsp://your_rtsp_stream_address')
def gen_frames():
while True:
success, frame = cap.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('/')
def index():
return render_template('index.html')
@app.route('/video_feed')
def video_feed():
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(debug=True)
```
请注意,此代码需要使用Flask框架和Jinja2模板引擎。在运行代码之前,请确保您已经安装了这些依赖项。另外,您需要将“your_rtsp_stream_address”替换为您自己的RTSP流地址。
希望这可以帮助您展示OpenCV读取的RTSP协议视频流到前端HTML页面!
相关推荐


















