Created
January 17, 2013 08:27
-
-
Save mgng/4554540 to your computer and use it in GitHub Desktop.
Lambda Face API サンプル
This file contains hidden or 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 | |
// Lambda Face API サンプル | |
require_once "./face-php-client/Face.php"; | |
$public_key = 'メモった Public Key'; | |
$private_key = 'メモった Private Key'; | |
$img_url = '画像のURL'; | |
// 認証 | |
$client = new Face( $public_key, $private_key ); | |
// Lambda Face API 叩く | |
$res = $client->detect( $img_url ); | |
if ( $res->statusCode !== 200 || $res->body->status !== 'success' ) { | |
exit('error'); | |
} | |
// gdと色作っとく | |
$gd = imagecreatefromjpeg( $img_url ); | |
$red = imagecolorallocate( $gd, 255, 0, 0 ); | |
// photos ってのが detect に渡した写真情報配列 | |
foreach( $res->body->photos as $photo ) { | |
// tags ってのが 写真に含まれる顔情報配列。なかったらスルー | |
if ( ! isset( $photo->tags ) ) { continue; } | |
foreach( $photo->tags as $tag ) { | |
// 両目と口の両端を結ぶ四角形を赤色で塗りつぶす | |
$points = array( | |
$tag->eye_left->x, $tag->eye_left->y, | |
$tag->eye_right->x, $tag->eye_right->y, | |
$tag->mouth_right->x, $tag->mouth_right->y, | |
$tag->mouth_left->x, $tag->mouth_left->y, | |
); | |
imagefilledpolygon( $gd, $points, 4, $red ); | |
} | |
} | |
// 出力 | |
imagejpeg( $gd, 'result.jpg' ); | |
// 解放して終わり | |
imagedestroy( $gd ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment