Why am I getting " module 'cv2' has no attribute 'resize'" - python

I am using the following line in my program
img = cv2.resize(img, dsize=(299, 299), interpolation=cv2.INTER_LINEAR)
It is giving the following error
AttributeError: module 'cv2' has no attribute 'resize'
The type of image img is <class 'imageio.core.util.Array'>
I checked the official documentation of OpenCV and it contains the attribute resize.
Where am I going wrong?

The issue occurred due to the erroneous installation of OpenCV.
Although the system is behaving as OpenCV was properly installed. It wasn't installed properly.
Uninstalling and installing OpenCV again solved the issue.
pip uninstall opencv-python
and then
pip install opencv-python

Maybe your cv2 import is not correct. In a Python console, try
import cv2
help(cv2.resize)
In case it does not show the description of the resize method, the import does not work properly. Your could also check help(cv2)which gives a long list of all methods and attributes contained in the module.
Are you working within a virtual environment that needs activation first?

Related

How to make the opencv-contrib module work?

I followed this guide to install opencv (version 3.4.4) and the contrib modules because I want to work with the SIFT algorithm.
https://www.pyimagesearch.com/2018/05/28/ubuntu-18-04-how-to-install-opencv/
When I noticed, it was not working as expected after the installation, I deleted the build folder and tried again, but this did not work as well.
I imported like this:
import cv2 as cv
I tried to use SIFT (in python 3.6) in the following ways:
sift = cv.SIFT_create()
sift = cv.xfeatures2d.SIFT_create()
sift = cv.SIFT()
sift = cv.xfeatures2d.SIFT()
As this usually results in errors like this: Attribute Error: module cv2 has no attribute 'SIFT_create' (same thing happens for the other 3 options), I figure that I am either using it in an incorrect way or the installation process itself did not work properly.
After this, I found the pip install opencv-contrib-python and used it. With no results at all.
I would really appreciate some hints on how I can make opencv with contrib modules work.
You may have to say cv2.xfeatures2d_SIFT or similar. the modules of OpenCV don't necessarily map to python submodules.
Since the patent on SIFT expired in 2019, OpenCV moved it back into features2d (main repo) from xfeatures2d (opencv_contrib repo). Please use the most recent 3.4.x release, or 4.x.

AttributeError: partially initialized module 'cv2' has no attribute 'CascadeClassifier' (most likely due to a circular import)

I've been going round in circles for ages trying to figure this out. Why am I getting this attribute error? I've tried using absolute references, and get the same issue. PyCharm is also highlighting CascadeClassifier, cvtColor and COLOR_BGR2GRAY saying it cannot find reference in cv2.py. I'm not sure if more information is relevant to solving this problem, so please ask if more is needed.
import cv2
face_cascade = cv2.CascadeClassifier('read_only/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('read_only/haarcascade_eye.xml')
grayed_images = []
for x in np_images:
gray_img = cv2.cvtColor(x, cv2.COLOR_BGR2GRAY)
grayed_images.append(gray_img)
print(x)
If I am correct, you are using an environment different from where you run.
Step1: In command line/terminal where you see the opencv-python when you run pip list:
run python command. Copy your code, check if it works (you can simply import cv2 alternatively)
If it works, my idea should be correct. Otherwise, there is something bigger.
Step 2: (Assuming step1 works.) In Pycharm, Under Run > Edit Configurations, change python interpreter to whichever interpreter is you are that has opencv.
Step2 better alternative: On Pycharm, open the terminal, pip install opencv-python. After that you should have the opencv.
Downgrade opencv to the version 4.5.5.64.
You can install opencv using
pip install opencv-python==4.5.5.64.
This works for me

Opencv contrib face library

I am trying to use LBPHfacerecognizer in my python3.6 code.
recognizer = cv2.face.createLBPHFaceRecognizer()
I am aware that face module is in opencv_contrib module. I even uninstalled openCV and installed again with contrib module. I compiled OpenCV with it and python still gives an error which is:
AttributeError: module 'cv2.cv2' has no attribute 'face'
Although in opencv/build/lib I have libopencv_face.dylib, it doesn't compile with python3 and opencv.
I tried everything I can think of but I am running out of ideas.
Any ideas?
edit: Python doesn't give an error when I am working under python3 environment anymore but I still have the same error when I try to run my code in the terminal.
What I am trying to run:
python3 main.py
Any ideas why I have this problem or how I can fix it?
Firstly, the first mistake of mine was after making cmake for few times, I didn't delete old cv2.so file and replace it with new cv2.so.
And it turned out that for working opencv_contrib face module, I have to put this new cv2.so file in the same file that my main.py exists. I don't know if this is the way how should be in the first place. I totally find out this coincidently by myself.
After this, my code worked without any error.
recognizer = cv2.createLBPHFaceRecognizer();
This makes error for me
I tried out, just view the image here.
Try out the code below, it may give you result.
recognizer = cv2.face.LBPHFaceRecognizer_create();

No module named cv

I'm trying to run this script:
https://github.com/openalpr/train-detector/blob/master/crop_plates.py
I've never dealt with python before. First of all I had some syntax errors in the print lines. They were lacking parentheses...but I'm not sure if I get this error because of the Python version I installed or this was indeed a syntax error. After fixing the syntax error I'm receiving the following error:
C:\>py crop_plates.py
Traceback (most recent call last):
File "crop_plates.py", line 7, in <module>
import cv2, cv
ImportError: No module named cv
I did the following:
Installed Python 2.7.5 32 bits.
Installed numpy 1.9.1 32 bits (also tried different versions)
Installed matplotlib 1.3.0.
Installed OpenCv
Copied cv2.pyd from
C:\Users\Me\Downloads\opencv\build\python\2.7\x86
to
C:\Python27\Lib\site-packages
But I can't find anywhere the cv.pyd file that I'm missing.
I found references to this guide for installing Opencv but it's not working
http://opencv.willowgarage.com/documentation/python/
Thank you very much,
Edit: I'm running Python in Windows 7.
For ImportError: No module named cv, try installing opencv-python module using below command:
pip install opencv-python
cv2.cv doesn't exists in OpenCV 3.0. Use simply cv2
Also, the attribute is no more CV_HOUGH_GRADIENT but HOUGH_GRADIENT
So what you looking for is probably:
cv2.HOUGH_GRADIENT
OK , I searched for a while and find the following :
First: you may write: import cv2.cv as cv to solve your proplem
cv and cv2 are both interface for python , and now , the newest "cv2" is the most used one , but clearly some libraries still use cv and others (like yours) use mix of the two,What is different between all these OpenCV Python interfaces?
It seems this code is using a old version of OpenCV. Unfortunately there is no mention of the exact version they used in their repo.
As the code is at least 2 years old, I would install opencv version 2.4.
pip install opencv-python==2.4

PIL Module / Could not initialize images API

I'm running OSX and Python 2.7. The app I have requires the PIL module, which gives me this error when I try to install it.
I've tried several installations but nothing seems to do the trick. How do I get this to work?
WARNING 2012-04-18 18:15:54,222 dev_appserver.py:3394] Could not initialize images API; you are likely missing the Python "PIL" module. ImportError: dlopen(/Library/Python/2.7/site-packages/PIL/_imaging.so, 2): Symbol not found: _jpeg_resync_to_restart
Referenced from: /Library/Python/2.7/site-packages/PIL/_imaging.so
Expected in: flat namespace
The answer to this was uninstalling everything and then using pip install pil.

Categories

Resources