cv2.VideoCapture 无法访问 heroku 网络部署中的用户摄像头

问题描述

我正在从事一个项目,我需要在网络中(如 IP 摄像机)“广播”用户摄像机输入(远程)。所以我有一个解决方法来使用 python Flask 应用程序来实现这一点。因为我认为当我尝试(再次)捕获 Web URL 时,它的工作方式与 IP 摄像机相同。所以在思维导图格式中,它会是这样的:

用户摄像头 > cv2.VideoCapture(用户摄像头) > 视频源的结果 URL > cv2.VideoCapture(视频源的 URL) 以供进一步使用

在我的本地机器上,代码 cv2.VideoCapture(0) 正在从我的网络摄像头返回一个摄像头输入。所以我认为当我尝试在 Heroku 上部署它时,这会以同样的方式工作。但是在网络上发生的事情是,没有来自用户网络摄像头的输入。我该如何解决这个问题?

这是我的完整 app.py 代码

from flask import Flask,render_template,Response
import cv2

app = Flask(__name__)

camera = cv2.VideoCapture(0) 

def gen_frames(): 
    while True:
        # Capture frame-by-frame
        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():
    return render_template('index.html')


if __name__ == '__main__':
    app.run(threaded=True,port=5000)

解决方法

试试这个链接,它应该有助于通过网络捕获用户的网络摄像头https://towardsdatascience.com/video-streaming-in-web-browsers-with-opencv-flask-93a38846fe00