Face detection in images using PHP


In a previous post we saw how to detect human faces in images using JavaScript. In the current post we will look into a PHP port of a JavaScript face detection algorithm without using openCV. Since the code is completed written in PHP it cannot attain the raw speed that C,C++ provides, but for many trivial application a PHP version can do.

Installing

Before reading further you can download the code below. The original JavaScript port is located here, the following includes the complete code with a crop method added along with a few example files. As the code relies on the GD library, make sure that it is installed and activated on your system.

Download Source
Downloads : 8703 / File size : 499.3 kB

Running our sample code

The sample code to show the detected face is show below. Note that in images having multiple faces only the first one is detected. Although this might seem like a limitation, this can be useful in web applications that need to create a user profile picture from a raw image. So that if a user uploads a full length picture of him/her, the given code can detect the face and automatically create a profile picture for the user.

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


Flickr :By modelxing

The above code displays a border around the detected face, instead we can directly crop the resulting face area and output the resulting image.

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

Face coordinates

If you need the coordinates of the detected face for further processing, you can use the following.

// Return coordinates in JSON
// {'x':56.375, 'y':45.1, 'w':227.55}
$face_detect->toJson();
 
// OR
 
// Return coordinates as an Array
$face_detect->getFace();

Extending the class

You can extend the Face_Detector class using your own methods. For example I’ve added three methods – Rotate(), resizeFace() and toGrayScale() to the class, which lets you modify the final face area further.

<?php
 
include "FaceDetector.php";
 
/* We now extend the above class so we can add our own methods */
class FaceModify extends Face_Detector {
 
  public function Rotate() {
    $canvas = imagecreatetruecolor($this->face['w'], $this->face['w']);
    imagecopy($canvas, $this->canvas, 0, 0, $this->face['x'], 
              $this->face['x'], $this->face['w'], $this->face['w']);
    $canvas = imagerotate($canvas, 180, 0);
    $this->_outImage($canvas);
  }
 
  public function toGrayScale() {
    $canvas = imagecreatetruecolor($this->face['w'], $this->face['w']);
    imagecopy($canvas, $this->canvas, 0, 0, $this->face['x'], 
              $this->face['x'], $this->face['w'], $this->face['w']);
    imagefilter ($canvas, IMG_FILTER_GRAYSCALE);
    $this->_outImage($canvas);
  }
 
  public function resizeFace($width, $height) {
    $canvas = imagecreatetruecolor($width, $width);
    imagecopyresized($canvas, $this->canvas, 0, 0, $this->face['x'], 
                     $this->face['y'], $width, $height, 
                     $this->face['w'], $this->face['w']);
    $this->_outImage($canvas);
  }
 
  private function _outImage($canvas) {
    header('Content-type: image/jpeg');
    imagejpeg($canvas);
  }
}
 
 
/* Using the extended class */
$face_detect = new FaceModify('detection.dat');
$face_detect->face_detect('sample-image3.jpg');
$face_detect->resizeFace(100,100);
 
?>

NB: If for some reason the code returns a error, turn off error reporting in php.ini

Further Links

1. Automatic Face Detection in Photos with PHP
2. http://www.facedetection.com/

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at]gmail.com for any new project requirements and price quotes.

10 Responses

1

Juan C. Monestel

July 7th, 2011 at 5:14 am

Great article… better if was multiple detection hehe

2

Gesichtserkennung mit PHP | tagdocs.de

July 8th, 2011 at 4:18 am

[...] beruht (und daher ohne jegliche Hilfsbibliotheken wie OpenCV, etc. auskommt).Die Klasse von Sameer Borate bietet eine Reihe nützlicher Features wobei die gleiche Einschränkung wie schon damals gilt: die [...]

3

m a r c o

July 19th, 2011 at 2:13 am

Hi,
Nice idea, but it doesn’t seem to find any face on the image from your previous javascript demo :-/ …any idea?
I modified your code to get multiple faces, and it gives very uncertain results…
Regards,
m a r c o

4

La détection des visages en PHP | MakeMonSite

July 23rd, 2011 at 9:55 pm

[...] Source :: Codediesel [...]

5

Linkdump #55: PHP – Personal Home Page. « Tomasz Kowalczyk

August 24th, 2011 at 1:48 pm

[...] Face detection in images using PHP. Wykrywanie twarzy w PHP. [...]

6

Fred

November 15th, 2012 at 8:35 am

Hello, thank you for this tutorial. We successfully added this code in our application for android to handle remote images. Can you update this code to get multiple faces detection? Please.
Best regards

7

Leírások.tk

March 11th, 2013 at 2:41 am

[...] képeket, vagy az arcot. A codediesel.com oldalon éppen egy ilyen cikkre akadtam (Face detection in images using PHP), amiben PHP használatával, JPG képből egy osztály és némi adat [...]

8

Mostafa

June 6th, 2014 at 12:42 pm

Hi,
it’s very googd script. but…

how i can save the face witch detect?

9

Stef157

July 29th, 2014 at 4:55 am

Do it like this :

public function cropFace($path) {
$canvas = imagecreatetruecolor($this->face['w'], $this->face['w']);
imagecopy($canvas, $this->canvas, 0, 0, $this->face['x'], $this->face['x'], $this->face['w'], $this->face['w']);
//header(‘Content-type: image/jpeg’);
imagejpeg($canvas, $path);
}

10

Closca

September 4th, 2014 at 6:31 am

HI, i am using this to know how to resize and image and don;t lose the face, but if the face is undetectable stays alot of time to analyze, over 30 seconds for one image,
how could it make it faster, or somehow to decide faster if the face exists?

Your thoughts

Sign up for fresh content in your email