<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/using-microsofts-emotion-api-to-settle-an-old-argument/"></div></div><div><a data-url="https://www.r-bloggers.com/using-microsofts-emotion-api-to-settle-an-old-argument/" data-counturl="https://www.r-bloggers.com/using-microsofts-emotion-api-to-settle-an-old-argument/" data-count="horizontal" data-text="Using Microsoft’s Emotion API to Settle an Old Argument" 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="https://ntguardian.wordpress.com/2016/11/14/using-microsofts-emotion-api-r-self-portrait/"> R – Curtis Miller's Personal Website</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>So Tuesday happened. I have a lot to say about our President-elect, and you’ll probably see a couple posts about him in the next few weeks. But that’s for later. Today, I’m writing about a more pressing issue, one that has been subject to debate for <em>years</em> without resolution, and I plan to settle this debate once and for all.</p>
<p>Do I look angry in this picture?</p>
<p><img src="https://i2.wp.com/ntguardian.files.wordpress.com/2016/11/selfportrait.jpg?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Here’s the backstory: in my first semester of college in 2010, I took a drawing class. We were required to draw a self-portrait at the beginning of the class, then at the end, after having been trained. I had taken many drawing courses prior to the class and thought myself quite skilled, and I remember when I drew my first self-portrait thinking “How can I do better than this?” But a semester later, I drew this.</p>
<p>I remember after drawing this picture thinking “That’s how.”</p>
<p>Everyone who saw the picture was impressed, from my parents to my art professor (who begged me not to stop drawing; sadly, though, I have not drawn like this since that class, though I hope someday to recover those skills and draw again). I hung the picture on my bedroom door, and one morning, my little sister’s dog Fancy woke me up when she was barking frantically at it; it was so realistic it apparently frightened her.</p>
<p>Yet while the picture was impressive, there was a common reaction to it: I look angry. Everyone would ask me, “Why do you look so angry in this picture?” It even reached a point where my grandpa, when he saw it, pointed to it and said, “This is not you. This is not the person who you are.” (My professor, when I mentioned this to him, said what I managed to do was become so focused I captured that focus and intensity in the drawing.)</p>
<p>I can assure you that I was not angry while drawing this picture. Just consider some of the selfies I took that night:</p>
<p><img src="https://i1.wp.com/ntguardian.files.wordpress.com/2016/11/selfportrait1.jpg?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img src="https://i1.wp.com/ntguardian.files.wordpress.com/2016/11/selfportrait2.jpg?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img src="https://i1.wp.com/ntguardian.files.wordpress.com/2016/11/selfportrait3.jpg?w=456&ssl=1" data-recalc-dims="1"></p>
<p>No people, I was not angry, and I don’t have anger issues. (Well, I <em>do</em> have a deep anger at the universe for all the slights it dealt me and all the injustices in the world, but that’s a separate issue.)</p>
<p>Months ago, I saw <a target="_blank" rel="nofollow" href="http://thinktostart.com/analyze-face-emotions-r/">this tutorial</a> for using <a target="_blank" rel="nofollow" href="https://www.microsoft.com/cognitive-services/en-us/emotion-api">Microsoft’s Emotion’s API</a> for detecting emotions in photos, using R. I thought the idea would be fun, and today I decided to see what the app would say about my drawing.</p>
<p>Here’s the result (using a lot of the code from the aforementioned tutorial; note that you will have to <a target="_blank" rel="nofollow" href="https://www.microsoft.com/cognitive-services/en-us/emotion-api">create an account and get a key to use the API</a>):</p>
<pre><code>library(&quot;httr&quot;)
library(&quot;XML&quot;)
library(&quot;stringr&quot;)
library(&quot;ggplot2&quot;)
library(&quot;reshape2&quot;)

# Define image source
img.url = 'https://ntguardian.files.wordpress.com/2016/11/selfportrait.jpg'

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

# 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'
))
</code></pre>
<pre>## Response [https://api.projectoxford.ai/emotion/v1.0/recognize]
##   Date: 2016-11-13 22:41
##   Status: 200
##   Content-Type: application/json; charset=utf-8
##   Size: 264 B
</pre>
<pre><code># Request results from face analysis
(SelfPortrait = httr::content(faceEMO)[[1]])
</code></pre>
<pre>## $faceRectangle
## $faceRectangle$height
## [1] 530
## 
## $faceRectangle$left
## [1] 584
## 
## $faceRectangle$top
## [1] 549
## 
## $faceRectangle$width
## [1] 530
## 
## 
## $scores
## $scores$anger
## [1] 0.004670194
## 
## $scores$contempt
## [1] 8.175469e-05
## 
## $scores$disgust
## [1] 5.616587e-05
## 
## $scores$fear
## [1] 0.0008795662
## 
## $scores$happiness
## [1] 3.147202e-05
## 
## $scores$neutral
## [1] 0.9871294
## 
## $scores$sadness
## [1] 0.003053426
## 
## $scores$surprise
## [1] 0.00409801
</pre>
<pre><code># Define results in data frame
o&lt;-melt(as.data.frame(SelfPortrait$scores))
</code></pre>
<pre>## Using  as id variables
</pre>
<pre><code>names(o) &lt;- c(&quot;Emotion&quot;, &quot;Level&quot;)
o
</code></pre>
<pre>##     Emotion        Level
## 1     anger 4.670194e-03
## 2  contempt 8.175469e-05
## 3   disgust 5.616587e-05
## 4      fear 8.795662e-04
## 5 happiness 3.147202e-05
## 6   neutral 9.871294e-01
## 7   sadness 3.053426e-03
## 8  surprise 4.098010e-03
</pre>
<pre><code># Make plot
ggplot(data=o, aes(x=Emotion, y=Level, fill = Emotion)) +
    geom_bar(stat=&quot;identity&quot;) +
    scale_fill_brewer(palette = &quot;Set3&quot;) +
    ggtitle(&quot;Detected Emotions in Self Portrait&quot;) +
    theme_bw()
</code></pre>
<p><img src="https://i0.wp.com/ntguardian.files.wordpress.com/2016/11/emotion_self_portrait.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The API successfully detected the region of the picture where my face is located, and I’m confident it provides a reasonable analysis of the emotions seen. It appears there is only one emotion in the drawing that Microsoft detects: neutral. Lots and lots of neutral. I will also admit that there is an intensity in my expression that the API probably does not capture or quantify, and that intensity coupled with the shere “neutrality” of my expression perhaps is perceived as anger.</p>
<p>Will that end the discussion? Probably not. Computers see what they see, but what matters in the end is what people perceive, not computers; it’s to them emotions matter. Nevertheless, it’s nice to get some vindication from Microsoft; I was <em>not</em> angry!</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ntguardian.wordpress.com/2028/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/ntguardian.wordpress.com/2028/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></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="https://www.r-bloggers.com/using-microsofts-emotion-api-to-settle-an-old-argument/"></div></div><div><a data-url="https://www.r-bloggers.com/using-microsofts-emotion-api-to-settle-an-old-argument/" data-counturl="https://www.r-bloggers.com/using-microsofts-emotion-api-to-settle-an-old-argument/" data-count="vertical" data-text="Using Microsoft’s Emotion API to Settle an Old Argument" 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="https://ntguardian.wordpress.com/2016/11/14/using-microsofts-emotion-api-r-self-portrait/"> R – Curtis Miller's Personal Website</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://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="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/40YK-vOSeRQ"></a></div>