利用Flask+Python+OpenCV实现摄像头读取图像帧网页视频流
github链接:https://github.com/Windxy/Flask_videostream_object_detection
1.环境准备
python3.x
cv2
flask
2.代码
文件树:
----文件名\
|----templates\
| |----index.html
|----app.py
|----base_camera.py
|----camera_opencv.py
2.1 index.html
<html>
<head>
<title>视频流演示</title>
</head>
<body>
<h1>视频流演示</h1>
<img src="{
{ url_for('video_feed') }}">
</body>
</html>
说明:该代码块输入在templates文件夹下的index.html
- 其中,图片的路径,对应flask中的
url_for函数,这个函数用于给指定函数(video_feed函数)构造url链接,下面看一下app.py中的相关函数
2.2 app.py
from importlib import import_module
import os
from flask import Flask, render_template, Response
from camera_opencv import Camera
app = Flask(__name__)
@app.route('/')
def index():
"""主页"""
return render_template('index.html')
def gen(camera):
"""视频流生成"""
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/video_feed')

本文介绍如何使用Flask、Python和OpenCV实现实时摄像头图像帧的网页视频流。通过具体代码示例,展示了从摄像头读取图像帧,并将其转换为网页上显示的视频流的过程。
最低0.47元/天 解锁文章
1232

被折叠的 条评论
为什么被折叠?



