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 might 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, which were 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 which contains a flag for whether there is a face on it and then you could filter for them. 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.

You might also be interested in the following

24 Comments on „Face detection with PHP”

Joni Lee

and now got this error message

checking for cvLoad in -lcv... no
configure: error: wrong OpenCV version or OpenCV not found

Joni

checking /opt/local/include/opencv/cv.h usability... no
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 ?

Silvio
Silvio

Realy nice

cosmin

I used the extension and it worked great on OpenSUSE with OpenCV 1 but right now I can t get it to work on OSX 10.6 with the MacPorts OpenCV 2. Luckily the Python bindings work just fine.
An update to OpenCV2 would be great but from my browsing of the code files I suspect that it's not a quick task...

cosmin

I used the extension and it worked great on OpenSUSE with OpenCV 1 but right now I can t get it to work on OSX 10.6 with the MacPorts OpenCV 2. Luckily the Python bindings work just fine.
An update to OpenCV2 would be great but from my browsing of the code files I suspect that it's not easily doable...

smallevil

hi:
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 ~~

Mauro Monetiro
Mauro Monetiro

Please I am trying to install php facedetec and is giving the error / facedetect.c: 107: error: too few arguments to function 'cvHaarDetectObjects'
Can you help?

Nhat
Nhat

Hello,I've some problems bellowed ! I want to crop the only face that is not the background and the hair. What would i do ?
And how to convert it to 3D image ? Thank you very much !

You can reply to [email protected] ?

Can Ince

Hello Robert,

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

ed hardy schuhe
ed hardy schuhe

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.

Sezer
Sezer

@stefan, where is power of python? opencv is a C++ code, what does pyhton do here?
Doesn't make sense to compare a primative language like pyhton (with no braces) to mostly C-like language PHP.

Stefan
Stefan

@Tobias - In this case, the power actually comes from Python, but PHP is still quite good.

westworld
westworld

How do I install facedetect on windows?
I can't install through Pecl and get error phpize not found.

Is the code compatible with opencv 2?

best regards,

westworld

Robert

Hi,

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

Gildus
Gildus

Hello, is possible to detect objects like books, or pcs, or vehicles?.
Do you know how do it?
Regards
Gildus

Amar
Amar

Hello Everybody,

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.

Robert

The XML files are in the install dir of OpenCV.

Mark
Mark

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

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
Ben

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

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

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

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

That´s an interesting type of using PHP :)
Here, anyone can see how powerfull PHP is with challenges like this...

 

Sorry, comments are closed for this article. Contact me if you want to leave a note.