Skip to content

Instantly share code, notes, and snippets.

@mgng
Created March 19, 2012 01:55
Show Gist options
  • Select an option

  • Save mgng/2090190 to your computer and use it in GitHub Desktop.

Select an option

Save mgng/2090190 to your computer and use it in GitHub Desktop.
face_detect.php
<?php
$file_path = './sample.jpg';
$cascade = './haarcascade_frontalface_alt.xml';
$faces = face_detect( $file_path, $cascade );
if ( ! count( $faces ) ) {
exit( '顔がない' );
}
$img = imagecreatefromjpeg( $file_path );
$line_color = imagecolorallocate( $img, 255, 0, 0 );
foreach ( $faces as $f ) {
imagepolygon( $img, array(
$f['x'], $f['y'],
$f['x'] + $f['width'], $f['y'],
$f['x'] + $f['width'], $f['y'] + $f['height'],
$f['x'], $f['y'] + $f['height'],
),
4,
$line_color
);
}
imagejpeg( $img, "{$file_path}.face_detect.jpg" );
imagedestroy( $img );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment