【问题标题】:imutils VideoStream returns NoneType while integrating with flaskimutils VideoStream 在与烧瓶集成时返回 NoneType
【发布时间】:2019-03-25 18:21:23
【问题描述】:

所以我想使用 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() 以错误状态调用: -1072875772 [WARN:0] videoio(MSMF):异步 ReadSample() 调用失败,错误状态:-1072875772 [WARN:1] videoio(MSMF):无法抓取 框架。错误:-1072875772

并且它不会像这张图片一样返回我的任何视频流:

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

【问题讨论】:

  • 现在为什么我使用 VideoStream 而不是 cv2.VideoCapture(0) 是因为我的其他程序需要 videostream 来实时计算对象并且它在我的教程中,所以我使用 VideoStream 代替。跨度>

标签: python opencv flask imutils


【解决方案1】:

好吧,所以我就是那个愚蠢的人。代码应该是这样的:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-25
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多