【发布时间】:2021-03-01 12:53:10
【问题描述】:
在虚拟机 (GCP) 上运行服务器时,我尝试访问客户端的摄像头。当我在本地机器上运行 Flask 服务器时,它工作正常 - 请求访问摄像头的权限,但是当我在虚拟机上运行 Flask 服务器时,它没有检测到摄像头(我在浏览器中运行
[WARN:3] 全局 /tmp/pip-req-build-7m_g9lbm/opencv/modules/videoio/src/cap_v4l.cpp (893) 打开 VIDEOIO(V4L2:/dev/video0): can' t 按索引打开相机。
代码:
app.py
from flask import Flask, render_template, Response
import cv2
app = Flask(__name__)
camera = cv2.VideoCapture(0)
def gen_frames():
while True:
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():
"""Video streaming home page."""
return render_template('index.html')
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5002, debug=True)
我的问题:是否可以以这种方式运行 Flask 服务器,检测用户的摄像头并请求访问它的权限?感谢您的建议。
【问题讨论】:
-
这与 Flask 完全无关。摄像头在虚拟机中不可用。
标签: python opencv flask video-streaming virtual-machine