60

I am facing some attribute error while running face recognizing the code. My face detects code run perfectly.But while I try to run the face recognizing code it shows some attribute error. I googled and tried to follow all the steps. But still, it shows the same error. Here is my code:

face recognition

enter link description here

and I get the following error:

C:\Users\MAN\AppData\Local\Programs\Python\Python36\python.exe C:/Users/MAN/PycharmProjects/facerecognition/Recognise/recognize1.py
Traceback (most recent call last):
  File "C:/Users/MAN/PycharmProjects/facerecognition/Recognise/recognize1.py", line 4, in <module>
    recognizer = cv2.createLBPHFaceRecognizer()
AttributeError: module 'cv2.cv2' has no attribute 'createLBPHFaceRecognizer'

Process finished with exit code 1.

I am using Windows platform. python 3.6 version.Thanks in advance.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Manjurul Ahsan
  • 917
  • 2
  • 7
  • 8

15 Answers15

140

You need to install opencv-contrib

pip install opencv-contrib-python

It should work after that.

Željko Krnjić
  • 2,356
  • 2
  • 17
  • 24
20

if you are using python3.x and opencv==4.1.0 then use following commands First of all

python -m pip install --user opencv-contrib-python

after that use this in the python script

cv2.face.LBPHFaceRecognizer_create() 
Priyansh gupta
  • 906
  • 12
  • 10
  • I need this variant but no works on mac os 10.12.6 , gcc --version Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 9.0.0 (clang-900.0.39.2) Target: x86_64-apple-darwin16.7.0 – Nikola Lukic Dec 24 '19 at 12:32
8

You might be running Python3 and therefore you are supposed to use pip3 to install the opencv-contrib package :

pip3 install opencv-contrib-python

This worked for me.

r_a_k
  • 111
  • 2
  • 8
7

Use the following

recognizer = **cv2.face.LBPHFaceRecognizer_create()**

After you install:

pip install opencv-contrib-python

If using anaconda then in anaconda prompt:

conda install pip

then

pip install opencv-contrib-python
kabdulla
  • 5,199
  • 3
  • 17
  • 30
prateek parab
  • 107
  • 1
  • 2
  • 6
  • 4
    do you mean `pip install opencv-contrib-python`? My pip says it does not find that distribution – GlabbichRulz Mar 21 '18 at 21:37
  • am using anaconda3 but still am facing error as below: model = cv2.face.LBPHFaceRecognizer_create() AttributeError: module 'cv2.cv2' has no attribute 'face' – Suraj Bhatt Apr 02 '19 at 06:08
  • @SurajBhatt have you been able to solve it. I'm also facing the same issue – TOLULOPE ADETULA Apr 03 '19 at 06:53
  • @TOLULOPE ADETULA not yet. still facing same error. actually am using anaconda3 not sure whether it conflict with version. If you able to solve pl post. thanks. – Suraj Bhatt Apr 04 '19 at 07:08
  • @SurajBhatt I don't know what I did in particular that solved the issue for me. I updated my OpenCV library and also **pip install opencv-contrib-python**. It first didn't work until I restarted my system – TOLULOPE ADETULA Apr 05 '19 at 05:50
  • @TOLULOPEADETULA Great... Thanks for letting me know. let me try again with conda. – Suraj Bhatt Apr 06 '19 at 07:55
4

opencv has changed some functions and moved them to their opencv_contrib repo so you have to call the mentioned method with:

recognizer = cv2.face.createLBPHFaceRecognizer()

Note: You can see this issue about missing docs. Try using help function help(cv2.face.createLBPHFaceRecognizer) for more details.

Arpit Solanki
  • 9,567
  • 3
  • 41
  • 57
4

I have some problem while executing :

import cv2 as cv
face_recognizer = cv.face.LBPHFaceRecognizer_create()

generating an error : cv2.cv2 has no face attributes.

if i try to install with :

sudo pip install opencv-contrib-python

it will take hours to compile and finally nothing works !

But on the site : https://www.piwheels.org/project/opencv-contrib-python/#install only version 4.4.0.46 has files !

Then I try this :

sudo pip3 install opencv-contrib-python==4.4.0.46

installation is instantaneous !!!

I need to install some other libraries :

sudo apt install libaec0 libaom0 libatk-bridge2.0-0 libatk1.0-0 libatlas3-base libatspi2.0-0 libavcodec58 libavformat58 libavutil56 libbluray2 libcairo-gobject2 libcairo2 libchromaprint1 libcodec2-0.8.1 libcroco3 libdatrie1 libdrm2 libepoxy0 libfontconfig1 libgdk-pixbuf2.0-0 libgfortran5 libgme0 libgraphite2-3 libgsm1 libgtk-3-0 libharfbuzz0b libhdf5-103 libilmbase23 libjbig0 libmp3lame0 libmpg123-0 libogg0 libopenexr23 libopenjp2-7 libopenmpt0 libopus0 libpango-1.0-0 libpangocairo-1.0-0 libpangoft2-1.0-0 libpixman-1-0 librsvg2-2 libshine3 libsnappy1v5 libsoxr0 libspeex1 libssh-gcrypt-4 libswresample3 libswscale5 libsz2 libthai0 libtheora0 libtiff5 libtwolame0 libva-drm2 libva-x11-2 libva2 libvdpau1 libvorbis0a libvorbisenc2 libvorbisfile3 libvpx5 libwavpack1 libwayland-client0 libwayland-cursor0 libwayland-egl1 libwebp6 libwebpmux3 libx264-155 libx265-165 libxcb-render0 libxcb-shm0 libxcomposite1 libxcursor1 libxdamage1 libxfixes3 libxi6 libxinerama1 libxkbcommon0 libxrandr2 libxrender1 libxvidcore4 libzvbi0

It works well since when using import cv2 face !!

I have now in pip3 freeze :

opencv-contrib-python==4.4.0.46
opencv-python==4.5.1.48

Hope this will be usefull !!!!!

Jean David
  • 51
  • 8
3

For me changing createLBPHFaceRecognizer() to

recognizer = cv2.face.LBPHFaceRecognizer_create()

fixed the problem

skyCode
  • 358
  • 1
  • 7
3

I got openCV installed smoothly in my mac by:

$ brew install opencv
$ brew link --overwrite --dry-run opencv // to force linking
$ pip3 install opencv-contrib-python

I got it at windows 10 using:

c:\> pip3 install opencv-python
c:\> pip3 install opencv-contrib-python

Then I got it tested by

$ python3
Python 3.7.3 (default, Mar 27 2019, 09:23:15) 
[Clang 10.0.1 (clang-1001.0.46.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.1.0'
>>> exit()
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
2

I had a similar problem:

module cv2 has no attribute "cv2.TrackerCSRT_create"

My Python version is 3.8.0 under Windows 10. The problem was the opencv version installation.

So I fixed this way (cmd prompt with administrator privileges):

  1. Uninstalled opencv-python: pip uninstall opencv-python
  2. Installed only opencv-contrib-python: pip install opencv-contrib-python

Anyway you can read the following guide:

https://github.com/skvark/opencv-python

2
python -m pip install --user opencv-contrib-python

After doing this just Restart your system and then if you are on Opencv >= 4.* use :
recognizer = cv2.face.LBPHFaceRecognizer_create()

This should solve 90% of the problem.

snehit vaddi
  • 3,854
  • 2
  • 9
  • 13
2

I'm using PyCharm and installing opencv-contrib-python-headless solved it for me. I tried all the other solutions on this thread initially and none of them seemed to work for me.

Dharman
  • 30,962
  • 25
  • 85
  • 135
1

RESTART YOUR IDE

I tried all of the different things but nothing seems to be working then I just restarted my IDE and it worked like charm.

Still, if it does not work then try restarting your system.

FYI, I am working on the following versions

opencv-contrib-python==4.4.0.46
opencv-python==4.1.2.30
Amit Pathak
  • 1,145
  • 11
  • 26
0

For me, I had to have OpenCV (3.4.2), Py-OpenCV (3.4.2), LibOpenCV (3.4.2).

My Python was version 3.5.6 with Anaconda in Windows OS 10.

Cloud Cho
  • 1,594
  • 19
  • 22
0

Check your OpenCV version

import cv2
cv2.__version__

If your are running Python v3.x and OpenCV v4 and above:

pip install opencv-contrib-python

Try running your program again. This should now work.

0

Go to Python Interpreter Settings in Pycharm / your IDE environment and change the package versions or install them in these versions.

opencv-contrib-python == 4.4.0.46 opencv-python == 4.1.2.30

Check Screenshot for clarity.

enter image description here