[PROJECT STOPPED] Feature Detection based on Haar Cascades in PHP (Viola-Jones-Lienhart et al Algorithm)
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



Further development on this project has stopped!!

Feature Detection Library for PHP

Based on Viola-Jones Feature Detection Algorithm using Haar Cascades and improvement Viola-Jones-Lienhart et al Feature Detection Algorithm

This is a port of OpenCV C++ Haar Detection (actually a port of JViolaJones which is a port of OpenCV for Java) to PHP



###How to Use You can use the existing openCV cascades to build your detectors.

To do this just transform the opencv xml file to PHP format using the haartophp (php) tool (in cascades folder)


to use opencv's haarcascades_frontalface_alt.xml in php do:

haartophp haarcascades_frontalface_alt.xml > haarcascades_frontalface_alt.php

this creates a php file: haarcascades_frontalface_alt.php which you can include in your php application (see examples)

the variable to use in php is similarly

IMPORTANT : The conversion process has changed from previous versions (both in the CLI script and in HAARPHP)

The changes are :

  • the feature rectangle coordinates are stored in an array instead of an object hash
  • the tilted flag/attribute for rectangles (Rainer Lienhart et al.) is incorporated in the conversion

so make sure to re-convert your .php xml cascades for this version of HAARPHP

The tilted attribute for rectangles (Rainer Lienhart et al. extension of the algorithm) describes tilted (rotated 45 deg) features (rectangles) in order to detect more features at rotated positions

The tilted flag is added in the .php file (as tilt ), (see also HAAR.js)

This is now incorporated into HAARPHP and this increases the compatibility with the OpenCV cascades (when they use tilted features, eg mouth cascade)

####Detector Methods


new detector($haardata);

Explanation of parameters

  • $haardata : The actual haardata (as generated by haartophp tool), this is specific per feature, openCV haar data can be used.



Clear any cached image data and haardata in case space is an issue. Use image method and cascade method (see below) to re-set image and haar data



Allow to use same detector (with its cached image data), to detect different feature on same image, by using another cascade. This way any image pre-processing is done only once

Explanation of parameters

  • $haardata : The actual haardata (as generated by haartojs tool), this is specific per feature, openCV haar data can be used.


$detector->image($GDImage, $scale);

Explanation of parameters

  • $GDImage : an actual GD Image object.
  • $scale : The percent of scaling from the original image, so detection proceeds faster on a smaller image (default 0.5 ). NOTE scaling might alter the detection results sometimes, if having problems opt towards 1 (slower)


$detector->selection('auto'|array|feature|$x [,$y, $width, $height]);

Allow to set a custom region in the image to confine the detection process only in that region (eg detect nose while face already detected)

Explanation of parameters

  • 1st parameter : This can be the string 'auto' which sets the whole image as the selection, or an array ie: array('x'=>10, 'y'=>'auto', 'width'=>100, 'height'=>'auto'} (every param set as 'auto' will take the default image value) or a detection rectangle/feature, or a x coordinate (along with rest coordinates).
  • $y : (Optional) the selection start y coordinate, can be an actual value or 'auto' ($y=0)
  • $width : (Optional) the selection width, can be an actual value or 'auto' ($width=image.width)
  • $height : (Optional) the selection height, can be an actual value or 'auto' ($height=image.height)

The actual selection rectangle/feature is available as $this->Selection or $detector->Selection


$detector->cannyThreshold(array('low'=> lowThreshold, 'high'=> highThreshold));

Set the thresholds when Canny Pruning is used, for extra fine-tuning. Canny Pruning detects the number/density of edges in a given region. A region with too few or too many edges is unlikely to be a feature. Default values work fine in most cases, however depending on image size and the specific feature, some fine tuning could be needed

Explanation of parameters

  • low : (Optional) The low threshold (default 20 ).
  • high : (Optional) The high threshold (default 100 ).


$detector->detect($baseScale, $scale_inc, $increment, $min_neighbors, $doCannyPruning);

Explanation of parameters (JViolaJones Parameters)

  • $baseScale : The initial ratio between the window size and the Haar classifier size (default 1 ).
  • $scale_inc : The scale increment of the window size, at each step (default 1.25 ).
  • $increment : The shift of the window at each sub-step, in terms of percentage of the window size (default 0.1 ).
  • $min_neighbors : The minimum numbers of similar rectangles needed for the region to be considered as a feature (avoid noise) (default 1 )
  • $doCannyPruning : enable Canny Pruning to pre-detect regions unlikely to contain features, in order to speed up the execution (optional default true ).

Examples included with face detection

HAARPHP is also part of PHP classes http://www.phpclasses.org/package/7393-PHP-Detect-features-on-images-such-as-faces-or-mouths.html

###Where to find Haar Cascades xml files to use for feature detection


  • keep up with the changes in openCV cascades xml format (will try)



  • refactor code (make smaller)
  • add clearCache method, to delete any stored/cached image data in the detector (in case space is an issue)
  • add the tilted feature (Lienhart et al, extension)
  • make new haartophp tool, output format changed, make sure to re-convert your .php haar cascades!!
  • tidy up the repo
  • fix some typos, edits
  • project stopped


  • add new methods (selection , cascade , cannyThreshold )
  • use fixed-point arithmetic if possible (eg gray-scale, canny computation)
  • optimize array indexing, remove unnecessary multiplications
  • reduce unnecessary loops, inline code instead of method calling for speed
  • rewrite merge method (features might be slightly different now)
  • features are now generic classes not arrays
  • code refactor/fixes
  • update readme, add method documentation


  • add haartophp tool in php (all-php solution)
  • optimize array operations, refactor, etc..


  • initial release

URL Nikos Web Development
URL Haar.php blog post
URL WorkingClassCode