• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

  人脸提供关于情绪的各种信息。微软于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  

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

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
R语言图表基础发布时间:2022-07-18
下一篇:
【R语言】R是现在最好的数据科学语言吗?发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap