简体   繁体   English

AttributeError:模块“cv2”没有属性“imread”

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

(i'm on mac os 10.8.5) (我在 mac os 10.8.5 上)

I'm using Python 3 (through jupyter notebook) and trying to import cv2我正在使用 Python 3(通过 jupyter 笔记本)并尝试import cv2

I did import cv2 succefully, but when I type im_g = cv2.imread("smallgray.png", 0) I get this error:我确实成功import cv2 ,但是当我键入im_g = cv2.imread("smallgray.png", 0)时,出现此错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-5eb2880672d2> in <module>()
----> 1 im_g = cv2.imread("smallgray.png", 0)

AttributeError: module 'cv2' has no attribute 'imread'

I also checked dir(cv2) and I get:我还检查了dir(cv2) ,我得到:

['__doc__', '__loader__', '__name__', '__package__', '__path__', '__spec__'

I guess a lot of functions are missing... Is it due to a wrong opencv installation?估计少了很多功能。。。是不是opencv安装错了? Actually I struggled a lot to get opencv and I guess I installed it 'too many times' and different ways via Terminal.实际上,我费了很大劲才得到 opencv,我想我安装它“太多次”了,而且是通过终端以不同的方式安装的。 ( brew , pip ) brewpip

Should I uninstall opencv and start over?我应该卸载 opencv 并重新开始吗? How can I do this properly?我怎样才能正确地做到这一点?

Thx in advance提前致谢

It might be the case that you installed it in the wrong way.可能是您以错误的方式安装它。 In my case as well, I installed OpenCV wrongly so I uninstalled it completely then reinstalled it which caused it to work.在我的情况下,我也错误地安装了 OpenCV,所以我完全卸载了它,然后重新安装它导致它工作。

Please notice the sequence of uninstalling and installing:请注意卸载和安装的顺序:

Uninstall:卸载:

pip uninstall opencv-python
pip uninstall opencv-contrib-python

Install:安装:

pip install opencv-contrib-python
pip install opencv-python

我也有这个问题,我解决了,问题出在我把我的脚本命名为 cv2.py,我只是把它重命名为别的东西。

Reader's problem could be, that a wrong library (cv2 package) has been installed.读者的问题可能是安装了错误的库(cv2 包)。 I installed opencv-python3 instead of opencv-python for example.例如,我安装了opencv-python3而不是opencv-python

So in case you have PyCharm IDE, go to File->Settings->Project: *->Python Interpreter and see what you have listed there:因此,如果您有 PyCharm IDE,请转到 File->Settings->Project: *->Python Interpreter 并查看您在此处列出的内容:

在此处输入图像描述

Make sure, you have installed opencv-python.确保你已经安装了 opencv-python。 If you have also other similar names, it should be fine - in my case it works.如果您还有其他类似的名称,那应该没问题 - 在我的情况下它可以工作。

Do cv2.cv2.imread()cv2.cv2.imread()

This should work in most of the cases.这应该适用于大多数情况。 Also, take a look here .另外,看看这里

This might happen if you have named one of your files as 'cv2.py'.如果您将其中一个文件命名为“cv2.py”,则可能会发生这种情况。 So when you 'import cv2', the system accesses your file instead of the actual library.因此,当您“导入 cv2”时,系统会访问您的文件而不是实际的库。 So try renaming cv2.py' to any other name and your code should work fine.因此,尝试将 cv2.py' 重命名为任何其他名称,您的代码应该可以正常工作。

Check:查看:

brew list | grep opencv

If it doesn't installed then try:如果未安装,请尝试:

brew install opencv

it works with me:它适用于我:

pip install opencv-python点安装 opencv-python

pip install opencv-contrib-python点安装 opencv-contrib-python

source : https://pypi.org/project/opencv-python/来源: https ://pypi.org/project/opencv-python/

I had the same issue, this turned out to solve the problem:我遇到了同样的问题,结果解决了这个问题:

from cv2 import cv2
im_g=cv2.imread("smallgray.png", 0)
print(im_g)

I faced Similar issue,On Ubuntu我遇到了类似的问题,在 Ubuntu
I had installed open-cv using the comand:我使用以下命令安装了 open-cv:

sudo pip3 install opencv-python3

I Uninstalled opencv, By:我卸载了opencv,作者:

sudo pip3 uninstall opencv-python3

Then reinstalled it Using the following Code:然后使用以下代码重新安装它:

pip3 install opencv-python

I also faced this problem.我也遇到过这个问题。
And get a solution by changing the current cv2.py to cv.py (You can name whatever you like except the name of packages in use).并通过将当前的cv2.py更改为cv.py来获得解决方案(您可以命名任何您喜欢的名称,除了正在使用的包的名称)。
Little Explanations小解释

Saving the current working file as the name of cv2.py , when we try to run the file it imports the current file with import cv2 statement, not actual cv2 module.将当前工作文件保存为cv2.py的名称,当我们尝试运行该文件时,它会使用import cv2语句导入当前文件,而不是实际cv2模块。 This whole thing is causing this whole Error.这整件事导致了整个错误。

The main problem is that you have your file or folder named "cv2", it is not permitted in Python.主要问题是您的文件或文件夹名为“cv2”,在 Python 中是不允许的。 The way to solve is change the name and then your code will be run解决的方法是更改​​名称然后运行您的代码

The same issue has recently appeared within my code and had no idea of what's happening.最近我的代码中出现了同样的问题,并且不知道发生了什么。 Tried different methods such as uninstalling opencv-python .尝试了不同的方法,例如卸载opencv-python

Coming to the naming of the file that I am working with, I definitely had no problem that might cause a collapse with the name of the library.谈到我正在使用的文件的命名,我绝对没有可能导致库名称崩溃的问题。

I have deleted the opencv-python , cleaned all of the package-related caches, and then reinstalled the opencv-python and opencv-contrib-python .我已经删除了opencv-python ,清理了所有与包相关的缓存,然后重新安装了opencv-pythonopencv-contrib-python Now, it is working fine and I can say that no issues have appeared yet.现在,它工作正常,我可以说还没有出现任何问题。

PS I am using Arch Linux. PS 我正在使用 Arch Linux。 If you wonder about how to clear caches, try looking at this source .如果您想知道如何清除缓存,请尝试查看此来源 I hope it helps.我希望它有所帮助。 Keep learning and exploring, thanks!继续学习探索,谢谢!

I personally had to uninstall all previous installations dealing with opencv:我个人不得不卸载所有以前处理 opencv 的安装:

pip uninstall opencv-python
pip uninstall opencv-contrib-python
pip uninstall opencv-contrib-python-headless

Then I deleted all files that had to do with opencv too in site-package after the uninstallation, files related to opencv like opencv_contrib_python-4.6.0.66.dist-info remained and caused errors.然后我在卸载后在站点包中删除了所有与 opencv 相关的文件,与 opencv 相关的文件如opencv_contrib_python-4.6.0.66.dist-info仍然存在并导致错误。

Once all these files were deleted I reinstalled opencv and opencv-contrib:删除所有这些文件后,我重新安装了 opencv 和 opencv-contrib:

pip install opencv-contrib-python
pip install opencv-python

now it works for me现在它对我有用

using cv2 on vscode i was having this problem but nothing worked.在 vscode 上使用 cv2 我遇到了这个问题,但没有任何效果。 Turns out that i named my file as "cv2.py" so it was importing itself原来我将我的文件命名为“cv2.py”所以它正在导入自己

"Generally, the Python Circular Import problem occurs when you accidentally name your working file the same as the module name and those modules depend on each other. This way the python opens the same file which causes a circular loop and eventually throws an error." “通常,Python 循环导入问题发生在您不小心将工作文件命名为与模块名称相同并且这些模块相互依赖时。这样 python 打开相同的文件,导致循环并最终引发错误。”

Perhaps you have just installed opencv-contrib-python and not opencv-python .也许您刚刚安装了opencv-contrib-python而不是opencv-python

Try this out, it worked for me-试试这个,它对我有用-

  1. First uninstall:第一次卸载:
pip uninstall opencv-python
pip uninstall opencv-contrib-python
  1. then install:然后安装:
pip install opencv-contrib-python==3.4.2.16
pip install opencv-python==3.4.2.16

You can refer here .你可以参考这里

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

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