Face detection in PHP

Face Detection in PHP (without OpenCV).

We have been looking for ways to detect faces in photos with PHP. Nowadays, face detection is built in many consumer products (camera obviously, but also Google and iPhoto), and seems to be a pretty common job. So we expected to find many solutions for doing it with PHP. Surprisingly, the only one we could find is OpenCV, an opensource lib that was originally developed by Intel. OpenCV seems to perform well but you need to be able to install it on your server. In our case, we wanted to have a pure PHP solution, so it can work with most hosts.

Learning about face detection

So, we started to think about implementing it our self. We read a articles, scientific papers, etc. The website http://www.facedetection.com/ is a great resource by the way. From this short research, we learnt that one of the most popular solutions is to use Viola Jones training with a Haar classifier. Very informative, but tedious to implement.

Always look at what others are doing

Then we looked for existing implementations in other languages. Let’s say Flash and Javascript. With Canvas, Javascript developers will certainly look at what flash developers do. There seem to be a few libs for face detection in AS3. They even work in real time. Pretty cool. We kept searching and finally found a canvas+javascript implementation of face detection at http://blog.kpicturebooth.com/?p=8. The code looked fairly compact and simple. Shouldn’t be hard to port to PHP.

Once the code converted to PHP, here’s the result:

And you simply use the class this way:

Which gives the following result:

The code requires GD and is a bit slow but should work on most PHP servers. You’ll also need the data file: http://svay.com/experiences/face-detection/detection.dat. Let me know if you have ideas for improving the code 🙂