Ask Your Question

Broken python bindings with createEigenFaceRecognizer

asked 2012-07-11 11:42:56 -0600

btreecat gravatar image

When Trying to use the createEigenFaceRecognizer() and subsequent train(images, lables) method it throws an error:

cv2.error: matrix.cpp:357: error: (-215) r == Range::all() || (0 <= r.start && r.start < r.end && r.end <= m.size[i]) in function Mat

I am using the CPP tutorial as my guide along with the ATT dataset.

Here is a copy of the script I am using to test this.

import cv2,numpy,csv

if __name__ == '__main__':

    inFile = csv.reader(open('att.csv', 'r'), delimiter=';')

    img_dict = {}

    images = []
    lables = []

    for row in inFile:

        image = cv2.imread(row[0])
        lable = int(row[1])

    test_mat = images[-1]
    test_lable = lables[-1]

    images = numpy.array(images[:-1])
    lables = numpy.array(lables[:-1])

    gallery = cv2.createEigenFaceRecognizer()
    gallery.train(images, lables)

I am currently building the opencv package from SVN and this is the build script I am using:

Any help would be greatly appreciated. Thank you.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2012-07-11 12:24:00 -0600

updated 2012-08-11 16:09:36 -0600

Python Wrapper for FaceRecognizer

The Python wrapper for cv::FaceRecognizer works now. First of all thanks to Vadim and Eric for writing the Python wrapper for the contrib module in the first place! Then with some help from Vadim, it was an easy fix (I needed to change 1! parameter). So if you update and compile your git repository it's supposed to work.

The example in this tutorial is going to show how to work with the Eigenfaces model in OpenCV's Python wrapper. You are going to learn how to train a FaceRecognizer on your data, get a prediction (with associated confidence) from the model and see how to access the model internal data. You'll also see how to plot the Eigenfaces, because it's what most of the people want to see.

You'll find the demo in samples/python2 folder:

I'll add this tutorial to the official documentation, as soon as my job allows. Feel free to comment if you have questions or any other requests.

Getting the Data right

We aren't doing a toy example, so you'll need some image data. For sake of simplicity I have assumed, that the images (the faces, persons you want to recognize) are given in folders. So imagine I have a folder (a dataset) images, with the subfolders person1, person2 and so on:

philipp@mango:~/facerec/data/images$ tree -L 2 | head -n 20
|-- person1
|   |-- 1.jpg
|   |-- 2.jpg
|   |-- 3.jpg
|   |-- 4.jpg
|-- person2
|   |-- 1.jpg
|   |-- 2.jpg
|   |-- 3.jpg
|   |-- 4.jpg


One of the public available datasets, that is already coming in such a folder structure is the AT&T Facedatabase, available at:

Once unpacked it is going to look like this (on my filesystem it is unpacked to /home/philipp/facerec/data/at/, your path is different!):

philipp@mango:~/facerec/data/at$ tree .
|-- s1
|   |-- 1.pgm
|   |-- 2.pgm
|   `-- 10.pgm
|-- s2
|   |-- 1.pgm
|   |-- 2.pgm
|   `-- 10.pgm
|-- s3
|   |-- 1.pgm
|   |-- 2.pgm
|   `-- 10.pgm


40 directories, 401 files

Example: Eigenfaces

I am going to use some functions from my Guide To Face Recognition with Python. These functions will make it much easier to work with the image data, like reading the folder hierarchie. The script outputs a prediction, the names of available model data, the mean face and the first (at most) 16 eigenfaces:

  • mean.png The mean face of the given data.
  • eigenfaces_i.png The eigenfaces calculated from the given data.

You can either show the results or save them to disk. Run the application with:

python </path/to/your/images/> [</path/to/store/results/at>]

So if I have extracted the AT&T Facedatabase to /home/philipp/facerec/data/at and I want to show the results I call it like this:

python /home/philipp/facerec/data/at

If I have ... (more)

edit flag offensive delete link more


[btreecat] Thanks Phillip! I saw your guild from April but got a little lost with that. Then the release of 2.4.2, I figured we would just go ahead and use the built in functions.

Thank you for the CPP tutorial and we will continue to play with that for the time being until there is a python guide available.

Also, we noticed that the Python bindings seem to be missing from 2.4.2 release which is why we built from SVN.

Kirill Kornyakov gravatar imageKirill Kornyakov ( 2012-07-12 01:31:07 -0600 )edit

[Philipp Wagner] Please use the comments, instead of a reply! There's a small "Post a comment" link below each answer, which enables you to comment posts. If you have problems with understanding the guide, it would be awesome if you could note what's hard to get at: This way I could also make the OpenCV Guide better.

Kirill Kornyakov gravatar imageKirill Kornyakov ( 2012-07-12 01:31:42 -0600 )edit

The post comment link was not available till now (With the exception of the one below my own question) which is why I used the reply. Ill take another shot at the guide and get back to you with any questions.

btreecat gravatar imagebtreecat ( 2012-07-13 11:07:10 -0600 )edit

Hey Philipp, great tutorial, but I'm stuck. I'm running python 2.7 in Ubuntu Precise on a 64 bit HP G62. I opened up a terminal, cd'd to the working directory, entered: python <single space>path to orl_faces<single space>path to empty data directory I created for out_dir and I'm getting the following error:

Unexpected error: <type 'exceptions.AttributeError'> Traceback (most recent call last): File "", line 100, in <module> [X,y] = read_images(sys.argv[1]) File "", line 75, in read_images im = cv2.imread(os.path.join(subject_path, filename), cv2.IMREAD_GRAYSCALE) AttributeError: 'module' object has no attribute 'IMREAD_GRAYSCALE'

Any guidance?

regards, Richard

rkappler gravatar imagerkappler ( 2012-08-07 19:25:51 -0600 )edit

Hey Philipp, great tutorial, but I'm stuck. I'm running python 2.7 on windows. Pulled latest code for Opencv 2.4.2 and tried to execute example. But I receive this error message: Traceback (most recent call last): File "", line 106, in <module> model = cv2.createEigenFaceRecognizer() AttributeError: 'module' object has no attribute 'createEigenFaceRecognizer'. Please let me know how can i resolve this issue. I assume that Fixes are also done for windows build as well?

Anoop gravatar imageAnoop ( 2012-08-09 07:39:10 -0600 )edit

Hi Philipp, thank you for the write-up, it's excellent. Your link to however is broken. Could you please re-link or post the file elsewhere and link to it? I'm very much looking forward to working through it and learning the working details. Thanks in advance!

hondaman900 gravatar imagehondaman900 ( 2016-03-25 10:50:06 -0600 )edit

Actually, I found it here:

Thanks again for the write-up!

hondaman900 gravatar imagehondaman900 ( 2016-03-25 18:13:05 -0600 )edit

answered 2016-04-02 16:19:57 -0600

i got an error when i used ths function model.load(config.TRAINING_FILE) OpenCV Error: Unspecified error (File can't be opened for writing!) in load, file /home/pi/opencv-2.4.9/modules/contrib/src/facerec.cpp, line 325 Traceback (most recent call last): File "", line 19, in <module> model.load(config.TRAINING_FILE) cv2.error: /home/pi/opencv-2.4.9/modules/contrib/src/facerec.cpp:325: error: (-2) File can't be opened for writing! in function load


edit flag offensive delete link more

Question Tools


Asked: 2012-07-11 11:42:56 -0600

Seen: 10,208 times

Last updated: Mar 25 '16