R语言:用微软的深度学习理解图片情感

本文系转载,原博客:http://blog.csdn.net/wzgl__wh/article/details/52904069

使用微软(https://www.microsoft.com/cognitive-services/en-US/subscriptions)的Emotion 来理解一张图片的人物表情,下面直接上代码:

> library("httr")#链接API
> library("XML")#爬取网页数据
> library("stringr")#字符串处理
> library("ggplot2")#绘图使用
> img.url     = 'https://www.whitehouse.gov/sites/whitehouse.gov/files/images/first-family/44_barack_obama[1].jpg'
> URL.emoface = 'https://api.projectoxford.ai/emotion/v1.0/recognize'
> emotionKEY = 'b72720c5c1ed4b40a25d00e3db325c32' # 在此处输入你获取的key
> mybody = list(url = img.url)
> faceEMO = POST(  
+   url = URL.emoface,  
+   content_type('application/json'), add_headers(.headers = c('Ocp-Apim-Subscription-Key' = emotionKEY)),  
+   body = mybody,  
+   encode = 'json'  
+ )
> faceEMO
Response [https://api.projectoxford.ai/emotion/v1.0/recognize]
  Date: 2017-01-03 04:23
  Status: 200
  Content-Type: application/json; charset=utf-8
  Size: 259 B

> obama = httr::content(faceEMO)[[1]]
> obama
$faceRectangle
$faceRectangle$height
[1] 269

$faceRectangle$left
[1] 399

$faceRectangle$top
[1] 80

$faceRectangle$width
[1] 269


$scores
$scores$anger
[1] 9.530491e-10

$scores$contempt
[1] 7.204279e-13

$scores$disgust
[1] 2.932419e-09

$scores$fear
[1] 1.770692e-13

$scores$happiness
[1] 1

$scores$neutral
[1] 1.277603e-11

$scores$sadness
[1] 1.228429e-12

$scores$surprise
[1] 1.559173e-10


> o<-as.data.frame(as.matrix(obama$scores))
> o$V1 <- lapply(strsplit(as.character(o$V1 ), "e"), "[", 1)
> o$V1<-as.numeric(o$V1)
> colnames(o)[1] <- "Level"
> o$Emotion<- rownames(o)
> ggplot(data=o, aes(x=Emotion, y=Level)) +  
+   geom_bar(stat="identity")

这里写图片描述

欧巴马的图:
这里写图片描述

这张图显示他在微笑,但是我们的结果是生气的,看来深度学习还是有改进的空间的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值