Ask Your Question
0

Attribute Error: module 'cv2' has no attribute 'face'

asked 2017-10-08 09:41:39 -0600

Huy Nguyen gravatar image

Hi all,

First of all, I am quite a newbie with the Raspberry Pi, Python and OpenCV so please forgive me for likely asking "stupid" questions at times.

Following is my setup: I have a Kuman pi camera connecting to a Raspberry Pi 3 Model B for some experiments with OpenCV Facial Recognition.

I followed the instructions on Pyimagesearch and was able to get OpenCV to install and running on the pi (or so I thought). As far as I know, the pi cam allowed me to take images and save them (i.e. cv2.imwrite, etc) as positive images for recognition training step.

The training step is where i run into the problem when I tried to execute the following line in the script:

model = cv2.face.createEigenFaceRecognizer()

The error message is: AttributeError: module 'cv2' has no attribute 'face'

Every where that I searched on the internet present the problem as needing "extra modules" in the opencv_contrib which I thought I already have (I have populated folders of ~/opencv-3.3.0/build/modules and ~/opencv_contrib-3.3.0/modules). The problem seems to be that python does not know to look for "face" attribute in the opencv_contrib-3.3.0 folder for some reason.

Any suggestions would be very appreciated! Thanks in advance!

Also note that I created a virtualenv and virtualenvwrapper while installing opencv. Does this have anything to do with it?

edit retag flag offensive close merge delete

4 answers

Sort by ยป oldest newest most voted
1

answered 2017-10-08 19:35:14 -0600

supra56 gravatar image

In OpenCV 3.3.0:

model = cv2.face.FisherFaceRecognizer_create()
edit flag offensive delete link more
0

answered 2018-07-12 17:08:58 -0600

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

edit flag offensive delete link more
0

answered 2019-07-09 06:53:54 -0600

You need to upgrade your OpenCV package using 'python -m pip install opencv-contrib-python --upgrade'. Hope this helps.

edit flag offensive delete link more
0

answered 2017-10-08 09:55:07 -0600

berak gravatar image

updated 2017-10-08 09:57:13 -0600

"Every where that I searched on the internet present the problem as needing "extra modules" -- yea, that's true.

there are 2 options:

  • the "easy" (but somewhat crippled) one: use pip install opencv_contrib_python this will install the required cv2.face module, but, (unless you're on windows) it won't have any gui or video-file capabilities.)

  • the hard, but only true way: build all of it from src. see here for how to

edit flag offensive delete link more

Comments

Berak,

Really appreciate your help on this!

I am still a little confused about the cmake command used in the link above:

$ cd <opencv_build_directory>
$ cmake -DOPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules <opencv_source_directory>

When I downloaded and unzipped opencv-3.3.0 and opencv_contrib-3.3.0, these folders were created automatically.  Based on this, I determined that "opencv_build_directory" is ~/opencv-3.3.0/build, "opencv_contrib/modules" would be ~/opencv_contrib-3.3.0, etc.

However, when I execute the command
$ cmake -DOPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/modules ~/opencv-3.3.0
I get the following message:
CMake Error: the source directory "/home/pi/opencv-3.3.0" does not appear to contain CMakeLists.txt.

Any reason why that is?

Thank you!

Huy

Huy Nguyen gravatar imageHuy Nguyen ( 2017-10-08 11:00:45 -0600 )edit

does/home/pi/opencv-3.3.0 contain a CmakeLists.txt file ?

folder should look exactly like here: https://github.com/opencv/opencv

berak gravatar imageberak ( 2017-10-09 01:14:15 -0600 )edit

Berak,

Thanks again for your reply!

I must have accidentally deleted all of the opencv-3.3.0 contents except for the \modules folder, that was why I could not find the CMakeLists.txt.

Anyway, I got opencv to work with the pip install command but it is opencv-2.4.9.1. I could never get opencv-3.3.0 to work properly. I tried to unzip opencv-3.3.0 and opencv_contrib-3.3.0 again and rebuild opencv with the following command lines:

$ cmake -DOPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/modules ~/opencv-3.3.0
$ make -j5

but it yield "fatal error" in the middle of the compile. Unfortunately, it stopped at about 60% in the progress bar so I never could see if the extra_modules would get installed or not.

Maybe I should reinstall everything from scratch to make it work?

Hu

Huy Nguyen gravatar imageHuy Nguyen ( 2017-10-10 09:57:20 -0600 )edit

wait, if you have 2.4.9 running, you can just use cv2.createEigenFaceRecognizer() and such.

(it's just a pity, because 2.4.9 is as old and ugly as hell, otherwise)

to mend the 3.3 issues, we need to know your errors (we're not psychics !)

berak gravatar imageberak ( 2017-10-10 10:04:39 -0600 )edit
1

Berak, that's exactly where I am at! I am currently able to use cv2.createEigenFaceRecognizer() function. But I'll try to do a fresh install of opencv-3.3.0 and see if I can get through. Will report back here with (or without) errors :)! Thanks.

Huy Nguyen gravatar imageHuy Nguyen ( 2017-10-10 13:26:52 -0600 )edit

well, again if errors happen, you've got to report those, else we cannot help !

berak gravatar imageberak ( 2017-10-10 13:54:06 -0600 )edit

Berak,

I tried to reinstall and fix the path to the extra modules path again but met with the following error message:


/home/pi/opencv-3.3.0/modules/core/src/system.cpp:768:5: error: expected primary-expression before ';' token modules/core/CMakeFiles/opencv_core.dir/build.make:1220: recipe for target 'modules/core/CMakeFiles/opencv_core.dir/src/system.cpp.o' failed make[2]: * [modules/core/CMakeFiles/opencv_core.dir/src/system.cpp.o] Error 1 CMakeFiles/Makefile2:2509: recipe for target 'modules/core/CMakeFiles/opencv_core.dir/all' failed make[1]: [modules/core/CMakeFiles/opencv_core.dir/all] Error 2 Makefile:160: recipe for target 'all' failed make: ** [all] Error 2


Any suggestions? Thanks!

Huy

Huy Nguyen gravatar imageHuy Nguyen ( 2017-10-12 23:39:14 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-10-08 09:41:39 -0600

Seen: 26,232 times

Last updated: Oct 08 '17