cv2 videoCapture没有属性

问题描述

import cv2

import sys


cpt=0

vidStream=cv2.videoCapture(0)
while True:
    ret,frame=vidStream.read()

    cv2.imshow("test frame",frame)
    cv2.imwrite(r"C:\Users\Abhishek\PycharmProjects\images\0\image%04i.jpg"%cpt,frame)
    cpt +=1

    if cv2.waitKey(10)==ord('q'):
        break

任何人请帮助我修复错误 我收到一条错误消息,内容为“模块'cv2.cv2'没有属性'videoCapture'”和“无法导入cv2”

解决方法

1)不是cv2.videoCapture(),而是尝试cv2.VideoCapture()。

Python 2.7.18rc1 (default,Apr  7 2020,12:05:55) 
[GCC 9.3.0] on linux2
Type "help","copyright","credits" or "license" for more information.
>>> import cv2
>>> vid = cv2.videoCapture(0)
Traceback (most recent call last):
  File "<stdin>",line 1,in <module>
AttributeError: 'module' object has no attribute 'videoCapture'
>>> vid = cv2.VideoCapture(0) #like that it works fine.
>>> 

尝试使用命令pip install opencv-pythonpip install opencv-contrib-python安装 opencv ,并检查是否仍然出现“无法导入cv2”错误

,

请安装

pip install opencv-pythonpip install opencv-contrib-python,然后运行以下代码。...

import cv2
import sys
cpt=0
vidStream=cv2.videoCapture(0)
while True:
    ret,frame=vidStream.read()
    cv2.imshow("test frame",frame)
    cv2.imwrite(r"C:\Users\Abhishek\PycharmProjects\images\0\image%04i.jpg"%cpt,frame)
    cpt +=1
    if cv2.waitKey(10)==ord('q'):
        break