PHP image face recognition technology based on OpenCV

Source: Internet
Author: User
The technology introduced in this article is not original, but learned from a German named Robert Isele. He wrote a PHP extension openCV, which encapsulates only two functions, face_detect and face_count. OpenCV is an open-source computer graphics image library developed with C/C ++. it is very powerful and has a complete set of research materials. This article focuses on how to use php to call local functions. Face detection technology is only an application branch of openCV.
1. install
Compile the source code into a dynamic so file.
1. install OpenCV (OpenCV 1.0.0)
: Http://sourceforge.net/project/showfiles.php? Group_id = 22870 & package_id = 16948
# Tar xvzf OpenCV-1.0.0.tar.gz
# Cd opencv-1.0.0
#./Configure
# Make
# Make install
# Make check (check whether all installation is correct)
Tip: do not specify the installation path. Otherwise, the path of OpenCV cannot be found after compiling facedetect.
1.2 install facedetect
Http://www.xarg.org/download/facedetect-1.0.0.tar.gz
# Tar xzvf facedetect-1.0.0.tar.gz
# Cd facedetect-1.0.0
# Phpize &./configure & make install
After compilation, the location of the facedetect. so file is displayed.
Finally, confirm to add in php. ini
Extension = facedetect. so, restart apache.
2. function usage
Check whether facedetect is available in phpinfo.
Extract all xml files from the openCV source code/data/haarcascades/And put them in the php execution directory.
// Check the number of faces
Var_dump(face_count('party.jpeg ', haarcascade_frontalface_alt.xml '));
// Return the face position parameter in the image. if there are multiple faces, the array is returned.
$ Arr = face_detect('party.jpeg ', haarcascade_frontalface_alt2.xml ');
Print_r ($ arr );
3. Application
Use imagick to apply images. Because face_detect returns only one rectangle parameter, which contains the x, y coordinates and w, h length and width parameters. The following is a demo of my application.

The code is as follows:


If ($ _ FILES ){
$ Img = $ _ FILES ['Pic '] ['tmp _ name'];
$ Arr = face_detect ($ img, 'haarcascade _ frontalface_alt2.xml ');
// $ Arr1 = face_detect ($ img, 'haarcascade _ frontalface_alt_tree.xml ');
If (is_array ($ arr1) $ all = array_merge ($ arr, $ arr1 );
Else $ all = $ arr;
$ Im = new Imagick ($ img );
// $ Draw = new ImagickDraw ();
// $ BorderColor = new ImagickPixel ('red ');
// $ Draw-> setFillAlpha (0.0 );
// $ Draw-> setStrokeColor ($ borderColor );
// $ Draw-> setStrokeWidth (1 );
If (is_array ($ all )){
Foreach ($ all as $ v ){
$ Im_cl = $ im-> clone ();
$ Im_cl-> cropImage ($ v ['w'], $ v ['H'], $ v ['x'], $ v ['Y']);
$ Im_cl-> swirlImage (60 );
$ Im-> compositeImage ($ im_cl, Imagick: COMPOSITE_OVER, $ v ['x'], $ v ['Y']);
// $ Draw-> rectangle ($ v ['x'], $ v ['Y'], $ v ['x'] + $ v ['w'], $ v ['Y'] + $ v ['H']);
// $ Im-> drawimage ($ draw );
}
}
Header ("Content-Type: image/png ");
Echo $ im;
} Else {
?>


}
?>


References:
Http://www.xarg.org/2008/07/face-detection-with-php/
Http://www.opencv.org.cn/index.php/homepage
Http://www.cs.iit.edu /~ Agam/cs512/lect-notes/opencv-intro/index.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.