Ask Your Question
1

OpenCV 3.0.0, FaceRecognizer and Python Bindings

asked 2015-03-05 20:16:30 -0600

al897 gravatar image

Hi there -

I am very new to OpenCV and am using it for a school project on the Raspberry Pi (Mine is the B+ model). I have followed numerous guides to get the FaceRecognizer to work in Python, but to no avail.

My specific problem is -

model = cv2.createEigenFaceRecognizer()
attributeError: 'module' object has no attribute 'createEigenFaceRecognizer'

I also want to note that I can do

import cv2

with no issues, and run functions such as

cv2.imwrite(), cv2.cvtColor()

with no problems, so there isn't an issue with OpenCV not working in general. My problem is with this specific class (faceRecognizer) which is critical for me.

To elaborate, I am running OpenCV 3.0.0 and Python 2.7.3. To install OpenCV, I basically followed the tutorial at http://mitchtech.net/raspberry-pi-ope..., but instead of getting the file from sourceforge, I cloned from the OpenCV repository for the latest version, and in cMake, I added a flag BUILD_PYTHON_NEW_SUPPORT=ON (per a suggestion found online.)

I googled extensively to find a solution, most notably

http://answers.opencv.org/question/25...

http://answers.opencv.org/question/12...

http://www.raspberrypi.org/forums/vie...

and most sources tell me just to "recompile from the latest source". That's what I thought I did. I'm stuck as where to go now - did I compile the library incorrectly? I am aware that OpenCV is written in C++ and there are python bindings to make functions work in python, but I'm not sure where I'm going wrong here. Any input is much appreciated.

edit retag flag offensive close merge delete

Comments

Hmm the interface for algorithms is reshaped in OpenCV3.0. As far as I understand you need to access it through a pointer interface. @berak is more of an expert in this, I will let him do the talking here.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-03-06 02:06:30 -0600 )edit

How can I clone and compile from the opencv_contrib repo ?

Boaz gravatar imageBoaz ( 2016-12-28 04:53:12 -0600 )edit

3 answers

Sort by ยป oldest newest most voted
3

answered 2015-03-06 02:13:01 -0600

berak gravatar image

updated 2017-10-23 12:26:04 -0600

[edit] : this is the opencv3.0 answer, interfaces changed in later versions, please look at @supra56 ' answer below

opencv 3.0 is slightly different. you will have to get and build the opencv_contrib repo, and then there is a new 'face' submodule (namespace):

>>> help(cv2.face)
Help on module cv2.face in cv2:

NAME
    cv2.face

FILE
    (built-in)

FUNCTIONS
    createEigenFaceRecognizer(...)
        createEigenFaceRecognizer([, num_components[, threshold]]) -> retval

    createFisherFaceRecognizer(...)
        createFisherFaceRecognizer([, num_components[, threshold]]) -> retval

    createLBPHFaceRecognizer(...)
        createLBPHFaceRecognizer([, radius[, neighbors[, grid_x[, grid_y[, thres
hold]]]]]) -> retval

so, you will have to use : cv2.face.createEigenFaceRecognizer(),

edit flag offensive delete link more

Comments

Awesome!! I don't know if I just plain missed it or if it's not immediately clear that the face module is not within the standard release, but that clears a lot of things up. I will clone and compile from the opencv_contrib repo and see if it works.

al897 gravatar imageal897 ( 2015-03-06 11:44:55 -0600 )edit

you're right, it's not immediately clear, what was moved where (or why).

berak gravatar imageberak ( 2015-03-06 11:52:19 -0600 )edit
1

Thank you so much for your help! Everything is set up now. One last question - how can I access the current API available? I am trying to use the train(), save() and predict() functions but can't seem to find a way to use it properly.

model.train(np.asarray(X), np.asarray(y))
AttributeError: 'cv2.face_BasicFaceRecognizer' object has no attribute 'train'

Typing in

$ python
>>> import cv2
>>> help(cv2.face_BasicFaceRecognizer)

doesn't help either.

al897 gravatar imageal897 ( 2015-03-06 21:50:31 -0600 )edit

apologies, i did not test it.

you're right, the created model is lacking all functions. something broken with the wrappers, i guess.

berak gravatar imageberak ( 2015-03-07 01:05:10 -0600 )edit

sorry for the bad news, but this is a plain bug, not an error on your side (or your fault even)

berak gravatar imageberak ( 2015-03-07 05:31:37 -0600 )edit
1

if you're bold, try to fix it manually: build/modules/python2/pyopencv_generated_types.h ~line 9043

static void pyopencv_face_BasicFaceRecognizer_specials(void)
{
    pyopencv_face_BasicFaceRecognizer_Type.tp_base = NULL;

change to:

static void pyopencv_face_BasicFaceRecognizer_specials(void)
{
    pyopencv_face_BasicFaceRecognizer_Type.tp_base = &pyopencv_face_FaceRecognizer_Type;

same for :

static void pyopencv_face_LBPHFaceRecognizer_specials(void)
{
    pyopencv_face_LBPHFaceRecognizer_Type.tp_base = &pyopencv_face_FaceRecognizer_Type;

rerun make install, but take care not to invoke cmake again (else it will overwrite your changes)

berak gravatar imageberak ( 2015-03-07 05:48:09 -0600 )edit
1

I think that seemed to fix it! I can successfully call

model.train(np.asarray(X), np.asarray(y))

I haven't tried save() or predict() since I have a couple of errors in my program (passing in an empty training set when calling train(), might be an error on my part), but thank you so much for your help! Hopefully anybody else who has the same problems as me sees this post and can follow your instructions.

One last note - is there a person or a page I can report this bug to so it can get fixed in the future?

al897 gravatar imageal897 ( 2015-03-07 13:15:48 -0600 )edit

try a help(model) to see, if the set of function is complete ( worked on my side)

i already tried to make an issue here , feel free to append, if you find anything missing there.

berak gravatar imageberak ( 2015-03-07 13:32:27 -0600 )edit
1

Yes, doing

>>> test = cv2.face.createEigenFaceRecognizer()
>>> help(test)

listed the face_BasicFaceRecognizer class with all the appropriate methods (save, predict, train, update, etc..) inherited from face_FaceRecognizer. Issue solved!

al897 gravatar imageal897 ( 2015-03-07 13:52:47 -0600 )edit

@berak@al897

I am new to OpenCV and I tried all the previous steps and still stuck at the same point. AttributeError: 'module' object has no attribute 'face'

When I tried to explore this file: build/modules/python2/pyopencv_generated_types.h I found that pyopencv_face_BasicFaceRecognizer_Type.tp_base = &pyopencv_face_FaceRecognizer_Type; pyopencv_face_LBPHFaceRecognizer_Type.tp_base = &pyopencv_face_FaceRecognizer_Type;

What should I do ?

M.Tolba gravatar imageM.Tolba ( 2015-12-13 11:34:09 -0600 )edit
1

answered 2017-10-23 11:48:09 -0600

supra56 gravatar image

updated 2017-10-23 12:16:43 -0600

@al897: For RPI2/3, python 3.5, Opencv 3.3.0

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

Comments

Here is link: face

supra56 gravatar imagesupra56 ( 2017-10-23 11:52:10 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-03-05 20:16:30 -0600

Seen: 19,514 times

Last updated: Oct 23 '17