Facial Recognition Algorithm with R

Introduction: This is a sample algorith which shows the power of Machine Leraning and Neural Networks to learn (supervised and unsupervised) to recognize attributes. This could be put to use in big data content analysis and digital ethnography. The African Leadership Centre is working on developing robusts systems which could put some of the ideas behind this algorithm to use in the areas of peacebuilding and security research in Africa. 


Subject: Barack Obama

Objective: To determine the Age, Gender and Facial Attributes of the Subject





faceURL = "https://api.projectoxford.ai/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=true&returnFaceAttributes=age,gender,smile,facialHair"
img.url = 'http://i4.mirror.co.uk/incoming/article7384896.ece/ALTERNATES/s615b/Barack-Obama.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'

When you run faceResponse in R, you this this prompt:

> faceResponse
Response [https://api.projectoxford.ai/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=true&returnFaceAttributes=age,gender,smile,facialHair]
  Date: 2016-10-30 14:45
  Status: 200
  Content-Type: application/json; charset=utf-8
  Size: 1.28 kB

If the call was successful a “Status: 200” is returned and the response object is filled with interesting information. The API returns the information as JSON which is parsed by R into nested lists.

Now to define the facial Attributes using the API

ObamaFace = content(faceResponse)[[1]]

You get the following Response when you run ObamaFace$faceAttributes in R

> ObamaFace$faceAttributes
[1] 0.308

[1] "male"

[1] 54.5

[1] 0.1

[1] 0.2

[1] 0.1

The results show that Obama's age was 54.5 when this picture was taken and that his gender is male. Other attributes such as beard, moustache etc show that the subject has less of those attributes.