Perform face detection in PHP

Posted by | June 14, 2011 | PHP | No Comments

This short article will learn you how to perform face detection in PHP. So basically we are going to make use of OpenCV ( you know it you are using it everyday …aren’t you ?? 😀 ) we actually will abuse only on face detection algorithm that comes with it. We are going to compile the face detection as an .so module for php and hook this module in PHP.



  • LAMP stack installed, you know… Apache 2, PHP
  • Build essentials (gcc compilers and all the hoard )
  • A Linux distribution (mine is Ubuntu  11.something)
  • 1 beer & nothing beater to do  😀

OK so the fist thing that you have to do is to install the build essentials by running the command :

sudo apt-get install build-essential

Be proud ! and tell to all you’re friends ! you are a linux developer ’cause you have a compiler running on you PC. Now that you can compile c/c++ files you will need the OpenCV library. There are two ways to install it: that first is to download the latest version from and run make and make-install commands (not recommended, there is always a missing library or a stupid error ) or you just can open System->Administration->Synaptic package manager search opencv and check all the libraries as in the following image:

OpenCv library is installed but… hold on, do not touch you’re beer yet ! you still have to download and compile facedetection library, do that by using the following magic incantation :

tar -xvf facedetect-1.0.1.tar.gz
cd facedetect

Congratulation !! you parents should be so proud of  you, now is the first time when you have compiled a module for PHP. The was created and copied to the corresponding PHP lib directory (/usr/lib/PPH5), add to the php.ini file ( in  /etc/php5/apache2/php.ini or a similar location) restart the apache, now if you run phpinfo you should see the facedetect module up and running:

Now let’s create a PHP file ( for e.g. face.php)  with the following content:


function LoadJpeg($img) {
$im = @imagecreatefromjpeg($img);
if (!$im) {

echo ‘Bad image’;

$total = face_count($_GET[‘file’], ‘haarcascade_frontalface_alt.xml’);
$rectangles = face_detect($_GET[‘file’], ‘haarcascade_frontalface_alt.xml’);

$im = LoadJpeg($_GET[‘file’]);
$pink = imagecolorallocate($im, 255, 105, 180);

if (count($rectangles) > 0) {

foreach ($rectangles as $arr) {
imagerectangle($im, $arr[‘x’], $arr[‘y’], $arr[‘x’] + $arr[‘w’],
$arr[‘y’] + $arr[‘h’], $pink);
header(‘Content-Type: image/jpeg’);

now run it in the browser http://localhost/face.php?file=lena.jpg and voala !!! you have detected the face of Lenna (still have to test it on fill version of Lenna :)) .

While you are enjoying you’re beer just add few words as feed back in section belowww.

About Andrei Tara

Andrei is a developer/geek who loves creating high quality and efficient applications, always following the best practices in software engineering. He loves the challenge of learning new, exciting things and playing with new languages, frameworks, and tools.

Leave a Reply