简体   繁体   English

AttributeError:模块 'cv2' 没有属性 'VideoCapture'

[英]AttributeError: module 'cv2' has no attribute 'VideoCapture'

I had some problems with Opencv in Python.我在 Python 中遇到了 Opencv 的一些问题。

This attribute problem also happens with imread imread也会出现此属性问题

I tried to uninstall and reinstall with contrib-Opencv,but it did not work.我尝试使用 contrib-Opencv 卸载并重新安装,但没有成功。

About 2 months ago, my opencv file still worked well, but I don't know why it doesn't work now.大约 2 个月前,我的 opencv 文件仍然运行良好,但我不知道为什么它现在不起作用。

In the next reinstallations,this command always sastifies but no good results在下一次重新安装中,此命令总是满足但没有好的结果

import cv2
cap = cv2.VideoCapture(0)
...
cap.release()

My error command:我的错误命令:

Traceback (most recent call last):
  File "C:\Users\Hoang Cao Chuyen\Documents\pyml\cv11.py", line 4, in <module>
    cap = cv2.VideoCapture(0)
AttributeError: module 'cv2' has no attribute 'VideoCapture'
[Finished in 0.2s with exit code 1]
[shell_cmd: python -u "C:\Users\Hoang Cao Chuyen\Documents\pyml\cv11.py"]

One of the issue I noticed is that you used cv11 as the main.py name.我注意到的一个问题是您使用cv11作为 main.py 名称。

It is very easy for PyCharm to get confused if you have a file saved as cv2.py.如果你有一个保存为 cv2.py 的文件,PyCharm 很容易混淆。 Please check if you have any other similar files with the name cv2.请检查您是否有任何其他名称为 cv2 的类似文件。

Else, try to do this:否则,尝试这样做:

  1. remove OpenCV删除 OpenCV
  2. reinstall using command pip install opencv-contrib-python使用命令pip install opencv-contrib-python

OR或者

try reinstalling ffmpeg as it might be one of the issue尝试重新安装 ffmpeg 因为它可能是问题之一

pip install ffmpeg-python

After asking my friend,i had the solution.It is very easy because i saved this file in Documents path in user which my private name Hoang Cao Chuyen without '_'.I changed into another path,and it was OK.问了我的朋友后,我有了解决方案。这很容易,因为我将这个文件保存在我的私人名称 Hoang Cao Chuyen 没有'_'的用户的文档路径中。我换了另一个路径,没关系。

Add these two lines:添加这两行:

from cv2 import VideoCapture
from cv2 import waitKey

The problem is that your file is called cv2.py so it imports itself and an error occurs, this can be understood from this line.问题是你的文件被称为 cv2.py 所以它自己导入并发生错误,这可以从这一行理解。

Most likely due to a circular import很可能是由于循环导入

Which translates as:翻译为:

Most likely due to circular import很可能是由于循环进口

To fix the problem, rename your file要解决此问题,请重命名您的文件

I had the same problem and was searching for ages.我有同样的问题,正在寻找年龄。 Finally I found a way that is (at least currently) working for me.最后,我找到了一种(至少目前)对我有用的方法。

When installing opencv there will be a folder 'cv2' which again includes the package 'cv2'.安装 opencv 时,会有一个文件夹“cv2”,其中再次包含 package“cv2”。

Installation directory: C:\Users\user\AppData\Local\Programs\Python\Python39\Lib\cv2 Package directory: C:\Users\user\AppData\Local\Programs\Python\Python39\Lib\cv2\cv2 Installation directory: C:\Users\user\AppData\Local\Programs\Python\Python39\Lib\cv2 Package directory: C:\Users\user\AppData\Local\Programs\Python\Python39\Lib\cv2\cv2

Directory after installation安装后的目录


Probably this leads to confusing for python (maybe some circular import or so).可能这会导致 python 混淆(可能是一些循环导入)。 Therefore I just renamed the installation directory to something else, eg C:\Users\user\AppData\Local\Programs\Python\Python39\Lib\cv2_因此我只是将安装目录重命名为其他目录,例如 C:\Users\user\AppData\Local\Programs\Python\Python39\Lib\cv2_

Directory after renaming重命名后的目录

That is the solution that is currently (and hopefully in the future as well) working for me).这是目前(希望将来也能)为我工作的解决方案)。

The best way to solve all the problems of OPENCV-PYTHON is by uninstalling it and reinstalling it.解决 OPENCV-PYTHON 的所有问题的最佳方法是卸载它并重新安装。

Even I faced the same problem.甚至我也面临同样的问题。

I fixed it by:我通过以下方式修复了它:

python -m pip uninstall Opencv-python

Then I reinstalled it by using a lower version.然后我使用较低版本重新安装了它。 But unfortunately, I did not know the versions of opencv;但不幸的是,我不知道 opencv 的版本; So by using a small trick you can get it by running:因此,通过使用一个小技巧,您可以通过运行来获得它:

python -m pip install opencv-python==

and you will get an error similar to this:你会得到一个类似这样的错误:

.45, 3.4.13.47, 3.4.15.55, 3.4.16.57, 3.4.16.59, 3.4.17.61, 3.4.17.63, 4.3.0.38, 4.4.0.40, 4.4.0.42, 4.4.0.44, 4.4.0.46, 4.5.1.48, 4.5.3.56, 4.5.4.58, 4.5.
4.60, 4.5.5.62, 4.5.5.64)
ERROR: No matching distribution found for opencv-python==

Here you can see all the versions of opencv-python;在这里可以看到opencv-python的所有版本; choose any one (but not the latest as the error occurs due the latest version of opencv-python. install it by using:选择任何一个(但不是最新的,因为由于最新版本的opencv-python而发生错误。使用以下方法安装它:

pip install opencv-python==3.4.17.61 (You can choose your version, but this version solved the issue for me) pip install opencv-python==3.4.17.61 (你可以选择你的版本,但这个版本解决了我的问题)

then enjoy your coding....然后享受你的编码......

Even AUTO-COMPLETE error in opencv-python gets solved.甚至 opencv-python 中的 AUTO-COMPLETE 错误也得到了解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM