Ask Your Question

Need to .pgm image of specific size for FaceRecognizer in python

asked 2012-08-11 13:09:27 -0600

rkappler gravatar image

I'm working with Philipp Wagner's FaceRecognizer an excellent bit of code, and have gotten to the point where I want to train it on my own database. I have trained it on the att faces database iaw the tutorials successfully. Now I'd like to either add new faces to that database on the theory that the more faces it's trained on the better, or simply train it on a new set of faces. I tried adding faces to the att database, didn't work. This is where I started to learn about .pgm type files, which is what the att database contains. As I understand it, the pgm type file is actually an array that fools the machine into thinking its an image. Also, the arrays must all be of the same size for FaceRecognizer to work on them. 1) is there a handy way for OpenCV to convert jpg's to pgm? (from my research I have learned it's not just a change in extension) 2) can opencv capture images in .pgm format so as not to require conversion? 3) can anyone point me to a tutorial that allows me to save the image as a specific number of pixels? 4) still learning FaceRecognizer, so this may be a dumb question: can image types other than .pgm be converted to arrays so faceRecognizer can work on them? regards, Richard

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-08-11 13:44:56 -0600

updated 2012-08-11 16:01:54 -0600

PGM is an image format, just like JPEG, PNG, BMP or many others you probably know. OpenCV can read almost all of them (probably not GIF), so there's no limit in what files you can read.

Regarding your other problems: reading the documentation for any kind of software always helps a lot. I've tried to write as much documentation as possible, in order to answer exactely those kind of questions. So you have a good chance finding an answer in there. Some face recognition algorithms like the Eigenfaces and Fisherfaces require your images to be grayscale and of equal size, see the notes in the documentation:

This has algorithmic reasons and sorry, but there's simply no way to hide this kind of complexity from the user (or resizing the images to a default size).

So how do you preprocess the images to grayscale and equal size? It's already done within the script I have provided. First of all I read all images as grayscale, so no need to think about this one. And second, see the definition of read_images:

Which is:

def read_images(path, sz=None):
    """Reads the images in a given folder, resizes images on the fly if size is given.

        path: Path to a folder with subfolders representing the subjects (persons).
        sz: A tuple with the size Resizes 

        A list [X,y]

            X: The images, which is a Python list of numpy arrays.
            y: The corresponding labels (the unique number of the subject, person) in a Python list.

That means if you pass the sz parameter, your images get resized to a given size while loading. So if you want to resize all of your images to 70 x 70 pixels, you would call the function with:

[X,y] = read_images(path=sys.argv[1],sz=(70,70))

And that's it.

Please note, I fixed a Python bug on 64bit machines with the latest commit. Thanks to Leo Dirac for reporting:

edit flag offensive delete link more


As always Philipp, thank you. I think the size bit will solve all.

rkappler gravatar imagerkappler ( 2012-08-11 14:16:46 -0600 )edit

Then please accept answers, if they solve a problem.

Philipp Wagner gravatar imagePhilipp Wagner ( 2012-08-11 15:52:38 -0600 )edit

Right. You've made your position very clear. Speaking as a teacher, I can tell you that very often when things are obvious to us, they are not to our students. Because you are quite certain you have explained something adequately, does not mean it is understandable to the people you are trying to teach. I'm a 52 year old veteran with several degrees, I'm not too bad a programmer, and I'm a damned good teacher. Hopefully this establishes that I'm not an idiot or lazy. Had your tutorials explained everything well, I (and many others) wouldn't have to keep asking questions. If I read your response the wrong way, I apologize. Whether I did or did not, I'll not bother you again.

rkappler gravatar imagerkappler ( 2012-08-11 17:02:33 -0600 )edit

No no, you got me totally wrong. This is a QA page, so you can accept questions. You see this little check sign next to my answer? This way questions are marked as solved, so others know the answer solved the question.

Philipp Wagner gravatar imagePhilipp Wagner ( 2012-08-11 22:53:17 -0600 )edit

Then you have, again, my apology. Unfortunately I can't "accept"/ check the answer as solving the problem, because it does not. If I put [X,y] = read_images(sys.argv[1], sz=(92,112)) and add several cropped webcam shots to the att db, the program errors out. It won't resize the new images and the size argument throws a 215 error within opencv that I'm trying to chase down. I'm wondering if this is all due to the bug listed above, as I'm working on a 64 bit machine, but I rather doubt it. They seem to be unrelated to the y labels, but then again I haven't had my coffee yet either. Again, sorry for the misunderstanding, I was getting frustrated at being told to read the docs when I've read them (and almost everything else you've written in the last ...(more)

rkappler gravatar imagerkappler ( 2012-08-12 08:01:30 -0600 )edit

Oh, I almost forgot, in your comment over on the bug report you say to add y = np.asarray(y, dtype=np.int32) but don't say where. I've tried it in several different spots in and haven't found the right one, as I either get a y is undefined error or nothing changes. Any hint would be greatly appreciated.

rkappler gravatar imagerkappler ( 2012-08-12 08:04:09 -0600 )edit

Simply send me a mail. This turns into a chat, you can find my mail adress on my website.

Philipp Wagner gravatar imagePhilipp Wagner ( 2012-08-12 08:23:30 -0600 )edit

I see that you've uploaded a new version with the change, I'll try that before I take anymore of your time.

rkappler gravatar imagerkappler ( 2012-08-12 08:29:30 -0600 )edit

Question Tools

1 follower


Asked: 2012-08-11 13:09:27 -0600

Seen: 5,942 times

Last updated: Aug 11 '12