无法在 Ubuntu 16.04(主机)上的 OpenCV 中使用集成网络摄像头

Unable to use integrated webcam in OpenCV on Ubuntu 16.04(host)

Ubuntu 16.04 上 OpenCV 中的集成网络摄像头返回以下错误。我用不同的程序检查了 cheese,它显示静止图像和视频,因此这里似乎不是相机本身的问题。

我用来测试的代码:

    import cv2
    import numpy as np
    import time
    cam = cv2.VideoCapture(2)
    if not cam.isOpened():
     print('Cannot open camera')

    while True:
     ret,frame = cam.read()
     cv2.imshow('webcam', frame)
     if cv2.waitKey(1)&0xFF == ord('q'):
      break

   cam.release()
   cv2.destroyAllWindows()

错误:

Cannot open camera (feedback from script at if not cam.isOpened():).

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /io/opencv/modules/highgui/src/window.cpp, line 325

Traceback (most recent call last): File "Video_test.py", line 13, in cv2.imshow('webcam', frame) cv2.error: /io/opencv/modules/highgui/src/window.cpp:325: error: (-215) size.width>0 && size.height>0 in function imshow

如有任何帮助,我们将不胜感激。谢谢!

使用 cam.open() 尝试以下操作:

import cv2
import numpy as np
import time

cam = cv2.VideoCapture(2)  # camera index (default = 0) (added based on Randyr's comment).

print 'cam has image : %s' % cam.read()[0] # True = got image captured. 
                                           # False = no pics for you to shoot at.

# Lets check start/open your cam!
if cam.read() == False:
    cam.open()

if not cam.isOpened():
    print('Cannot open camera')

while True:
    ret,frame = cam.read()
    cv2.imshow('webcam', frame)
    if cv2.waitKey(1)&0xFF == ord('q'):
        break

cam.release()
cv2.destroyAllWindows()

您还可以使用 cam = cv2.VideoCapture(value) 值.. 现在设置为 2。尝试一个范围......例如1-10.