当代码检测到网络摄像头时,cv2模块没有捕获图像



我使用的代码是:

import cv2
camera = cv2.VideoCapture(0)
im=camera.read()[1]
print im

我得到的输出为
在某些情况下,它会返回RGB值,但不是每次我想要的时候
在哪些情况下,它将返回None??

您的问题是:

在哪些情况下,它将返回None??

这可以很容易地在VideoCapture的文档中找到。对于读取的功能,它表示:

方法/函数将VideoCapture::grab()和VideoCapture::retrieve()。这是最方便的用于从解码读取视频文件或捕获数据的方法,以及返回刚刚抓取的框架。I如果没有捕获任何帧(相机已断开连接,或者视频文件中没有更多帧),方法返回false,函数返回NULL指针

因此,连接到您的相机似乎是个问题。

import cv2
cv2.namedWindow('webCam')
cap = cv2.VideoCapture(0)

if cap.isOpened():
    ret, frame = cap.read()
else:
    ret = False
    print "problem here"

while True:
    #get frames
    ret,frame = cap.read()
    frame = cv2.flip(frame,1)  # flip image 
    cv2.imshow('webCam', frame)  # show cam
    # to exit
    esc = cv2.waitKey(5) & 0xFF == 27
    if esc:
        break
cap.release()
cv2.destroyAllWindows()

最新更新