Search code examples
razurehttrazure-cognitive-services

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

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?


Solution

  • There no current R code example in FACE API Documentation. 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
    ...