python - imutils VideoStream与flask集成时返回NoneType

标签 python opencv flask imutils

因此,我想使用imutils VideoStream创建视频流并将其放在网络上。这是代码:

camera_web.py

from flask import Flask, render_template, Response
from imutils.video import VideoStream
from imutils.video import FPS
import cv2

app = Flask(__name__)
vs = VideoStream(src=0).start()

@app.route('/')
def index():
    """ Video streaming home page """
    return render_template('index.html')

def gen():
        rval, frame = vs.read()
        cv2.imwrite('t.jpg', frame)
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + open('t.jpg', 'rb').read() + b'\r\n')


@app.route('/video_feed')
def video_feed():
    return Response(gen(), mimetype='multipart/x-mixed-replace; boundary=frame')

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

index.html
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vehicle Counter Web</title>
</head>
<body>
    <h1>Vehicle Counter Demo</h1>
    <img src="{{ url_for('video_feed') }}">
</body>
</html>

现在,当我运行它时,它返回错误:

[ WARN:0] videoio(MSMF): OnReadSample() is called with error status: -1072875772 [ WARN:0] videoio(MSMF): async ReadSample() call is failed with error status: -1072875772 [ WARN:1] videoio(MSMF): can't grab frame. Error: -1072875772



而且它不会像下面的图片那样返回我的任何视频流:

enter image description here

我的代码中是否有错误,或者flask不支持imutils VideoStream?提前致谢。

最佳答案

好吧,所以我就是那个傻瓜。代码应如下所示:

camera_web.py

from flask import Flask, render_template, Response
from imutils.video import WebcamVideoStream
from imutils.video import FPS
import imutils
import time
import cv2

app = Flask(__name__)


@app.route('/')
def index():
    """ Video streaming home page """
    return render_template('index.html')


def gen():
    vs = WebcamVideoStream(src=1).start()
    time.sleep(2.0)
    while True:
        frame = vs.read()
        frame = imutils.resize(frame, width=500)
        rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        cv2.imwrite('t.jpg', frame)
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + open('t.jpg', 'rb').read() + b'\r\n')


@app.route('/video_feed')
def video_feed():
    return Response(gen(), mimetype='multipart/x-mixed-replace; boundary=frame')


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

然后我们去了!我们现在应该看到视频流。
Link to the image

关于python - imutils VideoStream与flask集成时返回NoneType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55279031/

相关文章:

Python 在终端中使用 .csv 文件

python - 将包添加到 PyPi 时发生 SSL 错误

python - 以 elif 方式比较 Pandas 数据框中的字符串

c++ - OpenCV 中的局部归一化

python - "Empty reply from server"用于 Flask + uWSGI 设置

python - jinja2.exceptions.TemplateSyntaxError : expected token 'end of print statement' , 得到 'posted'

python - Flask:提供来自不同文件夹的静态文件,如 css

java - 更改原始图像文件的颜色权重

opencv - 从 64 位切换到 32 位 openCV 时出现链接错误

android - BaseLoaderCallback的Finish()方法不存在?