Face detection with PHP
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:

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

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

haarcascade_frontalface_alt_tree.xml

haarcascade_frontalface_alt.xml

haarcascade_frontalface_default.xml

haarcascade_lowerbody.xml

haarcascade_profileface.xml

haarcascade_upperbody.xml

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.
24 Responses to "Face detection with PHP"
Sorry, comments are closed for this article. Contact me if you want to post an inventive contribution.
checking for cvLoad in -lcv... no
configure: error: wrong OpenCV version or OpenCV not found
checking /opt/local/include/opencv/cv.h presence... no
checking for /opt/local/include/opencv/cv.h... no
configure: error: 'opencv/cv.h' header not found
any clue ?
An update to OpenCV2 would be great but from my browsing of the code files I suspect that it's not a quick task...
An update to OpenCV2 would be great but from my browsing of the code files I suspect that it's not easily doable...
the opencv have 2.2.0 version,but this php ext don't install,i find a "http://www.santyago.pl/files/facedetect-1.0.1-opencv-2.2.0.patch" ,use "patch -p1 < facedetect-1.0.1-opencv-2.2.0.patch", install done.but call "face_count" at program return false.
when update for this php ext?
thank you ~~
Can you help?
And how to convert it to 3D image ? Thank you very much !
You can reply to vdnhat2009@gmail.com ?
Thanks, before all, for your facedetect php extension.
A quick note with the hope to be helpful:
In facedetect.c, line 107:
faces = cvHaarDetectObjects(img , cascade, storage, 1.1, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(0, 0));
Using the default scale_factor value (1.1) did not work very good for me. Instead,
faces = cvHaarDetectObjects(img , cascade, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(0, 0));
solved all the problems.
Cheers
Can
Doesn't make sense to compare a primative language like pyhton (with no braces) to mostly C-like language PHP.
I can't install through Pecl and get error phpize not found.
Is the code compatible with opencv 2?
best regards,
westworld
detecting the gender and age would be a hard challenge to OpenCV but the quality of the results depend on the used training files.
Many people asked me, if it is possible to detect also general objects with that extension. Yes it is possible, but you have to create your own XML training files for that task. The PHP binding is the same, just the name \"facedetect\" is a bit misleading in that case, but as this is historically originated, I'll retain the name of this extension.
BTW: I published v0.0.2 today.
Robert
Do you know how do it?
Regards
Gildus
I am using OpenCV for the facedetection is there any way through which I can detect the gender as well as approx. age of the detected face using OpenCV.
Thanks.
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)?
keep it up,
cheers // marc
Here, anyone can see how powerfull PHP is with challenges like this...