raw code

PHP Facedetect Extension

Robert Eisele

With OpenCV it is possible to find structures on images. The library is shipped with training files to detect faces from different perspectives on images. To access the library and detect faces via PHP I wrote a wrapper.

Overview

This extension provides a PHP implementation of the OpenCV library. The extension offers two new functions. In princible, they differ only by their return value. The first returns only the number of faces found on the given image and the other an associative array of their coordinates.

<?php

var_dump(face_count('party.jpeg', 'cascade.xml'));
var_dump(face_detect('party.jpeg', 'cascade.xml'));

?>

Installation

Installing with `pecl` command-line utility

  1. Install OpenCV (OpenCV 2.3.1)
  2. Execute command "pecl install facedetect"
  3. Make sure you have extension=facedetect.so in your php.ini

Installing from sources

  1. Install OpenCV (OpenCV 2.3.1)
  2. Download facedetect source package
  3. Unpack facedetect source package
  4. Go to facedetect folder and type "phpize && ./configure && make && make install"
  5. Make sure you have extension=facedetect.so in your php.ini

Compiling Facedtect into PHP

  1. Install OpenCV (OpenCV 2.3.1)
  2. Download facedetect source package
  3. Unpack facedetect source package to $PHP_SOURCE_DIR/ext/facedetect
  4. In php source root directory run commands: "rm configure && ./buildconf --force"
  5. Configure PHP with command "./configure --with-facedetect"
  6. Run make && make install

Installation note

You should know, that OpenCV needs the following libraries and programs:

  1. pkgconfig
  2. libpng
  3. zlib
  4. libjpeg
  5. libtiff
  6. python

If you use Debian you can install the needed packages by run the following command:

# apt-get install pkg-config python libjpeg62-dev libpng12-dev libtiff4-dev

For details on how install OpenCV on Linux check out the OpenCV install guide

Links