Face API in R – Microsoft Cognitive Services

Face API in R – Microsoft Cognitive Services


In this blog we will explore Face API in R (Face API – Microsoft Cognitive Services)

This API can detect human faces in image and returns face locations, landmarks, and other important attributes like (age, gender, smile & glasses etc.)

Click here & Register for the free subscription of Microsoft Cognitive Services (Face API).

Here is my free subscription. Free 30,000 transactions per month. 20 per Minute.


After registering please copy the Subscription Key. This is the Subscription Key which provides access to this API.

Face – Detect API

Request URL:


Request Parameters:

Parameter 1: returnFaceId (Optional) 
Type: Boolean(Default value is true)
Decription: It return unique faceIds of the detected human face in the image. 

Parameter 2: returnFaceLandmarks (Optional)
Type: Boolean(Default value is false)
Description: It return face landmarks of the detected human face in the image..

Parameter: returnFaceAttributes (Optional) 
Type: String (Comma Seperated) 
Description: It return the face attributes including age, gender, headPose, smile, facialHair & glasses.
Input Example: "returnFaceAttributes=age,gender"

Request Headers:

Content-Type (optional): Media type of the body sent to the API.
application/json or application/octet-stream
Ocp-Apim-Subscription-Key: Subscription key which provides access to this API.

Request Body: JSON or Binary Data

    "url":"Url of the image"

R Commands & Output:-

R Packages Required: httr


# Below is the URL having returnFaceLandmarks = true and returnFaceAttributes = age,gender,headPose,smile,facialHair,glasses

face_api_url = "https://api.projectoxford.ai/face/v1.0/detect?returnFaceLandmarks=true&returnFaceAttributes=age,gender,headPose,smile,facialHair,glasses"

# Below is the image we are going to upload

body_image = upload_file("C:\\Users\\lenovo\\Documents\\image.jpeg")

# Below is the POST methord (Adding Request headers using add_headers)

result = POST(face_api_url,
              body = body_image,
              add_headers(.headers = c("Content-Type"="application/octet-stream",
                                       "Ocp-Apim-Subscription-Key"="Subscription Key")))

API_Output = content(result)

# Coverting Output into R Dataframe 

Output_Face_Attributes = as.data.frame(API_Output)



Input R Code Snapshot: 




Here you can check the attributes of the image.


For more details & other Image and Face API’s, please visit this Link.


Happy Learning! Your feedback would be appreciated!