R:使用POST()httr从Azure Face Recognition服务中检索答案

问题描述 投票:0回答:1

我想从Microsoft Azure https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236连接FACE API我不知道如何在代码中实现faceLandmarks和faceAttributes

library(httr)
library("XML")
library("jsonlite")


faceURL <- 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect'
faceKEY <- 'XXX'
img.url <- 'https://www.economist.com/sites/default/files/imagecache/640-width/images/2019/02/articles/main/20190216_wbp504.jpg'
mybody = list(url = img.url)

faceResponse = POST(
  url = faceURL, 
  content_type('application/json'), add_headers(.headers = c('Ocp-Apim-Subscription-Key' = faceKEY)),
  body = mybody,
  encode = 'json'
)
content(faceResponse)

我只得到:

[[1]]$`faceId`

[[1]]$faceRectangle

如何获取faceLandmarks和faceAttributes的结果?

r azure httr azure-cognitive-services
1个回答
0
投票

FACE API文档中没有当前的R代码示例。但是解决方案是将URL更改为:

faceURL <- 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=true&returnFaceAttributes=age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise'

经过分析:

outcome = httr::content(faceResponse)[[1]]
names(outcome)
[1] "faceId"         "faceRectangle"  "faceLandmarks"  "faceAttributes"

最后我们得到了:

> content(faceResponse)
[[1]]
[[1]]$`faceId`
[1] "XXX"
[[1]]$faceRectangle
[[1]]$faceRectangle$`top`
[1] 80

[[1]]$faceRectangle$left
[1] 221

[[1]]$faceRectangle$width
[1] 147

[[1]]$faceRectangle$height
[1] 147


[[1]]$faceLandmarks
[[1]]$faceLandmarks$`pupilLeft`
[[1]]$faceLandmarks$`pupilLeft`$`x`
[1] 264.7

[[1]]$faceLandmarks$`pupilLeft`$y
[1] 122.4


[[1]]$faceLandmarks$pupilRight
[[1]]$faceLandmarks$pupilRight$`x`
[1] 330.8

[[1]]$faceLandmarks$pupilRight$y
[1] 119.9


[[1]]$faceLandmarks$noseTip
[[1]]$faceLandmarks$noseTip$`x`
[1] 285.3
...
© www.soinside.com 2019 - 2024. All rights reserved.