Created
November 27, 2014 12:55
-
-
Save yene/395703f19752e30bbfd1 to your computer and use it in GitHub Desktop.
PHP mark faces in images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (extension_loaded("facedetect")) { | |
echo "facedetect extension loaded \n"; | |
} else { | |
echo "facedetect extension not loaded, can't do face detect \n"; | |
} | |
$file = "haarcascade_frontalface_alt.xml"; | |
exec("rm generated/*"); | |
foreach (glob("original/*.jpg") as $image) { | |
$count = \face_count($image, $file); | |
$detect = \face_detect($image, $file); | |
echo $image . " - " . $count . PHP_EOL; | |
list($width, $height) = getimagesize($image); | |
$draw = ""; | |
$all_x1 = array(); | |
$all_y1 = array(); | |
$all_x2 = array(); | |
$all_y2 = array(); | |
foreach ($detect as $face) { | |
$from_x = $face['x']; | |
$from_y = $face['y']; | |
$to_x = $face['x'] + $face['w']; | |
$to_y = $face['y'] + $face['h']; | |
$draw .= "rectangle $from_x,$from_y $to_x,$to_y "; | |
$all_x1[] = $face['x']; | |
$all_y1[] = $face['y']; | |
$all_x2[] = $face['x'] + $face['w']; | |
$all_y2[] = $face['y'] + $face['h']; | |
} | |
$box = ""; | |
if ($count > 0) { | |
$x1 = min($all_x1); | |
$y1 = min($all_y1); | |
$x2 = max($all_x2); | |
$y2 = max($all_y2); | |
$box = "-fill none -strokewidth 1 -stroke green -draw \"rectangle $x1,$y1 $x2,$y2\" "; | |
$center_x = $x1 + (($x2-$x1)/2); | |
$center_y = $y1 + (($y2-$y1)/2); | |
$x1 = $center_x-15; | |
$y1 = $center_y-15; | |
$x2 = $center_x+15; | |
$y2 = $center_y+15; | |
$box .= " -fill \"rgba( 255, 215, 0 , 0.5 )\" -stroke none -strokewidth 0 -draw \"rectangle $x1,$y1 $x2,$y2\" "; | |
} | |
$newImage = "generated/" . basename($image); | |
exec("convert $image -fill none -strokewidth 1 -stroke red -draw \"$draw\" $box $newImage"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment