Human face recognition in PHP
To auto detect faces in a photo and draw pink box around the faces with a php script running a linux centos server.
Face detection and face recognition are two different things. To recognize a face first detect a face.
Additional requirements apart from php xampp:
- OpenCV
- PHP Facedetect extension
Important thing is to add the following command in php.ini:
extension=facedetect.so
if detection is true the following values will set:
facedetectsupport enabled
facedetectversion 1.0.0
opencvversion 1.0.0
Calling detected face:
ex1.php
<?php
$mycount= face_count('face.jpg','haarcascade_frontalface_alt.xml'); it denotes the output of detected faces
$axis= face_detect('face.jpg','haarcascade_frontalface_alt.xml'); it denotes the face coordinates
print_r($axis);
?>
Face detection script:
<?php
//ex1.php -> detects faces and draws a pink square on faces
function LoadJpeg($imgname)
{
$im = @imagecreatefromjpeg($imgname); /* Loading things */
if (!$im) { /* See if it failed */
$im = imagecreate(150, 30); /* Initiate blank img */
$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);
?>