Build 10 classic Flash games and learn game development along the way with this ultra-fast paced game development course.

If you love this blog, this is the book for you.

Buy the book

Get the source code of 12 commercial Flash games, which have been loaded more than 50 million times!

Learn from real world successful examples.

Get it now

Box2D for Flash Games teaches you how to make Flash physics games from scratch with the most advanced features.

Create the new Flash game smashing hit.

Buy the book

PHP face detection class

Face detection is a computer technology that determines the locations and sizes of human faces in arbitrary (digital) images.

It detects facial features and ignores anything else, such as buildings, trees and bodies.

(source: Wikipedia)

There is so much to say about face detection and all its algorithms… I am planning a step by step tutorial about every branch of object detection in digital images, but first I want to publish Maurice Svay‘s PHP class.

Maurice, in his blog svay.com, explains he was looking for a face detection script for PHP, but wasn’t able to find one working without OpenCV (Open Source Computer Vision), 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.

So he coded his own pure PHP solution, that does not require any library to be installed on the server.

And, obviously, that can be easily ported into any language. His class itself has been translated from a javascript code that actually is no longer available online

So this is the class:

Using it is very easy, just write

once you unpacked and included detection.dat and the image in the same path as your class.

Do you want to see a result?

This was generated by the script, just changing the frame color from red to green, to make it more visible.

The class does not work that well on every photo, but it’s a good start for a journey into face detection

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (36 votes, average: 4.75 out of 5)
Loading ... Loading ...
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 32 comments

  1. Martti Laine

    on July 6, 2010 at 6:47 pm

    Holy sh*t. Never even thought about detecting faces in php. Thumbs up!

  2. Martti Laine

    on July 6, 2010 at 6:48 pm

    Sorry for my bad language.

    This article proves it, we’re on 21st century.

  3. Thomas

    on July 7, 2010 at 1:31 am

    So, what are the logics that make it work?

  4. mario

    on July 7, 2010 at 9:33 am

    face.com had release a API for face detection, you can even use the code to detect the location of eye, nose and mouth

  5. Mal kurz rundgeschaut… #24 | Braekling.de

    on July 11, 2010 at 9:41 am

    [...] PHP face detection class – gut erkannt mit Emanuele Feronato. [...]

  6. bossoi

    on July 13, 2010 at 7:56 pm

    I have a question… why are .dat file for?

  7. ammii*

    on August 2, 2010 at 4:44 pm

    Absolutely yes!! I’ve never ever thought about PHP for detecting faces!!!
    Thank you very much :))

  8. Marcius

    on August 9, 2010 at 12:23 pm

    Very good, congratuloations and tks for your contribe

  9. prince

    on August 16, 2010 at 9:39 am

    Amazing might use it in my next project :)

  10. Zeeshan Lalani

    on August 16, 2010 at 10:41 am

    very nice.. thanks..

  11. Face Detection Using PHP | blogfreakz.com

    on August 16, 2010 at 12:18 pm

    [...] Source: http://www.emanueleferonato.com/2010/07/06/php-face-detection-class/ [...]

  12. Shashi

    on August 16, 2010 at 1:22 pm

    Thanks a lot for this great post… i have been wondering whether it would be possible to do these kind of things with php… hats off php !

  13. datshay

    on August 16, 2010 at 2:24 pm

    Amazing….. No words to say ….
    Thumbs up !

  14. Karl Roos

    on August 16, 2010 at 10:10 pm

    This is amazing! Never thought you’d be able to do this with PHP!

  15. vcrack

    on August 18, 2010 at 2:45 pm

    how long your execution time to do this?

  16. Vincent

    on August 19, 2010 at 1:58 pm

    I’ve never tought that PHP could do this thing too. It’s awesome

  17. harika

    on August 22, 2010 at 4:32 pm

    thank you:D

  18. Proger

    on August 29, 2010 at 9:02 am

    Thanks, cool script.
    However, it is not always working correctly, here’s an example:
    http://programming.su/sites/programming.su/files/demo/face-detector/putin.jpg

  19. Detectar una cara en una foto usando PHP | Blog personal de Brian Urban

    on September 8, 2010 at 2:28 am

    [...] Via Emanuele Feronato [...]

  20. Face detection using PHP

    on November 20, 2010 at 12:00 pm

    [...] Without Open CV you can apply face detection using the PHP class mentioned at this blog [...]

  21. long.vu

    on January 13, 2011 at 6:23 pm

    I saw that happend error in function imagecolorat and can not work

  22. PHP and Jquery face detection scripts « Webania.net

    on January 21, 2011 at 11:52 pm

    [...] Download page [...]

  23. Dmitry

    on January 25, 2011 at 5:39 am

    Doesn`t detect multiple and small faces. How’d you tweak the code to do that?

  24. tumaji

    on May 6, 2011 at 2:09 pm

    Thank you, I looking for this script.

  25. tejas tank

    on December 2, 2011 at 7:44 am

    Here detection.dat is which kind of file. is a jpg or movie or other things…

  26. Mohd

    on December 28, 2011 at 3:13 pm

    Hay @Proger..

    At least it detected the face of Putin’s care :) I think PHP can’t recognize Putin.. same of the rest of us, this is logic.

  27. mrtaza

    on January 19, 2012 at 7:10 pm

    http://bit.ly/yZ7UxW

  28. mrtaza

    on January 19, 2012 at 7:12 pm

    i have used the above class in this sample
    http://bit.ly/yZ7UxW
    works in most cases.

  29. Dan

    on February 17, 2012 at 10:40 am

    I have two other Haar Xml files, is there any way to convert these to .dat files for use in this PHP implementation?

    Kind regards,

    Dan

  30. ??????????? ?? ???? – PHP ????? | Siljanoski.com

    on February 21, 2012 at 4:50 pm

    [...] ?????????? ?????: http://www.emanueleferonato.com/2010/07/06/php-face-detection-class/ [...]

  31. Muthukumar

    on September 13, 2012 at 6:57 am

    This is not working to me

  32. absfrm

    on October 30, 2012 at 10:44 pm

    hello
    this is powerful.thanks to share
    but can you answer my question?
    i want to detect a face(save some detail for it as name or family).
    and when detect that face again , system show me his/her detail.
    can i do this action?
    thanks a lot