How to identify a person in a photo using PHP

Face detection is used in social networks, photo editors, video chats, smart captcha, time tracking programs - you can come up with many more applications of this function.
image

Option number 1


A good solution for determining faces - FaceDetector in PHP, uses OpenCV. FaceDetector works stably with different skin tones, low-quality photographs, a large number of faces and people with glasses.

Work algorithm

Face recognition here is based on the Viola-Jones method, Haar cascades (rectangular primitives) and the AdaBoost learning algorithm. Primitives - white and black rectangles of different sizes - are superimposed on the image, after which their convolution with the picture is read. You can read more about the use of Haar cascades here and here .

Install FaceDetector

First you need to install the packages:

sudo apt-get install pkg-config python libjpeg62-dev libpng12-dev libtiff4-dev php-pear

Install OpenCV :

sudo apt-get install libopencv-dev

Install library:

pecl install facedetect

Be sure to make sure that in php.ini there is:

extension=facedetect.so

Application

FaceDetector has two main functions: face_count and face_detect for counting and determining faces, respectively. The haarcascade_frontalface_alt.xml file must be moved from /usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml to the project folder.

Code example - circle the faces with a pink square:

 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);
?>


image

Option number 2


Option without using OpenCV. The PHP FaceDetection library finds only one face in a photo. To use, you need to download a PHP script and paste it into your code.

Code usage example - circle one face with a green square:

face_detect('sample.jpg');
$face_detect->toJpeg(); 
$json = $face_detect->toJson(); 
$array = $face_detect->getFace();
?>

The face can be found and immediately cut using the cropFace () function :

face_detect('sample.jpg');
$face_detect->cropFace();
?>


Option number 3


Another library based on OpenCV. Python script for detecting faces.
First you need to download and install all the necessary packages (Python, Python OpenCV, OpenCV data files):

sudo apt-get install python python-opencv libopencv-dev

And install the FaceDetect library:

sudo cp facedetect /usr/local/bin

Check for the presence of a face in the photo. Returns 0 if there is a person, and 2 if not:

exec('./facedetect -q path/to/image.jpg');
echo exec('echo $?');

An example of using the FaceDetect library in PHP:

// получаем координаты лиц
ob_start();
passthru('/usr/local/bin/facedetect path/to/image.jpg');
$data = ob_get_clean();
echo $data;
// обводим координаты лиц, и сохраняем в файл test.jpg
exec('/usr/local/bin/facedetect -o  test.jpg path/to/image.jpg');

At the entrance there is a picture image.jpg, and at the output there is a picture test.jpg with a face highlighted in a square.
image

You can try this method of determining faces here . The algorithm shows good results even in photographs with many faces. Although sometimes it gives out amazing things. For example, here he identified two faces in Samuel L. Jackson. And here are two faces of Pamela Anderson.

PS: and yes - the function does not recognize cats .

Abstract


  1. Three libraries for detecting faces in PHP: PHP script on OpenCV, PHP FaceDetection and Python script FaceDetect.
  2. The OpenDV FaceDetector library uses Haar cascades.
  3. PHP FaceDetection can only find one face in a photo.
  4. You can try how python-script FaceDetect works here .

Also popular now: