How to detect face in image using PHP

In brief:

  1. Three libraries for face detection with PHP: PHP-script with OpenCV, PHP FaceDetection, and Python FaceDetect script.
  2. FaceDetector library with OpenCV uses Haar Cascades.
  3. PHP FaceDetection can find only one face on a photo.
Smart face detection is used in social networks, image editors, video chats, smart captcha, time tracking programs, etc.


Option №1

FaceDetector on PHP is quite a successfull solution which uses OpenCV. FaceDetector works stably with various skin tones, low quality pictures, lots of faces and people wearing glasses.

Operation algorithm

Face detection here is based on Viola-Jones method, Haar Cascades (rectangular primitives), and AdaBoost learning algorithm. The primitives are black and white rectangles of different sizes which are placed on the image. After superimposing their correlation with a picture is being read. 

FaceDetector installation

First, packages install is needed:
sudo apt-get install pkg-config python libjpeg62-dev libpng12-dev libtiff4-dev php-pear
 
OpenCV install:
sudo apt-get install libopencv-dev
 
Library install:
pecl install facedetect
 
Make sure php.ini includes:
extension=facedetect.so

Usage

FaceDetector has two main functions: face_count and face_detect for counting and detection faces, respectively. File haarcascade_frontalface_alt.xml should be moved from /usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml to a project folder.
For example, here the face will be surrounded with a pink square:

<?php function LoadJpeg($imgname)
{
    
$im = @imagecreatefromjpeg($imgname);
    if (!
$im
    {
        
$im  imagecreate(15030); 
        
$bgc imagecolorallocate($im255255255);
        
$tc  imagecolorallocate($im000);
        
imagefilledrectangle($im0015030$bgc);
        
imagestring($im155"Error loading $imgname"$tc);
    }
    return 
$im;
}
$totalface_count($_GET['file'],'haarcascade_frontalface_alt.xml'); $ordface_detect($_GET['file'],'haarcascade_frontalface_alt.xml'); $im LoadJpeg($_GET['file']); $pink imagecolorallocate($im255105180);
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); ?> 


Option №2

Option without OpenCV. PHP FaceDetection library finds only one face on a picture. It requires PHP-script to be downloaded and inserted into the code.
For example, here the face will be surrounded with a green square:

<?php include "FaceDetector.php"; $face_detect = new Face_Detector('detection.dat'); $face_detect->face_detect('sample.jpg'); $face_detect->toJpeg();  $json $face_detect->toJson();  $array $face_detect->getFace(); ?>
Face can be found and cropped immediately with a cropFace() function:
<?php include "FaceDetector.php"; $face_detect = new Face_Detector('detection.dat'); $face_detect->face_detect('sample.jpg'); $face_detect->cropFace(); ?>


Option №3

Another library based on OpenCV — Python-script for face detection. To start, all necessary packages should be downloaded and installed (Python, Python OpenCV, OpenCV data files):
sudo apt-get install python python-opencv libopencv-dev
 
Installation of FaceDetect library:
sudo cp facedetect /usr/local/bin
 
Checking face presence. Returns 0 if the face is present, and 2 — if not:
exec('./facedetect -q path/to/image.jpg');
echo exec('echo $?');
 
Code example using FaceDetect in PHP:
// receiving face coordinates
ob_start();
passthru('/usr/local/bin/facedetect path/to/image.jpg');
$data = ob_get_clean();
echo $data;
// leads round coordinates and saving test.jpg file
exec('/usr/local/bin/facedetect -o  test.jpg path/to/image.jpg');
 

Comments

Post a Comment

Popular posts from this blog

How to download a file using command prompt (cmd) Windows?

The future of Artificial Intelligence: 6 ways it will impact everyday life

How to Include ThreeJs in Your Projects