Microsoft Face API
Jan 28, 2017Before you start, you need to register at Microsoft Cognitive Services and ask for a free trial. Copy Face-Preview API key into an environment variable faceKEY
as follows Sys.setenv(faceKEY = "***YOUR*KEY***")
. Here, I will demonstrate Face API on my Twitter profile picture:
library(httr)
faceURL = "https://westus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceAttributes=age,gender,smile,facialHair"
img.url = "https://pbs.twimg.com/profile_images/420950459289833472/JtRoD1cw_400x400.jpeg"
faceKEY = Sys.getenv("faceKEY")
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'
)
# status ok = 200
faceResponse$status
## [1] 401
All information about a picture is now easily accesible by applying the function content
.
content(faceResponse)
## $error
## $error$code
## [1] "Unspecified"
##
## $error$message
## [1] "Access denied due to invalid subscription key. Make sure you are subscribed to an API you are trying to call and provide the right key."
For example happiness or beardiness can be extracted as follows.
content(faceResponse)[[1]]$faceAttributes$smile
## NULL
content(faceResponse)[[1]]$faceAttributes$facialHair$beard
## NULL
Adapted from Longhow Lam’s Blog by me, a crazy analyst with a little bit of beard.