【问题标题】:Python OpenCV gives black screen on webcam capturePython OpenCV 在网络摄像头捕获时出现黑屏
【发布时间】:2017-10-01 05:12:52
【问题描述】:

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

【问题讨论】:

  • 您确定cv2.VideoCapture(0) 是您的网络摄像头吗?可以试试cv2.VideoCapture(1)
  • 我确定是 0
  • 你可以试试print "{} x {}".format(cap.get(3), cap.get(4))显示宽高看看是否有效。
  • 这段代码运行良好。更重要的是,我可以使用 cap.set(3,320)、cap.set(4,240) 更改输出图像的大小,但我仍然看到黑色图像。
  • 不知道,上次我遇到这个问题时,封面在网络摄像头上。

标签: python python-2.7 opencv


【解决方案1】:

我今天在使用 gocv.io 库的 Windows 上也遇到了这个问题。

我删除并重新安装了网络摄像头驱动程序,问题解决了。

【讨论】:

    猜你喜欢
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    • 2011-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多