Face detection with PHP

July 22nd, 2008

The headline does say facedetection - but what does this mean? Easy said, this article focus on how to find faces on images with PHP. Faces have a certain form and so it is possible to search for it. At the end of the search you will say how many human faces are on the image or better: Where are human faces on my image. This article is not intended to be mathematically.

Intel works on the OpenCV library to search for structures on images. The library is shipped with training files, what was trained with hundrets of photos to detect faces from different perspectives on images and so we can focus on the PHP part.

Some may ask, what is this good for? Google has added the opportunity to search only for faces in their Image Search. Lets play with Google. The SERP of the search for "Washington" looks something like this:

Google search for Washington without face attribute

Now I'll add the attribute &imgtype=face to the URI - and what do we see?

Google search for Washington with face attribute

Well, we could create an image-database with also a flag "show faces only". We could also go a step ahead and save how many faces are on a given image to calculate a rank. Another example is image tagging. If you would like to say "this is Sarah on the image", you could add a writeable layer in the region of her face. Sounds nice? Well, read on :-)

For all PHP developers I have a good message: I wrote an OpenCV wrapper to detect faces with PHP. The extension offers the two new functions: face_count() and face_detect(). In princible, they differ only by their return value. The first returns only the number of faces found on the given image and the other an associative array of their coordinates. face_count() serves only to save the loops for counting.

As I've already mentioned, the library is shipped with a few resource files for facedetection. I want to show you an example with different training files. I will show the result by draw a rectangle around the face.

haarcascade_frontalface_alt2.xml

result for haarcascade_frontalface_alt2

haarcascade_frontalface_alt_tree.xml

result for haarcascade_frontalface_alt_tree

haarcascade_frontalface_alt.xml

result for haarcascade_frontalface_alt

haarcascade_frontalface_default.xml

result for haarcascade_frontalface_default

haarcascade_lowerbody.xml

result for haarcascade_lowerbody

haarcascade_profileface.xml

result for haarcascade_profileface

haarcascade_upperbody.xml

result for haarcascade_upperbody

The result in this case is not really strong but you should test PHP-Facedetect for yourself. I had the best results with haarcascade_frontalface_alt. For real world environments it would be good to combine the best resource files.

Go to the Facedetect Project Page

To show you a more dynamic example I searched for the possibility to get a random photo of a flickr stream. Because I don't use the Pear Environment, I wrote my own little Script to generate the Images. Here is the result:

To use another "random" image click here.

If you want to write a wrapper for another language or work with OpenCV in general you should take a look at the OpenCV Intro.

 

8 Responses to "Face detection with PHP"

Robert
August 3rd, 2008
The XML files are in the install dir of OpenCV.
Mark
August 3rd, 2008
I'm a little lost this doesn't seem to only be php, I used the script you provided but it's not able to do it - where do you get the facel xml files from?
Robert
July 25th, 2008
Ben, I've only wrote the wrapper for OpenCV. The used XML files were trained by Intel and lie in the install dir of OpenCV. If you want to create your own XML files you should search for "opencv haartraining". I think it is possible to train OpenCV for full body recognition such as naked body areas. I don't understand your first question. Do you want to know how you use the XML files with the PHP functions? If so - each function needs two parameters: path to the image and path to the xml cascade file. I hope I could help.
Ben
July 25th, 2008
I've been looking for something like this in PHP... Thank you. I have a few questions though: How are the xml files used? How do I train it for the best results for my application? Is it possible to use this for full body recognition (skin detection for naked image recognition, for example)?
Blake
July 24th, 2008
Great article Robert! This is definitely a unique kind of use for PHP, one that you don't see in the mainstream very often. Very interesting.
Robert
July 22nd, 2008
marc, thats right. The quality of the results is not in every case the best. But you should hold in mind, that this only depends on the quality of the training files. You can create new trainingfiles for yourself or wait for the next OpenCV release.
marc
July 22nd, 2008
first of all i have to say, that your blog has some very interesting articles. i've tried using face detection with OpenCV for python, but there were too many false positives. keep it up, cheers // marc
Tobias
July 22nd, 2008
That´s an interesting type of using PHP :) Here, anyone can see how powerfull PHP is with challenges like this...

Leave a Reply