6

(i'm on mac os 10.8.5)

I'm using Python 3 (through jupyter notebook) and trying to import cv2

I did import cv2 succefully, but when I type im_g = cv2.imread("smallgray.png", 0) I get this error:

---------------------------------------------------------------------------
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:

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

I guess a lot of functions are missing... Is it due to a wrong opencv installation ? Actually I struggled a lot to get opencv and I guess I installed it 'too many times' and different ways via Terminal. (brew, pip)

Should I uninstall opencv and start over ? How can I do this properly ?

Thx in advance

Samuel
  • 69
  • 1
  • 2
  • 8

12 Answers12

6

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.

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
Elletlar
  • 2,991
  • 7
  • 29
  • 34
Satyam Annu
  • 87
  • 1
  • 4
3

I was have this problem too, and i solved it, the problem was been in i named my script cv2.py, i'm just rename it to something else.

KR1470R
  • 71
  • 7
2

Reader's problem could be, that a wrong library (cv2 package) has been installed. I installed opencv-python3 instead of opencv-python for example.

So in case you have PyCharm IDE, go to File->Settings->Project: *->Python Interpreter and see what you have listed there:

enter image description here

Make sure, you have installed opencv-python. If you have also other similar names, it should be fine - in my case it works.

Sold Out
  • 1,039
  • 11
  • 28
  • In case you uninstall some other package (like opencv-python3), likely this step will corrupt your dependencies and you may get that error again. In such a case just uninstall and install the opencv-python again.. – Sold Out Oct 16 '20 at 15:40
1

Do cv2.cv2.imread()

This should work in most of the cases. Also, take a look here.

lucians
  • 2,131
  • 3
  • 31
  • 61
  • 1
    I tried and this is what I get: `--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in () ----> 1 im_g = cv2.imread("smallgray.png", 0) AttributeError: module 'cv2' has no attribute 'imread'` I also tried to rename `cv2.so` (in /site packages/cv2/) to `cv2.py` and got the same error. – Samuel Dec 17 '17 at 20:47
  • @lotusam try do the installation again – lucians Dec 17 '17 at 21:57
1

This might happen if you have named one of your files as 'cv2.py'. So when you 'import cv2', the system accesses your file instead of the actual library. So try renaming cv2.py' to any other name and your code should work fine.

0

Check:

brew list | grep opencv

If it doesn't installed then try:

brew install opencv
0

it works with me:

pip install opencv-python

pip install opencv-contrib-python

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

Jawad
  • 147
  • 10
0

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)
0

I faced Similar issue,On Ubuntu
I had installed open-cv using the comand:

sudo pip3 install opencv-python3

I Uninstalled opencv, By:

sudo pip3 uninstall opencv-python3

Then reinstalled it Using the following Code:

pip3 install opencv-python
0

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).
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. This whole thing is causing this whole Error.

Pijush Barman
  • 31
  • 1
  • 5
0

The main problem is that you have your file or folder named "cv2", it is not permitted in Python. The way to solve is change the name and then your code will be run

-1

Perhaps you have just installed opencv-contrib-python and not 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.

Grayrigel
  • 3,112
  • 5
  • 13
  • 25
Subhodeep
  • 33
  • 1
  • 6