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, 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

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

    • Sorry for my bad language.

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

  • Thomas

    So, what are the logics that make it work?

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

  • Pingback: Mal kurz rundgeschaut… #24 |

  • bossoi

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

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

  • Marcius

    Very good, congratuloations and tks for your contribe

  • Amazing might use it in my next project :)

  • very nice.. thanks..

  • Pingback: Face Detection Using PHP |

  • 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 !

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

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

  • how long your execution time to do this?

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

  • thank you:D

  • Thanks, cool script.
    However, it is not always working correctly, here’s an example:

  • Pingback: Detectar una cara en una foto usando PHP | Blog personal de Brian Urban()

  • Pingback: Face detection using PHP()

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

  • Pingback: PHP and Jquery face detection scripts «

  • Dmitry

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

  • Thank you, I looking for this script.

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

  • Mohd

    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.

  • mrtaza
  • mrtaza

    i have used the above class in this sample
    works in most cases.

  • Dan

    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,


  • Pingback: ??????????? ?? ???? – PHP ????? |

  • Muthukumar

    This is not working to me

  • absfrm

    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

  • Lakshmi Narayanan

    Hi this is great! Working fine with most of the complete and round faces. Thank u for the code! If you tweak the code further please let us know!

  • Pingback: Flash AS3 face detection and recognition library - Emanuele Feronato()

  • Sander

    Face detection with PHP is not necessary, you can better use jQuery client side script.

    Search on Google for jQuery face detection and you will find it.

  • suman

    programm is giving notice at line no 118 imagecolorat() out of bound ,,and also showing fatal error execution time exceed 30 sec in line 119 ,,,what i shoud do

  • Pingback: jQuery face detection plugin - Emanuele Feronato()

  • devpk

    hye i need to compare thumb impression … is that library useful for matching thumb .thumb is store in mysql blob format. kindly suggest me links

  • Y

    where can I run this code, ? tried with my web server, ..
    but I dont know how can I view the output

  • dalibor

    Hi, I was wondering how difficult would it be to tune the logic so that it “tags” objects on some uniform background, something like this
    I do not need it to actually recognize what it is on the picture, just to tag that there is “something” on the photo, and provide coordinates to tag it.
    Thanks in advanced

  • Aishah norzu

    i could not understand. we just run the code only ?

  • krishna Kumar

    hello i am geeting this error

    please help me

    thanks in advance
    Warning: Cannot modify header information – headers already sent by (output started at C:xampphtdocsdetection.php:118) in C:xampphtdocsdetection.php on line 77
    ????JFIF??>CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), default quality ??C $.’ “,#(7),01444’9=82<.342??C 2!!22222222222222222222222222222222222222222222222222??? "?? ???}!1AQa"q2???#B??R??$3br?