はじめに
Microsoftのクラウドサービス Azure にある、Face APIでお手軽に画像解析ができる噂を聞いたので、使ってみました。結果、ホントに簡単に画像解析ができました。
参考
こちらのサイトを参考にさせて頂きました。
-
Custom Vision ProjectでエラーでPublishできないときの一つの対処法
- Azureで作成するリソースグループとFace APIの『場所』を統一しておいた方が良いそうです
やること概要
今回やることは以下です。
- Azureポータルにサインインする
- リソースグループを作成する
- Face APIを作成する
- Face APIで画像解析する
では、Azureポータルにサインインした状態から始めていきます。
リソースグループを作ろう
Azureでリソースを作るためには、リソースグループが必要になります。そのため、まずはリソースグループを作成します。Azureポータルにサインインし、リソースグループをクリックします。
追加ボタンからリソースグループを作成します
リソースグループ名やリージョン(場所
という項目と同意っぽい)を設定します。リージョンはFace APIを作成するときに重要になります。そのため今回は、リソースグループ名だけで、リソースグループのリージョンが分かるようにfuga-JapanEast
とリージョン情報を含めた名前にしました。
このようにリソースグループが作成されれば成功です。
Face APIを作ろう
リソースグループが作成できたので、今回の主役Face APIを作成します。
作成したリソースグループ上で追加をクリックします。
Faceで検索すると見つかります。作成ボタンをクリックします
『場所』をリソースグループのリージョンと同じにします。価格レベルF0は無料枠の意味らしいです。
デプロイが完了したらFace API作成完了です。リソースに移動して、画像解析を試してみます。
Face APIで遊ぼう
前回の手順で、m作成したFace APIに移動し、Key1
とエンドポイント
をメモ帳などにコピーします(あとで使います)。その後、API Console
のリンクをクリックします。
Detect
をクリックします。
表示されたサイトの下方にスクロールし、Japan East
をクリックします。(多分、どのリージョンを選択しても問題ないと思います)
Nameのプルダウンで[resource name]…
を選択します。
先ほどメモした情報をもとにResource Name
, Ocp-Apim-Subscription-Key
の2項目を設定します。
少し下にスクロールし、インターネット上にある画像データのURLを選択します。
画像について、こちらの写真を使わせて頂きました。
https://www.pexels.com/ja-jp/photo/1128065/
顔の位置を検出したようです。数値だけではわかりにくいので、オプションを変更し、他の情報も表示してみます。先ほどのURLに、returnFaceAttributes=age
を追加し年齢も表示してみます。
Face APIの実行は、以下のようにcURLコマンドを使用します。(PowerShellを使うならこちらのサイトが参考になります)
(実行するときは、に作成したFaceのキーを登録し、https://hogehoge.local/01.PNG
を分析する画像のURLに変更してください。)
$ curl -v -X POST "https://face-api-fuga.cognitiveservices.azure.com/face/v1.0/detect?returnFaceId=false&returnFaceLandmarks=false&recognitionModel=recognition_01&returnRecognitionModel=false&detectionModel=detection_01&returnFaceAttributes=age" \
-H "Content-Type: application/json" \
-H "Ocp-Apim-Subscription-Key: <FaceAPI Key>" \
--data-ascii '{"url":"https://hogehoge.local/01.PNG"}'
実行結果がこちら、{"age":21.0}
、つまり21才と判断したようです。当たってそうです。
Note: Unnecessary use of -X or --request, POST is already inferred.
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 13.78.17.188:443...
* TCP_NODELAY set
* Connected to face-api-fuga.cognitiveservices.azure.com (13.78.17.188) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
ー中略ー
{ [97 bytes data]
100 178 100 97 100 81 109 91 --:--:-- --:--:-- --:--:-- 201[{"faceRectangle":{"top":378,"left":790,"width":340,"height":340},"faceAttributes":{"age":21.0}}]
* Connection #0 to host face-api-fuga.cognitiveservices.azure.com left intact
今度は、年齢だけでなく性別も表示するようにオプションを変更します。returnFaceAttributes=age,gender
と追加するだけ、楽ですね。
curl -v -X POST "https://face-api-fuga.cognitiveservices.azure.com/face/v1.0/detect?returnFaceId=false&returnFaceLandmarks=false&recognitionModel=recognition_01&returnRecognitionModel=false&detectionModel=detection_01&returnFaceAttributes=age,gender" \
-H "Content-Type: application/json" \
-H "Ocp-Apim-Subscription-Key: <FaceAPI Key>" \
--data-ascii '{"url":"https://hogehoge.local/01.PNG"}'
実行結果の抜粋です。年齢だけでなく性別"gender":"female"
も表示されました。
100 196 100 115 100 81 113 79 0:00:01 0:00:01 --:--:-- 193[{"faceRectangle":{"top":378,"left":790,"width":340,"height":340},"faceAttributes":{"gender":"female","age":21.0}}]
* Connection #0 to host face-api-fuga.cognitiveservices.azure.com left intact
今度はreturnFaceAttributes
で取得できる情報をすべて取得してみます。
$ curl -v -X POST "https://face-api-fuga.cognitiveservices.azure.com/face/v1.0/detect?returnFaceId=false&returnFaceLandmarks=false&recognitionModel=recognition_01&returnRecognitionModel=false&detectionModel=detection_01&returnFaceAttributes=age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise" \
-H "Content-Type: application/json" \
-H "Ocp-Apim-Subscription-Key: <FaceAPI Key>" \
--data-ascii '{"url":"https://hogehoge.local/01.PNG"}'
結果がこちら、情報多いので整形したJSON情報のみ記載しました。
眼鏡かけてない("glasses": "NoGlasses"
)、感情がニュートラル(emotion "neutral": 0.998
)など、ちゃんと分析できているようです。
{
"faceRectangle": {
"top": 378,
"left": 790,
"width": 340,
"height": 340
},
"faceAttributes": {
"smile": 0.001,
"headPose": {
"pitch": -0.6,
"roll": 8.5,
"yaw": 18.6
},
"gender": "female",
"age": 21.0,
"facialHair": {
"moustache": 0.0,
"beard": 0.0,
"sideburns": 0.0
},
"glasses": "NoGlasses",
"emotion": {
"anger": 0.0,
"contempt": 0.0,
"disgust": 0.0,
"fear": 0.0,
"happiness": 0.001,
"neutral": 0.998,
"sadness": 0.001,
"surprise": 0.0
},
"blur": {
"blurLevel": "low",
"value": 0.0
},
"exposure": {
"exposureLevel": "goodExposure",
"value": 0.44
},
"noise": {
"noiseLevel": "low",
"value": 0.0
},
"makeup": {
"eyeMakeup": true,
"lipMakeup": true
},
"accessories": [],
"occlusion": {
"foreheadOccluded": false,
"eyeOccluded": false,
"mouthOccluded": false
},
"hair": {
"bald": 0.05,
"invisible": false,
"hairColor": [
{
"color": "other",
"confidence": 0.93
},
{
"color": "red",
"confidence": 0.83
},
{
"color": "black",
"confidence": 0.53
},
{
"color": "gray",
"confidence": 0.31
},
{
"color": "brown",
"confidence": 0.15
},
{
"color": "blond",
"confidence": 0.14
}
]
}
}
}
おわりに
AzureのFace APIで画像解析してみました。知識なくても機械学習で画像解析できるの便利です。