Analyze Face Emotions with R

June 7, 2016
By

(This article was first published on ThinkToStart » R Tutorials, and kindly contributed to R-bloggers)

Human faces provide various information about emotionsMicrosoft launched a free service in December 2015 to analyze human faces detecting their emotions. The emotions detected are anger, contempt, disgust, fear, happiness, neutral, sadness, and surprise. These emotions are understood to be cross-culturally and universally communicated with particular facial expressions.

The Emotion API takes an facial expression in an image as an input, and returns the confidence across a set of emotions for each face in the image, as well as bounding box for the face, using the Face API.

The implementation in R allows to analyze human faces in a structured way. Note, that one have to create an account to use the Face API.

The example is referred to a simple example:

 

 

 

 

You need four R packages: httrXMLstringrggplot2.

Analyze Face Emotions with R:

#####################################################################
# Load relevant packages
library("httr")
library("XML")
library("stringr")
library("ggplot2")

# Define image source
img.url     = 'https://www.whitehouse.gov/sites/whitehouse.gov/files/images/first-family/44_barack_obama[1].jpg'

# Define Microsoft API URL to request data
URL.emoface = 'https://api.projectoxford.ai/emotion/v1.0/recognize'

# Define access key (access key is available via: https://www.microsoft.com/cognitive-services/en-us/emotion-api)
emotionKEY = 'XXXX'

# Define image
mybody = list(url = img.url)

# Request data from Microsoft
faceEMO = POST(
  url = URL.emoface,
  content_type('application/json'), add_headers(.headers = c('Ocp-Apim-Subscription-Key' = emotionKEY)),
  body = mybody,
  encode = 'json'
)

# Show request results (if Status=200, request is okay)
faceEMO

# Reuqest results from face analysis
Obama = httr::content(faceEMO)[[1]]
Obama
# Define results in data frame
o<-as.data.frame(as.matrix(Obama$scores))

# Make some transformation
o$V1 <- lapply(strsplit(as.character(o$V1 ), "e"), "[", 1)
o$V1<-as.numeric(o$V1)
colnames(o)[1] <- "Level"

# Define names
o$Emotion<- rownames(o)

# Make plot
ggplot(data=o, aes(x=Emotion, y=Level)) +
  geom_bar(stat="identity")

#####################################################################
# Define image source
img.url = 'https://www.whitehouse.gov/sites/whitehouse.gov/files/images/first-family/44_barack_obama[1].jpg'

# Define Microsoft API URL to request data
faceURL = "https://api.projectoxford.ai/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=true&returnFaceAttributes=age"

# Define access key (access key is available via: https://www.microsoft.com/cognitive-services/en-us/emotion-api)
faceKEY = 'XXXXX'

# Define image
mybody = list(url = img.url)

# Request data from Microsoft
faceResponse = POST(
  url = faceURL, 
  content_type('application/json'), add_headers(.headers = c('Ocp-Apim-Subscription-Key' = faceKEY)),
  body = mybody,
  encode = 'json'
)

# Show request results (if Status=200, request is okay)
faceResponse

# Reuqest results from face analysis
ObamaR = httr::content(faceResponse)[[1]]

# Define results in data frame
OR<-as.data.frame(as.matrix(ObamaR$faceLandmarks))

# Make some transformation to data frame
OR$V2 <- lapply(strsplit(as.character(OR$V1), "\\="), "[", 2)
OR$V2 <- lapply(strsplit(as.character(OR$V2), "\\,"), "[", 1)
colnames(OR)[2] <- "X"
OR$X<-as.numeric(OR$X)

OR$V3 <- lapply(strsplit(as.character(OR$V1), "\\y = "), "[", 2)
OR$V3 <- lapply(strsplit(as.character(OR$V3), "\\)"), "[", 1)
colnames(OR)[3] <- "Y"
OR$Y<-as.numeric(OR$Y)

OR$V1<-NULL

 

The post Analyze Face Emotions with R appeared first on ThinkToStart.

To leave a comment for the author, please follow the link and comment on their blog: ThinkToStart » R Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)