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:
Now I'll add the attribute &imgtype=face to the URI - and what do we see?
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.
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
- Simple System monitoring with PHP
- Create a simple and small GIF with PHP
- Punycode with PHP
- Dynamic thumbnail generation on the static server
Sorry, comments are closed for this article. Contact me if you want to leave a note.