打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用R语言对照片人物进行情绪分析

       人脸提供关于情绪的各种信息。微软于2015年12月推出免费服务,分析人脸,进行情绪检测。 检测到的情绪是愤怒,蔑视,厌恶,恐惧,幸福,中立,悲伤和惊喜。 这些情绪被理解为与特定的面部表情跨文化和普遍传达。

Emotion API将图像中的面部表情作为输入,并使用Face API返回图像中每个面部的一组情绪的置信度以及面部的边界框。

在R中的实现允许以结构化的方式分析人脸。 注意,必须创建一个帐户来使用Face API。

该示例引用了一个简单的示例:使用的是现任美国总统奥巴马的照片;如下



需要加载的包有: httr, XML, stringr, ggplot2.

[plain] view plain copy
  1. # 加载相关包  
  2. library("httr")#链接API  
  3. library("XML")#爬取网页数据  
  4. library("stringr")#字符串处理  
  5. library("ggplot2")#绘图使用  
  6.    
  7. # Define image source  
  8. img.url     = 'https://www.whitehouse.gov/sites/whitehouse.gov/files/images/first-family/44_barack_obama[1].jpg'  
  9.    
  10. # Define Microsoft API URL to request data  
  11. URL.emoface = 'https://api.projectoxford.ai/emotion/v1.0/recognize'  
  12.    
  13. # Define access key (access key is available via: https://www.microsoft.com/cognitive-services/en-us/emotion-api)  
  14. emotionKEY = 'XXXX' # 在此处输入你获取的key  
  15.    
  16. # Define image  
  17. mybody = list(url = img.url)  
  18.    
  19. # Request data from Microsoft  
  20. faceEMO = POST(  
  21.   url = URL.emoface,  
  22.   content_type('application/json'), add_headers(.headers = c('Ocp-Apim-Subscription-Key' = emotionKEY)),  
  23.   body = mybody,  
  24.   encode = 'json'  
  25. )  
  26.    
  27. # Show request results (if Status=200, request is okay)  
  28. faceEMO  
  29.    
  30. # Reuqest results from face analysis  
  31. Obama = httr::content(faceEMO)[[1]]  
  32. Obama  
  33. # Define results in data frame  
  34. o<-as.data.frame(as.matrix(Obama$scores))  
  35.    
  36. # Make some transformation  
  37. o$V1 <- lapply(strsplit(as.character(o$V1 ), "e"), "[", 1)  
  38. o$V1<-as.numeric(o$V1)  
  39. colnames(o)[1] <- "Level"  
  40.    
  41. # Define names  
  42. o$Emotion<- rownames(o)  
  43.    
  44. # Make plot  
  45. ggplot(data=o, aes(x=Emotion, y=Level)) +  
  46.   geom_bar(stat="identity")  
下面就是对这张照片的情感分析图。



[plain] view plain copy
  1. #人脸检测  
  2. #####################################################################  
  3. # Define image source  
  4. img.url = 'https://www.whitehouse.gov/sites/whitehouse.gov/files/images/first-family/44_barack_obama[1].jpg'  
  5.    
  6. # Define Microsoft API URL to request data  
  7. faceURL = "https://api.projectoxford.ai/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=true&returnFaceAttributes=age"  
  8.    
  9. # Define access key (access key is available via: https://www.microsoft.com/cognitive-services/en-us/face-api)  
  10. faceKEY = 'a868182e859c4458953f69dab084f5e8'  
  11.    
  12. # Define image  
  13. mybody = list(url = img.url)  
  14.    
  15. # Request data from Microsoft  
  16. faceResponse = POST(  
  17.   url = faceURL,   
  18.   content_type('application/json'), add_headers(.headers = c('Ocp-Apim-Subscription-Key' = faceKEY)),  
  19.   body = mybody,  
  20.   encode = 'json'  
  21. )  
  22.    
  23. # Show request results (if Status=200, request is okay)  
  24. faceResponse  
  25.    
  26. # Reuqest results from face analysis  
  27. ObamaR = httr::content(faceResponse)[[1]]  
  28.    
  29. # Define results in data frame  
  30. OR<-as.data.frame(as.matrix(ObamaR$faceLandmarks))  
  31.    
  32. # Make some transformation to data frame  
  33. OR$V2 <- lapply(strsplit(as.character(OR$V1), "\\="), "[", 2)  
  34. OR$V2 <- lapply(strsplit(as.character(OR$V2), "\\,"), "[", 1)  
  35. colnames(OR)[2] <- "X"  
  36. OR$X<-as.numeric(OR$X)  
  37.    
  38. OR$V3 <- lapply(strsplit(as.character(OR$V1), "\\y = "), "[", 2)  
  39. OR$V3 <- lapply(strsplit(as.character(OR$V3), "\\)"), "[", 1)  
  40. colnames(OR)[3] <- "Y"  
  41. OR$Y<-as.numeric(OR$Y)  
  42.    
  43. OR$V1<-NULL  
  44. OR  
结果如下:

 是他脸部的特征值:

[plain] view plain copy
  1.                         X     Y  
  2. pupilLeft           475.4 158.6  
  3. pupilRight          590.6 157.3  
  4. noseTip             534.4 227.7  
  5. mouthLeft           460.8 273.7  
  6. mouthRight          603.6 268.2  
  7. eyebrowLeftOuter    425.2 154.8  
  8. eyebrowLeftInner    508.4 142.3  
  9. eyeLeftOuter        458.6 162.6  
  10. eyeLeftTop          473.6 153.8  
  11. eyeLeftBottom       475.9 164.9  
  12. eyeLeftInner        492.8 162.0  
  13. eyebrowRightInner   552.3 141.4  
  14. eyebrowRightOuter   636.0 156.2  
  15. eyeRightInner       571.7 159.9  
  16. eyeRightTop         588.1 152.5  
  17. eyeRightBottom      587.4 163.9  
  18. eyeRightOuter       605.5 161.5  
  19. noseRootLeft        511.2 163.4  
  20. noseRootRight       551.2 163.0  
  21. noseLeftAlarTop     503.1 204.6  
  22. noseRightAlarTop    559.2 201.6  
  23. noseLeftAlarOutTip  485.3 226.9  
  24. noseRightAlarOutTip 580.5 224.1  
  25. upperLipTop         530.9 264.3  
  26. upperLipBottom      532.1 272.5  
  27. underLipTop         530.3 305.1  
  28. underLipBottom      532.5 318.6  

说明:本人对原博客进行翻译的时候,在某些地方进行了一定修改,与原文并不完全相同。

注转载请注明原文链接:http://blog.csdn.net/wzgl__wh/article/details/52904069

原文链接:点击打开链接

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
R网页抓取数据
Linux下HOOK动态链接库中API的方法
iOS解析新浪微博的@##以及URL链接并展示
PHP 微信支付
生信编程直播课程优秀学员作业展示1
微服务,ApiGateway 与 Kong
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服