Python OpenCV 在网络摄像头捕获时出现黑屏

标签 python python-2.7 opencv

系统参数: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/

相关文章:

Python selenium find_element_by_css_selector() 不工作

python - 运行时错误 : module compiled against API version a but this version of numpy is 9

OpenCV 目标检测边界框

c++ - 在 Visual Studio 2010 中安装 Opencv 2.43

python - stanford corenlp 不工作

python - Django - ajax 响应后模板重新加载

python - 如何在 Python 的 setup.py 中包含和安装本地依赖项?

python - 如何使用 python2.7 使用嵌套 for 循环迭代数据帧并附加到新的数据帧列?

python - Google Drive API v3 : service. files().list 未返回所有文件夹

c++ - 关于声明/释放 IplImages 的两种情况