ubuntu系统下,flask调用相机找不到设备

请问大佬们,ubuntu系统下flask的app.py文件使用cv2.VideoCapture(0)就会报错找不到设备,但是其他文件调用相机就没问题,是我虚拟机的问题吗,还是有其他的一些原因

开启调试模式时会出现这样的问题,但是禁用调试模式可以正常运行,windows系统无论开启或是禁用都不会有问题

贴一下代码:

from flask import Flask, render_template, Response
import cv2

cap = cv2.VideoCapture(0)
print(cap.isOpened())

app = Flask(__name__)


def gen_frames1():
    while True:
        success, frame = cap.read()
        if not success:
            break
        else:
            # Do some processing on the frame
            # ...

            ret, buffer = cv2.imencode('.jpg', frame)
            frame = buffer.tobytes()

            yield (b'--frame1\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')


@app.route('/')
def hello_world():  # put application's code here
    return render_template('index.html')


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


if __name__ == '__main__':
    app.run()

还有报错:
[ WARN:0@0.012] global cap_v4l.cpp:982 open VIDEOIO(V4L2:/dev/video0): can’t open camera by index
[ERROR:0@0.018] global obsensor_uvc_stream_channel.cpp:156 getStreamChannelGroup Camera index out of range