简体   繁体   English

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

[英]R: Retrieving answer from Azure FaceRecognition service using POST() httr

I want to connect with FACE API from Microsoft Azure https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236 I do not know how to implement faceLandmarks and faceAttributes into the code 我想从Microsoft Azure与FACE API连接https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236我不知道如何在代码中实现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)

I only got: 我只有:

[[1]]$`faceId`

and

[[1]]$faceRectangle

How to obtain the results of faceLandmarks and faceAttributes? 如何获取faceLandmarks和faceAttributes的结果?

There no current R code example in FACE API Documentation. FACE API文档中没有当前的R代码示例。 However the solution is to change the url to: 但是,解决方案是将网址更改为:

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'

and after analysis: 经过分析:

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

and finally we got: 最后我们得到:

> 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
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM