仕事で顔認証を行うことになりAmazon Rekognitionについて試しに使ってみることにした
Amazon Rekognition料金
料金はリージョンによってことなりますが、1ヶ月単位で、使用回数数によって金額が違い、1000枚単位金額のようです。
アジアより、米国のほうが安い印象なので、特に違いなければ、安いリージョン使用してもいいかもしれません。
ついでに12ヶ月の無料枠もあるので、気軽に試してみるのもいいかもです
Amazon Rekognitionガイド
AWSの公式のセットアップガイドにわかりやすく記載されてます
https://docs.aws.amazon.com/ja_jp/rekognition/latest/dg/setting-up.html
その他こちらの記事なども参考にやっていこうと思います
https://qiita.com/nowmura/items/7d6437298e15d7c9d4d4
https://qiita.com/ran/items/8bdf17d546a3ed1ec440
さっそく試してみることにします
IAMユーザー作成
AWSにログインし、以下にアクセス
https://console.aws.amazon.com/iam/home?region=ap-northeast-1#/home
左のタブのユーザーにアクセスし、ユーザーを追加からユーザー追加画面に移動
今回プログラムから使用したいため、「プログラムによるアクセス」にチェック
アクセス権の許可は、既存のポリシーの「AmazonRekognitionReadOnlyAccess」を選択するため、以下の画像のように選択します
その他先に進めると最後にアクセスキーが発行されます。
こちらで記載されるアクセスキーIDとシークレットアクセスキーはプログラムから使用する際に使うので絶対メモするようにしてください
※なくした場合は、再発行する必要があります
SDK追加
AWSには様々な言語のSDKが用意されているみたいです。
https://aws.amazon.com/jp/tools/#sdk
今回一番私が慣れていることもあり、PHPのSDKを使用することにしました
https://aws.amazon.com/jp/sdk-for-php/
composer を使用しSDKをインストールします
今回sdk-for-phpのバージョン3を使用したいため、インストールする環境はPHP 5.5以上のものにしてください
cd ~/workspace/aws
curl -sS https://getcomposer.org/installer | php
php composer.phar require aws/aws-sdk-php
※composerを使用するために、上のやり方でも大丈夫ですが、公式サイトに記載してますのでそちら参照ください
https://getcomposer.org/download/
顔分析 (DetectFaces)
SDKを使用し、顔を分析してみます
<?php
require 'vendor/autoload.php';
use Aws\Rekognition\RekognitionClient;
// 処理したい画像ファイル
$img_file = 'image.jpg';
$options = [
'region' => 'us-west-2', //リージョン
'version' => 'latest',
'credentials' => [
'key' => '[アクセスキーID]',
'secret' => '[アクセスシークレットキー]'
]
];
try {
$rekognition = new RekognitionClient($options);
// Call DetectFaces //顔分析 Attributes = ALLで表情把握
$result = $rekognition->DetectFaces([
'Image' => [
'Bytes' => file_get_contents($img_file),
],
'Attributes' => ['ALL']
]);
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
exit('DetectFaces error');
}
var_dump($result);
exit();
Attributes には、 DEFAULT
とALL
が指定でき、
DEFAULTだと顔がある位置のみですが、
ALLだと表情まで取得することができます
実行結果
Aws\Result Object
(
[data:Aws\Result:private] => Array
(
[FaceDetails] => Array
(
[0] => Array
(
[BoundingBox] => Array
(
[Width] => 0.21441695094109
[Height] => 0.19528225064278
[Left] => 0.47312760353088
[Top] => 0.3095155954361
)
[AgeRange] => Array
(
[Low] => 26
[High] => 42
)
[Smile] => Array
(
[Value] =>
[Confidence] => 92.802169799805
)
[Eyeglasses] => Array
(
[Value] =>
[Confidence] => 89.914497375488
)
[Sunglasses] => Array
(
[Value] =>
[Confidence] => 95.990943908691
)
[Gender] => Array
(
[Value] => Male
[Confidence] => 95.914886474609
)
[Beard] => Array
(
[Value] =>
[Confidence] => 86.742286682129
)
[Mustache] => Array
(
[Value] =>
[Confidence] => 98.448387145996
)
[EyesOpen] => Array
(
[Value] => 1
[Confidence] => 93.064506530762
)
[MouthOpen] => Array
(
[Value] =>
[Confidence] => 95.486305236816
)
[Emotions] => Array
(
[0] => Array
(
[Type] => CALM
[Confidence] => 81.099479675293
)
[1] => Array
(
[Type] => HAPPY
[Confidence] => 7.1107492446899
)
[2] => Array
(
[Type] => SAD
[Confidence] => 5.1335315704346
)
[3] => Array
(
[Type] => SURPRISED
[Confidence] => 2.3607263565063
)
[4] => Array
(
[Type] => CONFUSED
[Confidence] => 1.9357062578201
)
[5] => Array
(
[Type] => FEAR
[Confidence] => 1.2569398880005
)
[6] => Array
(
[Type] => ANGRY
[Confidence] => 0.77903920412064
)
[7] => Array
(
[Type] => DISGUSTED
[Confidence] => 0.32382744550705
)
)
[Landmarks] => Array
(
[0] => Array
(
[Type] => eyeLeft
[X] => 0.5526008605957
[Y] => 0.36890825629234
)
[1] => Array
(
[Type] => eyeRight
[X] => 0.64599227905273
[Y] => 0.39083537459373
)
[2] => Array
(
[Type] => mouthLeft
[X] => 0.52641272544861
[Y] => 0.44730740785599
)
[3] => Array
(
[Type] => mouthRight
[X] => 0.6047345995903
[Y] => 0.4657144844532
)
[4] => Array
(
[Type] => nose
[X] => 0.57987517118454
[Y] => 0.42365303635597
)
[5] => Array
(
[Type] => leftEyeBrowLeft
[X] => 0.52504652738571
[Y] => 0.3427766263485
)
[6] => Array
(
[Type] => leftEyeBrowRight
[X] => 0.55578184127808
[Y] => 0.34131714701653
)
[7] => Array
(
[Type] => leftEyeBrowUp
[X] => 0.58091521263123
[Y] => 0.35215479135513
)
[8] => Array
(
[Type] => rightEyeBrowLeft
[X] => 0.63443237543106
[Y] => 0.36473110318184
)
[9] => Array
(
[Type] => rightEyeBrowRight
[X] => 0.66347640752792
[Y] => 0.36661624908447
)
[10] => Array
(
[Type] => rightEyeBrowUp
[X] => 0.68729877471924
[Y] => 0.38080587983131
)
[11] => Array
(
[Type] => leftEyeLeft
[X] => 0.53598934412003
[Y] => 0.36440005898476
)
[12] => Array
(
[Type] => leftEyeRight
[X] => 0.5704557299614
[Y] => 0.37412104010582
)
[13] => Array
(
[Type] => leftEyeUp
[X] => 0.55374437570572
[Y] => 0.36521527171135
)
[14] => Array
(
[Type] => leftEyeDown
[X] => 0.55121928453445
[Y] => 0.37252825498581
)
[15] => Array
(
[Type] => rightEyeLeft
[X] => 0.62682664394379
[Y] => 0.38734859228134
)
[16] => Array
(
[Type] => rightEyeRight
[X] => 0.66259723901749
[Y] => 0.39413347840309
)
[17] => Array
(
[Type] => rightEyeUp
[X] => 0.64754563570023
[Y] => 0.38724127411842
)
[18] => Array
(
[Type] => rightEyeDown
[X] => 0.64377516508102
[Y] => 0.39426416158676
)
[19] => Array
(
[Type] => noseLeft
[X] => 0.55881685018539
[Y] => 0.42635735869408
)
[20] => Array
(
[Type] => noseRight
[X] => 0.59349393844604
[Y] => 0.43452203273773
)
[21] => Array
(
[Type] => mouthUp
[X] => 0.56815791130066
[Y] => 0.4487968981266
)
[22] => Array
(
[Type] => mouthDown
[X] => 0.55812007188797
[Y] => 0.4715388417244
)
[23] => Array
(
[Type] => leftPupil
[X] => 0.5526008605957
[Y] => 0.36890825629234
)
[24] => Array
(
[Type] => rightPupil
[X] => 0.64599227905273
[Y] => 0.39083537459373
)
[25] => Array
(
[Type] => upperJawlineLeft
[X] => 0.49725943803787
[Y] => 0.35380974411964
)
[26] => Array
(
[Type] => midJawlineLeft
[X] => 0.48038685321808
[Y] => 0.4408900141716
)
[27] => Array
(
[Type] => chinBottom
[X] => 0.54150652885437
[Y] => 0.50966727733612
)
[28] => Array
(
[Type] => midJawlineRight
[X] => 0.64569234848022
[Y] => 0.47971028089523
)
[29] => Array
(
[Type] => upperJawlineRight
[X] => 0.70074242353439
[Y] => 0.40151435136795
)
)
[Pose] => Array
(
[Roll] => 16.933578491211
[Yaw] => 0.79955726861954
[Pitch] => -1.3201627731323
)
[Quality] => Array
(
[Brightness] => 71.724494934082
[Sharpness] => 53.330047607422
)
[Confidence] => 99.99925994873
)
)
[@metadata] => Array
(
[statusCode] => 200
[effectiveUri] => https://rekognition.us-west-2.amazonaws.com
[headers] => Array
(
[content-type] => application/x-amz-json-1.1
[date] => Wed, 30 Dec 2020 15:21:46 GMT
[x-amzn-requestid] => 7b14f50d-f6ba-4b30-b7e0-dea1299c0eae
[content-length] => 3344
[connection] => keep-alive
)
[transferStats] => Array
(
[http] => Array
(
[0] => Array
(
)
)
)
)
)
[monitoringEvents:Aws\Result:private] => Array
(
)
顔比較 (compareFaces)
SDKを使用し、2枚の画像を比較し、一致度など確認できます
<?php
require 'vendor/autoload.php';
use Aws\Rekognition\RekognitionClient;
// 処理したい画像ファイル
$img_file = 'image.jpg';
$img_file2 = 'image2.jpg';
$options = [
'region' => 'us-west-2', //リージョン
'version' => 'latest',
'credentials' => [
'key' => '[アクセスキーID]',
'secret' => '[アクセスシークレットキー]'
]
];
try {
$rekognition = new RekognitionClient($options);
//画像比較
$result2 = $rekognition->compareFaces([
'QualityFilter' => 'NONE',
'SimilarityThreshold' => 90,
'SourceImage' => [
'Bytes' => file_get_contents($img_file)
],
'TargetImage' => [
'Bytes' => file_get_contents($img_file2)
],
]);
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
exit('DetectFaces error');
}
var_dump($result);
exit();
画像二枚を比較し、同一人物してるかなどSimilarityの値で確認することがでいます
値が100に近いほど同一人物に近いです
実行結果
Aws\Result Object
(
[data:Aws\Result:private] => Array
(
[SourceImageFace] => Array
(
[BoundingBox] => Array
(
[Width] => 0.21441695094109
[Height] => 0.19528225064278
[Left] => 0.47312760353088
[Top] => 0.3095155954361
)
[Confidence] => 99.99925994873
)
[FaceMatches] => Array
(
[0] => Array
(
[Similarity] => 99.921951293945
[Face] => Array
(
[BoundingBox] => Array
(
[Width] => 0.20137125253677
[Height] => 0.22035612165928
[Left] => 0.5190526843071
[Top] => 0.13732449710369
)
[Confidence] => 99.997840881348
[Landmarks] => Array
(
[0] => Array
(
[Type] => eyeLeft
[X] => 0.60321754217148
[Y] => 0.21122048795223
)
[1] => Array
(
[Type] => eyeRight
[X] => 0.67072576284409
[Y] => 0.25914442539215
)
[2] => Array
(
[Type] => mouthLeft
[X] => 0.55353808403015
[Y] => 0.29116678237915
)
[3] => Array
(
[Type] => mouthRight
[X] => 0.61000192165375
[Y] => 0.33085966110229
)
[4] => Array
(
[Type] => nose
[X] => 0.61376416683197
[Y] => 0.28621625900269
)
)
[Pose] => Array
(
[Roll] => 36.242065429688
[Yaw] => 10.054999351501
[Pitch] => -2.67072057724
)
[Quality] => Array
(
[Brightness] => 91.273376464844
[Sharpness] => 38.896011352539
)
)
)
)
[UnmatchedFaces] => Array
(
)
[@metadata] => Array
(
[statusCode] => 200
[effectiveUri] => https://rekognition.us-west-2.amazonaws.com
[headers] => Array
(
[content-type] => application/x-amz-json-1.1
[date] => Wed, 30 Dec 2020 15:32:52 GMT
[x-amzn-requestid] => 5ad15813-9671-4a7b-ae0b-4bed9fb00afd
[content-length] => 911
[connection] => keep-alive
)
[transferStats] => Array
(
[http] => Array
(
[0] => Array
(
)
)
)
)
)
[monitoringEvents:Aws\Result:private] => Array
(
)
)
結構気軽につかるもんですね
いろいろ活用していきたいと思います!