RでMicrosoft Face APIsを実行する(1)
画像から顔を検出して色々試してみたくなったのでAzureのFace APIsをRで実行してみる。
参考:
1. Azureアカウントの作成
Face APIを実行するのにAzureのアカウントが必要になるのでリンク先を参考に作成しておく。
5分でAzureアカウントを作ろう!|始め方 アカウント作成 Windowsアカウント| | ナレコムazureレシピ
2. Face APIの設定
アカウントが作成できたら管理ポータルから以下の手順でMarket PlaceからFace APIを有効にする。
1. 管理ポータル左下の"+新規"を選択
2. "MARKETPLACE"を選択
3. "Face APIs"を選択して右下の"→"を選択
4. リージョンを"日本(西)"に変更して右下の"→"を選択
2015年11月4日現在は無料のようでこのあとの"購入の確認"で右下の"購入"を選択すると購入完了となる。
5. 管理ポータルの"FaceAPIs"を選択
6. "次の手順のために Microsoft Web サイトにアクセスする"を選択
7. "show"を選択して"subscription key"を控える
ここで表示されるsubscription keyがAPIの実行に必要となる。
3. Rから実行
APIを実行できる準備ができたので試しにFace Detectionを実行してみる。バイナリファイルのPOSTが不安だったけどhttr:upload_file()で簡単に出来た。
レナ (画像データ) - Wikipedia(↓)で試した結果は以下の通り。
> str(content(res)) List of 1 $ :List of 4 ..$ faceId : chr "4565f401-2072-461d-a315-f92c86d7fbc7" ..$ faceRectangle:List of 4 .. ..$ top : int 227 .. ..$ left : int 217 .. ..$ width : int 161 .. ..$ height: int 161 ..$ faceLandmarks:List of 27 .. ..$ pupilLeft :List of 2 .. .. ..$ x: num 263 .. .. ..$ y: num 265 .. ..$ pupilRight :List of 2 .. .. ..$ x: num 326 .. .. ..$ y: num 266 .. ..$ noseTip :List of 2 .. .. ..$ x: num 315 .. .. ..$ y: num 318 .. ..$ mouthLeft :List of 2 .. .. ..$ x: num 266 .. .. ..$ y: num 350 .. ..$ mouthRight :List of 2 .. .. ..$ x: num 318 .. .. ..$ y: num 347 .. ..$ eyebrowLeftOuter :List of 2 .. .. ..$ x: num 244 .. .. ..$ y: num 249 .. ..$ eyebrowLeftInner :List of 2 .. .. ..$ x: num 293 .. .. ..$ y: num 255 .. ..$ eyeLeftOuter :List of 2 .. .. ..$ x: num 254 .. .. ..$ y: num 266 .. ..$ eyeLeftTop :List of 2 .. .. ..$ x: num 269 .. .. ..$ y: num 259 .. ..$ eyeLeftBottom :List of 2 .. .. ..$ x: num 267 .. .. ..$ y: num 274 .. ..$ eyeLeftInner :List of 2 .. .. ..$ x: num 280 .. .. ..$ y: num 270 .. ..$ eyebrowRightInner :List of 2 .. .. ..$ x: num 327 .. .. ..$ y: num 255 .. ..$ eyebrowRightOuter :List of 2 .. .. ..$ x: num 351 .. .. ..$ y: num 248 .. ..$ eyeRightInner :List of 2 .. .. ..$ x: num 319 .. .. ..$ y: num 271 .. ..$ eyeRightTop :List of 2 .. .. ..$ x: num 329 .. .. ..$ y: num 261 .. ..$ eyeRightBottom :List of 2 .. .. ..$ x: num 330 .. .. ..$ y: num 274 .. ..$ eyeRightOuter :List of 2 .. .. ..$ x: num 338 .. .. ..$ y: num 266 .. ..$ noseRootLeft :List of 2 .. .. ..$ x: num 297 .. .. ..$ y: num 272 .. ..$ noseRootRight :List of 2 .. .. ..$ x: num 315 .. .. ..$ y: num 273 .. ..$ noseLeftAlarTop :List of 2 .. .. ..$ x: num 291 .. .. ..$ y: num 306 .. ..$ noseRightAlarTop :List of 2 .. .. ..$ x: num 319 .. .. ..$ y: num 305 .. ..$ noseLeftAlarOutTip :List of 2 .. .. ..$ x: num 285 .. .. ..$ y: num 321 .. ..$ noseRightAlarOutTip:List of 2 .. .. ..$ x: num 322 .. .. ..$ y: num 318 .. ..$ upperLipTop :List of 2 .. .. ..$ x: num 304 .. .. ..$ y: num 344 .. ..$ upperLipBottom :List of 2 .. .. ..$ x: num 302 .. .. ..$ y: num 350 .. ..$ underLipTop :List of 2 .. .. ..$ x: num 299 .. .. ..$ y: num 354 .. ..$ underLipBottom :List of 2 .. .. ..$ x: num 298 .. .. ..$ y: num 363 ..$ attributes :List of 3 .. ..$ headPose:List of 3 .. .. ..$ pitch: num 0 .. .. ..$ roll : num 0.5 .. .. ..$ yaw : num 29.2 .. ..$ gender : chr "female" .. ..$ age : int 27
- faceId
- faceRectangle
- 画像内での顔の位置
- faceLandmarks
- 顔の特徴点(瞳孔とか鼻の頭とか)
- attributes
- 顔の向き、性別、年齢
顔の位置や特徴点をプロットしてみると帽子に隠れた右眉の端も検知できていることが分かる。
> library(dplyr) > library(ggplot2) > library(png) > library(grid) > > # 画像の読み込み > lena <- readPNG("lena_std.png") > > # 顔の位置 > fr <- content(res)[[1]]$faceRectangle %>% + as.data.frame() > > # 特徴点のx座標 > flm.x <- content(res)[[1]]$faceLandmarks %>% + data.frame() %>% + select(ends_with("x")) %>% + t() > > # 特徴点のy座標 > flm.y <- content(res)[[1]]$faceLandmarks %>% + data.frame() %>% + select(ends_with("y")) %>% + t() > > # プロット用のデータフレーム > # faceLandmarksのy座標は画像の上辺が0となる > rect <- data.frame(x1 = fr$left , + x2 = fr$left + fr$width, + y1 = dim(lena)[1] - fr$top, + y2 = dim(lena)[1] - fr$top - fr$height) > > flm <- data.frame(x = flm.x, y = dim(lena)[1] - flm.y) > > # プロット > g <- rasterGrob(lena, interpolate=TRUE) > qplot(0:dim(lena)[2], 0:dim(lena)[1], geom="blank") + + annotation_custom(g, xmin=0, xmax=dim(lena)[2], ymin=0, ymax=dim(lena)[1]) + + geom_point(data=flm, mapping=aes(x=x, y=y), color="blue") + + geom_rect(data=rect, mapping=aes(xmin=x1,xmax=x2, ymin=y1, ymax=y2), color="blue", alpha=0.0) + + coord_fixed()