I have always wondered how to detect faces automatically with php script. I have seen in many photo sharing and social network sites automatically detect a face and tag a name after being uploaded.
To auto detect faces in a photo and draw pink box around the faces with a php script running a linux centos server. Please note that face detection and face recognition are two different things. To recognize a face you have to first detect a face and then do the required processing.
PHP facedetect extension is very simple. All you have to do is call a function face_detect(p) and it will give all the x,y coordinates in a photo where we draw a square. face_count() will output how many total faces in the given photo. For documentation see here
We install opencv and then we compile the php facedetect extension with php.
How to Install OpenCV
If you are running centos then one single line will install opencv.
yum install opencv
OpenCV may also need the following depencies to work properly and you will need to install them as well.
yum install ibpng libpng-dev zlib libjpeg libjpeg-dev libtiff libjasper swig python pkgconfig gtk gtk-dev
For more installation instructions on linux see here
The opencv will be installed in/usr/local/share/opencv and all the important facedetection training files known as “haar training” can be found in haarcascades folder in xml format (like haarcascade_frontalface_alt.xml)
How to test run OpenCV
Inorder to test run opencv you have to compile the samples folder. Go to /usr/local/share/opencv/samples/c/
Run this command to compile to binary.
If you find large number of errors and variables not declared like shown below..
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc’
to the PKG_CONFIG_PATH environment variable
No package ‘opencv’ found
and then it will compile.
To test run the opencv run the following command in the c folder for a test.jpg photo.
./facedetect –cascade=”../haarcascades/haarcascade_frontalface_alt.xml” test.jpg
Since your server does not have Xwindow installed, you probably do not see the output just the processing time.
If you see error “error while loading shared libraries: libcxcore.so.2: cannot open shared object file: No such file or directory” then see this solution
Install PHP Facedetect Extension
Installing the PHP facedetect extension is very easy. Just download the package and compile it for PHP.
tar zxf facedetect-1.0.0
phpize && configure && make && make install
If you see phpize command not found error, install php developer libraries
yum install php-devel
yum install php5-devel
open php.ini and make sure you add this line.
If facedetect has been successful, you will see this enabled in test.php.
Thats it! all the installation part is over!
Writing a PHP Script
Before you start writing php script make sure you copy all xml files in /usr/local/share/opencv/haarcascades folder to the web folder.
and this simple two lines to from php to call the function
Make sure the path of xml files are correct else you would see a blank face or bool(false) output. Once you get this co-ordinates all we have to do is use php gd library to draw square around the face with the above coordinates.
This script will do that and you have call the script with photo file like this http://domain/face.php?file=test.jpg and the test.jpg file should be in the same folder
Just remember haarcascade_frontalface_alt.xml are haar training files used for face detection. You can play around with other training files like to detect various body parts, not just faces.
Conclusion and the Results
(standard test image used for image processing)
I have given multiple frontal detection photos to the script and the script performed well, despite few false face detections. One sample output file is shown below with the pink boxes detected by the script as faces. Unfortunately i can only show the blurred faces because of personal reasons.
I should say i am pretty surprised the way the multiple faces are detected and the results are good withhaarcascade_frontalface_alt.xml as training file . To get more accurate results, you need to do haar training to xml files to produce more accurate results.
Other Useful Links