如果释放摄像头后再次访问摄像头,OpenCV Python捕获将失败

OpenCV Python capture fails if I access a webcame again after releasing it

我正在开发一个多线程应用程序,以捕获来自四个USB网络摄像头的图像。为了简化和早期开发,我使用640x480和30fps的Logitech C920。

我有一个简单的功能,可以打开照相机并设置一些参数,然后释放照相机。因为这是一个多线程应用程序,所以当按下一个按钮时,每个线程都在运行四个线程。效果很好。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def camParameter(previewName, camID):
    #Set camera object and set parameters
    start_time = time.time()
    cam_test = True
    while cam_test:  
        cam = cv2.VideoCapture(camID)  
        present_time = time.time()
        if present_time - start_time > 2:
            print("Could not open camera", str(camID))
            break
        if cam.isOpened():
            cam_test=False

    width = 640
    height = 480
    fps = 30
    test_width = cam.get(3)
    test_height = cam.get(4)
    test_fps = cam.get(5)

    if test_width != width:
        cam.set(3,width)
    if test_height != height:
        cam.set(4,height)
    if test_fps != fps:
        cam.set(5,fps)

    print("Parameters set for camera", str(camID))
    cam.release()

但是,如果我再次调用该函数,或尝试打开相机进行流式播放,则会出现以下错误:

VIDEOIO ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV
Unable to stop the stream: Device or resource busy

我可以使用GUVCviewer打开相机,或者拔下/重新插入相机以重新获得访问权限。

有什么想法为什么第二次使用相机会导致此问题,或者如何解决?

我已经确认相机是实际发布的。我可以访问相机


我用GStreamer重新编译了openCV-它对多线程更友好。