📜 ⬆️ ⬇️

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 think of many more applications of this function.
image

Option number 1


A nice solution for face detection - FaceDetector in PHP, uses OpenCV. FaceDetector works stably with different skin tones, low-quality photos, lots of faces, and people with glasses.

Work algorithm

Face recognition 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 image is read. Read in detail about using the 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 

Make sure that php.ini has:

 extension=facedetect.so 

Application

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

Code example - faces a pink square around the face:

 <?php function LoadJpeg($imgname) { $im = @imagecreatefromjpeg($imgname); if (!$im) { $im = imagecreate(150, 30); $bgc = imagecolorallocate($im, 255, 255, 255); $tc = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, 150, 30, $bgc); 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); ?> 


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.

An example of using the code - one circle around 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(); ?> 

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

 <?php include "FaceDetector.php"; $face_detect = new Face_Detector('detection.dat'); $face_detect->face_detect('sample.jpg'); $face_detect->cropFace(); ?> 


Option number 3


Another library based on opencv. Python script to identify individuals.
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 on the photo. Returns 0 if there is a face, 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 is a picture image.jpg, and at the exit a picture test.jpg with a face selected in a square.
image

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

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

Abstract


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

Source: https://habr.com/ru/post/260579/


All Articles