<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/analyze-face-emotions-with-r/"></div></div><div><a data-url="http://www.r-bloggers.com/analyze-face-emotions-with-r/" data-counturl="http://www.r-bloggers.com/analyze-face-emotions-with-r/" data-count="horizontal" data-text="Analyze Face Emotions with R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://thinktostart.com/analyze-face-emotions-r/"> ThinkToStart » R Tutorials</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Human faces provide various information about <a target="_blank" href="https://en.wikipedia.org/wiki/Emotion">emotions</a>. <a target="_blank" href="https://www.microsoft.com/cognitive-services/en-us/emotion-api">Microsoft</a> 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.</p>
<p>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.</p>
<p>The implementation in R allows to analyze human faces in a structured way. Note, that one have to <a target="_blank" href="https://login.live.com/oauth20_authorize.srf?client_id=b5dbf12c-811e-4f91-aee1-da81da0a2c94&scope=wl.signin%20wl.emails&response_type=code&redirect_uri=https%3A%2F%2Fwww.microsoft.com%2Fcognitive-services%2FExternal%2FLogOn%3FReturnUrl%3D%252Fcognitive-services%252Fen-us%252Fsubscriptions%26__provider__%3Dmicrosoft%26__sid__%3D70468006091e4a0c8a8163d6332c5ebe">create an account</a> to use the Face API.</p>
<p>The example is referred to a simple <a target="_blank" href="https://www.whitehouse.gov/sites/whitehouse.gov/files/images/first-family/44_barack_obama%5B1%5D.jpg">example</a>:</p>
<p><img alt="" class="wp-image-2087" src="http://i1.wp.com/thinktostart.com/wp-content/uploads/2016/06/Obama-e1465245350513.jpg?resize=206%2C165" data-recalc-dims="1"></p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>You need four R packages: <a target="_blank" href="https://cran.r-project.org/web/packages/httr/index.html">httr</a>, <a target="_blank" href="https://cran.r-project.org/web/packages/XML/index.html">XML</a>, <a target="_blank" href="https://cran.r-project.org/web/packages/stringr/index.html">stringr</a>, <a target="_blank" href="https://cran.r-project.org/web/packages/ggplot2/index.html">ggplot2.</a></p>
<h3>Analyze Face Emotions with R:</h3>

<pre>#####################################################################
# Load relevant packages
library(&quot;httr&quot;)
library(&quot;XML&quot;)
library(&quot;stringr&quot;)
library(&quot;ggplot2&quot;)

# 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&lt;-as.data.frame(as.matrix(Obama$scores))

# Make some transformation
o$V1 &lt;- lapply(strsplit(as.character(o$V1 ), &quot;e&quot;), &quot;[&quot;, 1)
o$V1&lt;-as.numeric(o$V1)
colnames(o)[1] &lt;- &quot;Level&quot;

# Define names
o$Emotion&lt;- rownames(o)

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

#####################################################################
# 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 = &quot;https://api.projectoxford.ai/face/v1.0/detect?returnFaceId=true&amp;returnFaceLandmarks=true&amp;returnFaceAttributes=age&quot;

# 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&lt;-as.data.frame(as.matrix(ObamaR$faceLandmarks))

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

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

OR$V1&lt;-NULL</pre>

<p> </p>
<p>The post <a target="_blank" rel="nofollow" href="http://thinktostart.com/analyze-face-emotions-r/">Analyze Face Emotions with R</a> appeared first on <a target="_blank" rel="nofollow" href="http://thinktostart.com/">ThinkToStart</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/analyze-face-emotions-with-r/"></div></div><div><a data-url="http://www.r-bloggers.com/analyze-face-emotions-with-r/" data-counturl="http://www.r-bloggers.com/analyze-face-emotions-with-r/" data-count="vertical" data-text="Analyze Face Emotions with R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://thinktostart.com/analyze-face-emotions-r/"> ThinkToStart » R Tutorials</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/9owR72MmyFw"></a></div>