Reputation: 101
So i'm doing a little personal project but i keep getting this error when I try to create the recognizer. i have opencv-contrib and everything. Does anyone know whats going on? code posted below
import cv2, os
import numpy as np
from PIL import Image
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)
recognizer = cv2.face.createLBPHFaceRecognizer()
it gets caught on that last line. I've tried reinstalling all modules already. Not really sure what else to do. The weird thing is it works on my laptop but not my desktop. They both have the same modules, same python release and running the exact same code.
Upvotes: 10
Views: 105795
Reputation: 1
OpenCV has removed the cv2.face module from version 4.4.0 onwards. This module was used for face recognition and included algorithms such as Eigenfaces, Fisherfaces, and LBPH (Local Binary Patterns Histograms).
Instead of using the cv2.face module, OpenCV now recommends using the face_recognition library, which is a third-party library that provides a simple interface for face recognition tasks. This library uses dlib's implementation of face recognition algorithms, which is more accurate and faster than the algorithms included in the cv2.face module.
You can install the face_recognition library using pip, by running the following command in your terminal or command prompt
pip install face_recognition import face_recognition
Upvotes: 0
Reputation: 1
Latest Update:(2023)
Got same error after alot of research on this Find the exact solution
First of all install fresh opencv-contrib-python
As it has cv2.face.
And if you already opencv-python
installed too? they conflict.
Uninstall the opencv-python
and pick opencv-contrib-python
to get your error will get resolve.
pip install opencv-contrib-python
NOTE: Make sure to remove opencv-python
from your project before installing opencv-contrib-python
Upvotes: 0
Reputation: 1
clf=cv2.face.LBPHFaceRecognizer_create() AttributeError: module 'cv2' has no attribute 'face'
I also got this error... This is because your opencv module is out of date.
so please uninstall opencv:
pip uninstall opencv-python
and reinstall opencv using this command:
pip install opencv-contrib-python
and replace this line to:
recognizer = cv2.face.LBPHFaceRecognizer.create()
Upvotes: 0
Reputation: 1
The Library can change how we access methods or the method names by time. So, keep an eye for that.
Here's the latest one:
recognizer = cv.face.LBPHFaceRecogniser.create()
Upvotes: 0
Reputation: 31
OpenCV has removed the cv2.face
module from version 4.4.0 onwards. This module was used for face recognition and included algorithms such as Eigenfaces, Fisherfaces, and LBPH (Local Binary Patterns Histograms).
Instead of using the cv2.face
module, OpenCV now recommends using the face_recognition
library, which is a third-party library that provides a simple interface for face recognition tasks. This library uses dlib's implementation of face recognition algorithms, which is more accurate and faster than the algorithms included in the cv2.face
module.
You can install the face_recognition
library using pip, by running the following command in your terminal or command prompt
pip install face_recognition
import face_recognition
Upvotes: 3
Reputation: 107
I am also facing an same issue after installation of these below command in my command prompt.
> pip install opencv-contrib-python
> pip install opencv-contrib-python --user
Facing this below error in jupyter notebook.
Training done ---------------
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_4424/3540845204.py in 40 labels = np.array(labels) 41 ---> 42 face_recognizer = cv.face.LBPHFaceRecognizer_create() 43 44 # Train the Recognizer on the features list and the labels list
AttributeError: module 'cv2' has no attribute 'face'
After i am tried with visual studio code but make sure your running with this below confirmation commands in your command prompt or terminal.
I will suggest you to try in command prompt.
Microsoft Windows [Version 10.0.19043.1348] (c) Microsoft Corporation. All rights reserved. C:\Users\Admin>python Python 3.10.1 (tags/v3.10.1:2cd268a, Dec 6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> print(cv2.__version__) 4.5.5 >>> **recognizer = cv2.face.LBPHFaceRecognizer_create()** >>> **print(recognizer)** **<face_LBPHFaceRecognizer 000001F890F3D8D0>** >>>
After successfully fixed this issue, i have got these below outputs.
on.exe' 'c:\Users\Admin\.vscode\extensions\ms-python.python-2021.12.1559732655\pythonFiles\lib\python\debugpy\launcher' '55275' '--' 'c:\Users\Admin\Downloads\Opencv\Faces\face.py' > Training done --------------- PS C:\Users\Admin\Downloads\Opencv\Faces> c:; cd 'c:\Users\Admin\Downloads\Opencv\Faces'; & 'C:\Program Files\Python310\pyth0' '--' 'c:\Users\Admin\Downloads\Opencv\Faces\face_recognization1.py' > Label = Elton John with a confidence of 0.0 > Label = Elton John with a confidence of 0.0 on.exe' 'c:\Users\Admin\.vscode\extensions\ms-python.python-2021.12.1559732655\pythonFiles\lib\python\debugpy\launcher' '55339' '--' 'c:\Users\Admin\Downloads\Opencv\Faces\face_recognization1.py' > Label = nidhi agarwal with a confidence of 0.0
Upvotes: 0
Reputation: 1
There are some missing modules for contributed libraries in the default pip install opencv-python-contrib
so you need pip install opencv-contrib-python --upgrade
This will download a 41.5MB file and install that in your python package folder , after that you can check by simply First "import cv2" then typing "print(dir(cv2.face))" that will return a list of attributes in cv2.face which will include "createlbphfacerecognizer"
Upvotes: 0
Reputation: 1
write
pip install opencv-contrib-python --upgrade
if gives error then write
pip install opencv-contrib-python --user
it will work fine for cv2.face.LBPHFaceRecognizer_create()
Upvotes: 0
Reputation: 1
If you are using python3 use pip3 instead of pip:
pip3 install opencv-contrib-python
Then you can simply do:
recognizer = cv2.face.LBPHFaceRecognizer_create()
Upvotes: 0
Reputation: 831
Installing opencv-contrib-python solved the issue. I had installed only opencv-python, After Installing opencv-contrib-python, solved the issue.
pip install opencv-contrib-python
Upvotes: 0
Reputation: 21
try this it worked for me
pip install --force-reinstall opencv-contrib-python==4.1.2.30
Upvotes: 2
Reputation: 11
Upvotes: 1
Reputation: 59
Even I had this problem when I ran the code using Jupyter Notebook. If you are running it on Jupyter Notebook , then download it as pyhton(.py) file and try to run it in Anaconda Prompt or Command Prompt. This solved the problem for me. Thank You.
Upvotes: 0
Reputation: 13
I fixed this issue by installing:
sudo pip install opencv-contrib-python
.
Then look for correct format.
python2 is default
Basically the problem is that python3 and python2 have different format of code.
recognizer = cv2.face.LBPHFaceRecognizer_create()
This is the format of python2.
recognizer = cv2.face.createLBPHFaceRecognizer()
This is the format of python3
Upvotes: 0
Reputation: 2410
Under Windows 7, I was able to resolve the issue by simply uninstalling and re-installing opencv:
pip uninstall opencv-contrib-python
pip install opencv-contrib-python
The recogniser is called by:
recognizer = cv2.face.LBPHFaceRecognizer_create()
Upvotes: 9
Reputation: 722
I fixed this issue with two commands:
First: sudo pip3 uninstall opencv-contrib-python
Second: sudo python3 -m pip install opencv-contrib-python==3.3.0.9
This fixed my issues. Hope it helps somebody! Also, if you are using python2, replace “pip3” with “pip” and “python3” with “python”
Upvotes: 1
Reputation: 63
I had this problem running opencv Version 3.4.1. Here is what I did.
SPECS: Raspberry pi 3B, OS: Raspbian, Version: 9 (Stretch), Python 3, opencv Version 3.4.1
Check opencv version in python
import cv2
cv2.__version__
1) sudo pip install opencv-contrib-python
*After this I could not import cv2 in python until I installed the following.
2) sudo apt-get update
3) sudo apt-get install libhdf5-dev
4) sudo apt-get update
5) sudo apt-get install libhdf5-serial-dev libqtgui4 libqt4-test
Upvotes: 1
Reputation: 1525
There are some missing modules for contributed libraries in the default pip install opencv-python
so you need pip install opencv-contrib-python
Upvotes: 9
Reputation: 11
Try to use this:
import cv2
import os
import numpy as np
from PIL import Image
# Path for face image database
path = 'dataset'
recognizer = cv2.face_LBPHFaceRecognizer.create()
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
Upvotes: 1
Reputation: 21
Try to update your opencv by "python -m pip install opencv-contrib-python" ps: you have to delete the CV2 repository from the Python rep and then run this command (in the CMD windows) if it doesn't work
Upvotes: 1
Reputation: 241
Had the same problem. Use:
recognizer = cv2.face.LBPHFaceRecognizer_create()
It worked for my program.
Upvotes: 24
Reputation: 330
You are using Opencv 3.x, in the new version few modules has been removed. You have two options: 1. Add opencv_contrib module to your existing opencv 3.x version. Here's the link https://github.com/opencv/opencv_contrib 2.you can use older versions of Opencv. Like opencv 2.4.x
Upvotes: 0