Opencv contrib face library - python

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();

Related

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

I can't create an recognizer using the lastest opencv version

I'm' trying to build a face recognition project with python. But I've found a problem, I can't instantiate the recognizer because when I try execute this line:
recognizer = cv2.face.LBHFaceRecognizer_create()
I got this error:
AttributeError: module 'cv2.cv2' has no attribute 'face'
So I've searched for a solution in the web, much of them, suggested to execute this command on terminal:
pip install opencv-contrib-python
it doesn't solve the problem, I've tried to reinstall to but the problem remains. I've seek information about the documentation but it looks that there's a miss of information about how to create an recognizer in the web.Do Someone know how to solve the problem?
It works for me with a slight modification:
recognizer = cv2.face.LBPHFaceRecognizer_create()
I added a P after LB.
I have a working example here
If someone knows how to solve the problem this post shows exactly the solution for this problems step by step, it's only follow the first answer:
No module named 'cv2.cv2'

How to fix 'ImportError: dynamic module does not define module export function (PyInit_cv2)' error in Python?

I'm running a code on deep learning, which uses the opencv module, by running python main.py (contains import cv2 statement), but always get the error 'ImportError: dynamic module does not define module export function (PyInit_cv2)'.
I've tried to reinstall my anaconda and create new virtual environments, but all got the same result. This problem really confuses me a lot and I've googled for many related problems, none of them works. I think the problem is something related to the environment and has nothing to do with the code, because I got the same result by simply run import cv2 in python prompt. The more confusing thing is that, even after I remove the opencv module, I also get the same problem, but not a ModuleNotFoundError. Does anyone can give me some advice? Thanks a lot!
I think I found one possible reason of this error.
Recently I was configuring the caffe environment on one server, I downloaded the source code of opencv-2.4.13 and compiled manually, added /usr/local/opencv-2.4.13/build/lib to $PYTHONPATH, and caffe worked well. After that, when I entered one of my virtual environment using conda activate py35, which uses python3.5, tried import cv2 in the python prompt, got the error above.
I'm not sure but I think the cause of the error is opencv-2.4.13 compiles a python2 interface so it can't be imported by python3. Python imports packages by searching the directories listed in sys.path, where $PYTHONPATH is in the second place after the current working directory (This is a great article introduces the mechanism of python finding packages). So when we enter the py35 environment, python will first look for $PYTHONPATH and find the opencv installed on the root directory instead of finding the opencv in the virtual environment using conda install opencv-python.
So there are two solutions of this problem:
Use python2 instead.
Remove /usr/local/opencv-2.4.13/build/lib from $PYTHONPATH.
which all work for me.
Similar post, might help:
ImportError: dynamic module does not define init function (initfizzbuzz)
Could you provide info on how you installed the CV module?
I had the same problem, which was caused by the cv2.so file in /usr/local/lib/python2.7/site-packages/cv2.so. After I deleted the file and use command sudo pip3 install opencv-python, it worked for python3.

No module named itk (Insighttolkit) in Python

I am novice in python and Simple_ITK both to process .mha 3D image files. I have two problems:
When ever I install ITK, I always get an error as shown below:
and somehow I install ITK It doesn't work at all having no connectivity with python. And I also tried python-insighttoolkit3 package then it shows and error of
" Can't down load Python 2.8:i386" in Ubuntu software Center. I have tried various methods to install Insight Tool Kit in Ubuntu whatever I found but most of the time I get the same error as shown in picture above.
So can anybody guide me how to install and build ITK so that no error like no module named itk found.
Now another problem is I have a .py file and it has a class in which I have to fed input externally otherwise it will consider as None. SO I tried to call the file as
import /path/to/file/xxx.py
but it gives an Syntax error: invalid syntax
so how to call class from a python file from python Console .
Now, I'm using Ubuntu 15.02. For any missing details please ask.
Thanks.
1- For what I see in the image, I see that you are trying to install ITK instead of SimpleITK. For SimpleITK it is very simple using a linux package installer, like pip or easy-install. in this link you have the instructions to install it correctly. Remember to execute the instructions as super user. Maybe it is the reason of your error ( I can't see the instruction you are writing in the console ).
2- I recommend you to read the Python tutorial for modules The fastest way is to be located first on the directory of your .py file. The sequence would be:
cd /path
import module.py

Python OpenCV: ImportError Module Not Found

I had installed OpenCV, and it was working just fine. But I noticed that a few libraries were missing.
So I decided to install another copy. I moved the working copy to another directory, and started the install of the new one. I was having trouble with this new install, so I cancelled it and moved my old OpenCV install back to its original directory.
But now, my old install doesn't work anymore. Each time I try to run something, it says "ImportError: No module named cv2" or "ImportError: No module named cv" (depending on what was being imported in the file).
What do I do to fix this? I remember encountering a similar issue the first time I installed and ran this copy of OpenCV, and something had to be changed with the Python Path to work correctly. I can't quite remember, and I'm kind of confused about why those settings would change, if they indeed have.
Please let me know what I can do to rectify this. I am using OS X 10.9. Thank you.
UPDATE: I added this to my .bash_profile: export PYTHONPATH=/Users/santosh/opencv-2.4.7/build/lib:$PYTHONPATH. But now when I run opencv samples, I get "Segmentation fault: 11". Any thoughts?

Categories

Resources