系统参数:Windows 8.1、Python 2.7.13、OpenCV 3.2.0.7
这段代码工作正常。我尝试了 .avi 和 .mp4 视频:
import numpy as np
import cv2
import time
cap = cv2.VideoCapture('tmp.avi')
while(True):
ret, frame = cap.read()
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
但是当我尝试从网络摄像头获取图片时,尽管 ret 为 True,但我总是看到黑屏:
import numpy as np
import cv2
import time
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
if not ret: continue
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
我试过使用 grab 和 retrieve 方法而不是 read 方法,所以 grab 返回 True,但 retrieve 返回 ret=False:
import numpy as np
import cv2
import time
cap = cv2.VideoCapture(0)
while(True):
if not cap.grab(): break
ret, frame = cap.retrieve()
if not ret: continue
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
最后的代码适用于视频。
Windows Camera 软件工作正常,因此网络摄像头没问题。
我尝试重新安装 OpenCV,但没有帮助。
问题是什么?为什么 retrieve 方法返回 False 而 read 方法返回 True?
最佳答案
我今天在使用 gocv.io 的 Windows 上也遇到了这个问题图书馆。
我删除并重新安装了网络摄像头驱动程序,问题就解决了。
关于Python OpenCV 在网络摄像头捕获时出现黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43758895/