Part of my job at Dinkum is to make sophisticated things happen for our website and marketing clients on a daily basis. One such request came a few months ago for a project we’re working on for the Marlboro Music Festival.
The request? Help the Festival (which was founded in 1951 by the late, great pianist Rudolf Serkin and his colleagues) to add “facebook-style” photo tagging to their new database that we’re building, which includes thousands of historic photographs featuring many of the titans of 20th century classical music.
What about if I tell you that you can have an amazing facial recognition infrastructure totally free and “open”!? And that you can use it with your PHP projects? Once again, welcome to the beauty of the open source world, which we embrace by using tools such as WordPress on a daily basis.
It is not my intention to dive too deep into the technical details of this project, but be warned – this is a technical post, so if you’re not into that kind of thing, just skip to the bottom for an example photograph showing the founders of Marlboro Music with the result of our little tool.
In case you are of a technical mind, I’d like to share with you first what libraries you need and how to make it all work with PHP.
So, here’s the list:
- Linux, of course! (I tested it using Ubuntu)
- PHP/Apache
- GD Library
- OpenCV
- PHP Facedetect extension
The “most” important library here is OpenCV. Open Source Computer Vision is a library of programming functions for real time computer vision. Here are some of the uses for this fantastic resource:
- Human-Computer Interaction (HCI)
- Object Identification
- Face Recognition
- Gesture Recognition
- Motion Tracking
- Mobile Robotics
And much, much more! A pretty amazing testament to the open source community, right? So, let’s do some installations – beware, we’re getting VERY technical for a minute:
Installing the libraries (at least the main ones)
sudo aptitude install php5-dev re2c pkg-config python libjpeg62-dev libpng12-dev libtiff4-dev
Download and unzip the Face detect extension from here. Thanks to Robert Eisele, who wrote a wrapper to use OpenCV with PHP.
To install the extension type as root:
phpize && ./configure && make && make install
Check if the extension was succesfully installed, opening your php.ini file
extension=facedetect.so, and check your phpinfo()
Now it’s time to play!
Create a php file into your /var/www named face.php and paste the code below:
function LoadJpeg($imgname)
{
$im = @imagecreatefromjpeg($imgname); /* Attempt to open */
if (!$im) { /* See if it failed */
$im = imagecreate(150, 30); /* Create a blank image */
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
/* Output an errmsg */
imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);
}
return $im;
}
$total= face_count($_GET['file'],'haarcascade_frontalface_alt.xml');
$ord= face_detect($_GET['file'],'haarcascade_frontalface_alt.xml');
$im = LoadJpeg($_GET['file']);
$pink = imagecolorallocate($im, 255, 105, 180);
if(count($ord) > 0) {
foreach ($ord as $arr) {
imagerectangle($im,$arr['x'] ,$arr['y'] , $arr['x']+$arr['w'],
$arr['y']+$arr['h'], $pink);
}
}
header('Content-Type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
And put some images with faces there too to play a bit.
To try the code, you need to just use the code below
localhost/face.php?file=myimage.jpg
And if everything is ok, you will have a great image like this one:
with Rudolf Serkin in the center, along with the famous flutist Marcel Moyse (on the right – he premiered Stravinsky’s Rite of Spring in 1913 in Paris), his son Louis Moyse, daughter-in-law Blanche Moyse (who recently passed away at age 101) and famous brothers Hermann and Adolf Busch.
9 Replies to “Face recognition in an Open Source environment”
When i type this: phpize && ./configure && make && make install
i get this error: phpize && ./configure && make && make install
Any idea why this happens?
Do you think you can give me the error description?
This is cool :D… is it possible to compare / recognize face from other source image using this extension?
Great article!
Hey Ronggur!
What do you mean with “other source image”? Can you give me an example?
Oh sorry :D.. I mean when we add another image then we can compare faces in those two images..
Sorry my english is bad π
Oh that’s a good question! But I think you can get some information about the face, and then compare with the info of the other image. Not comparing directly face to face. But I’ll check it, it would be a nice feature π
Can anyone can help me to solve the issues listed in following.
http://stackoverflow.com/questions/11822759/issues-while-installing-opencv-library-for-php
just came across this! is it possible that a user on my site can upload a picture and its been macthed with another image in a database?
i want to compare two images one is already in database and other will be uplaod by user ….
is that possible in PHP??????