互联网加竞赛 基于深度学习的人脸表情识别

文章目录

  • 0 前言
  • 1 技术介绍
    • 1.1 技术概括
    • 1.2 目前表情识别实现技术
  • 2 实现效果
  • 3 深度学习表情识别实现过程
    • 3.1 网络架构
    • 3.2 数据
    • 3.3 实现流程
    • 3.4 部分实现代码
  • 4 最后

0 前言

🔥 优质竞赛项目系列,今天要分享的是

基于深度学习的人脸表情识别

该项目较为新颖,适合作为竞赛课题方向,学长非常推荐!

🧿 更多资料, 项目分享:

https://gitee.com/dancheng-senior/postgraduate


1 技术介绍

1.1 技术概括

面部表情识别技术源于1971年心理学家Ekman和Friesen的一项研究,他们提出人类主要有六种基本情感,每种情感以唯一的表情来反映当时的心理活动,这六种情感分别是愤怒(anger)、高兴(happiness)、悲伤
(sadness)、惊讶(surprise)、厌恶(disgust)和恐惧(fear)。

尽管人类的情感维度和表情复杂度远不是数字6可以量化的,但总体而言,这6种也差不多够描述了。

在这里插入图片描述

1.2 目前表情识别实现技术

在这里插入图片描述
在这里插入图片描述

2 实现效果

废话不多说,先上实现效果

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

3 深度学习表情识别实现过程

3.1 网络架构

在这里插入图片描述
面部表情识别CNN架构(改编自 埃因霍芬理工大学PARsE结构图)

其中,通过卷积操作来创建特征映射,将卷积核挨个与图像进行卷积,从而创建一组要素图,并在其后通过池化(pooling)操作来降维。

在这里插入图片描述

3.2 数据

主要来源于kaggle比赛,下载地址。
有七种表情类别: (0=Angry, 1=Disgust, 2=Fear, 3=Happy, 4=Sad, 5=Surprise, 6=Neutral).
数据是48x48 灰度图,格式比较奇葩。
第一列是情绪分类,第二列是图像的numpy,第三列是train or test。

在这里插入图片描述

3.3 实现流程

在这里插入图片描述

3.4 部分实现代码

import cv2import sysimport jsonimport numpy as npfrom keras.models import model_from_jsonemotions = ['angry', 'fear', 'happy', 'sad', 'surprise', 'neutral']cascPath = sys.argv[1]faceCascade = cv2.CascadeClassifier(cascPath)noseCascade = cv2.CascadeClassifier(cascPath)# load json and create model archjson_file = open('model.json','r')loaded_model_json = json_file.read()json_file.close()model = model_from_json(loaded_model_json)# load weights into new modelmodel.load_weights('model.h5')# overlay meme facedef overlay_memeface(probs):if max(probs) > 0.8:emotion = emotions[np.argmax(probs)]return 'meme_faces/{}-{}.png'.format(emotion, emotion)else:index1, index2 = np.argsort(probs)[::-1][:2]emotion1 = emotions[index1]emotion2 = emotions[index2]return 'meme_faces/{}-{}.png'.format(emotion1, emotion2)def predict_emotion(face_image_gray): # a single cropped faceresized_img = cv2.resize(face_image_gray, (48,48), interpolation = cv2.INTER_AREA)# cv2.imwrite(str(index)+'.png', resized_img)image = resized_img.reshape(1, 1, 48, 48)list_of_list = model.predict(image, batch_size=1, verbose=1)angry, fear, happy, sad, surprise, neutral = [prob for lst in list_of_list for prob in lst]return [angry, fear, happy, sad, surprise, neutral]video_capture = cv2.VideoCapture(0)while True:# Capture frame-by-frameret, frame = video_capture.read()img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY,1)faces = faceCascade.detectMultiScale(img_gray,scaleFactor=1.1,minNeighbors=5,minSize=(30, 30),flags=cv2.cv.CV_HAAR_SCALE_IMAGE)# Draw a rectangle around the facesfor (x, y, w, h) in faces:face_image_gray = img_gray[y:y+h, x:x+w]filename = overlay_memeface(predict_emotion(face_image_gray))print filenamememe = cv2.imread(filename,-1)# meme = (meme/256).astype('uint8')try:meme.shape[2]except:meme = meme.reshape(meme.shape[0], meme.shape[1], 1)# print meme.dtype# print meme.shapeorig_mask = meme[:,:,3]# print orig_mask.shape# memegray = cv2.cvtColor(orig_mask, cv2.COLOR_BGR2GRAY)ret1, orig_mask = cv2.threshold(orig_mask, 10, 255, cv2.THRESH_BINARY)orig_mask_inv = cv2.bitwise_not(orig_mask)meme = meme[:,:,0:3]origMustacheHeight, origMustacheWidth = meme.shape[:2]roi_gray = img_gray[y:y+h, x:x+w]roi_color = frame[y:y+h, x:x+w]# Detect a nose within the region bounded by each face (the ROI)nose = noseCascade.detectMultiScale(roi_gray)for (nx,ny,nw,nh) in nose:# Un-comment the next line for debug (draw box around the nose)#cv2.rectangle(roi_color,(nx,ny),(nx+nw,ny+nh),(255,0,0),2)# The mustache should be three times the width of the nosemustacheWidth =  20 * nwmustacheHeight = mustacheWidth * origMustacheHeight / origMustacheWidth# Center the mustache on the bottom of the nosex1 = nx - (mustacheWidth/4)x2 = nx + nw + (mustacheWidth/4)y1 = ny + nh - (mustacheHeight/2)y2 = ny + nh + (mustacheHeight/2)# Check for clippingif x1 < 0:x1 = 0if y1 < 0:y1 = 0if x2 > w:x2 = wif y2 > h:y2 = h# Re-calculate the width and height of the mustache imagemustacheWidth = (x2 - x1)mustacheHeight = (y2 - y1)# Re-size the original image and the masks to the mustache sizes# calcualted abovemustache = cv2.resize(meme, (mustacheWidth,mustacheHeight), interpolation = cv2.INTER_AREA)mask = cv2.resize(orig_mask, (mustacheWidth,mustacheHeight), interpolation = cv2.INTER_AREA)mask_inv = cv2.resize(orig_mask_inv, (mustacheWidth,mustacheHeight), interpolation = cv2.INTER_AREA)# take ROI for mustache from background equal to size of mustache imageroi = roi_color[y1:y2, x1:x2]# roi_bg contains the original image only where the mustache is not# in the region that is the size of the mustache.roi_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)# roi_fg contains the image of the mustache only where the mustache isroi_fg = cv2.bitwise_and(mustache,mustache,mask = mask)# join the roi_bg and roi_fgdst = cv2.add(roi_bg,roi_fg)# place the joined image, saved to dst back over the original imageroi_color[y1:y2, x1:x2] = dstbreak#     cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)#     angry, fear, happy, sad, surprise, neutral = predict_emotion(face_image_gray)#     text1 = 'Angry: {}     Fear: {}   Happy: {}'.format(angry, fear, happy)#     text2 = '  Sad: {} Surprise: {} Neutral: {}'.format(sad, surprise, neutral)## cv2.putText(frame, text1, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 0, 0), 3)# cv2.putText(frame, text2, (50, 150), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 0, 0), 3)# Display the resulting framecv2.imshow('Video', frame)if cv2.waitKey(1) & 0xFF == ord('q'):break# When everything is done, release the capturevideo_capture.release()cv2.destroyAllWindows()

4 最后

🧿 更多资料, 项目分享:

https://gitee.com/dancheng-senior/postgraduate

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/442479.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

StarRocks -- 基础概念(数据模型及分区分桶)

1. 数据模型 StarRocks提供四种数据模型&#xff1a; Duplicate Key, Aggregate Key, Unique Key, Primary Key 1.1 Duplicate Key 适用场景&#xff1a; 分析原始数据&#xff0c;如原始日志和原始操作记录。可以使用多种方法查询数据&#xff0c;不受预聚合方法的限制。加…

阿里十年 “帕鲁” 手把手带你 学习 ThreadLocal

阿里十年 “帕鲁” 手把手带你 学习 ThreadLocal 文章目录 阿里十年 “帕鲁” 手把手带你 学习 ThreadLocal前言目录ThreadLocal代码演示ThreadLocal的数据结构GC 之后 key 是否为 null&#xff1f;ThreadLocal.set()方法源码详解ThreadLocalMap Hash 算法ThreadLocalMap Hash …

Docker的使用方式

一、Docker概念 Docker类似于一个轻量的虚拟机。 容器和镜像是Docker中最重要的两个概念&#xff0c;镜像可以保存为tar文件&#xff0c;Dockerfile是配置文件&#xff0c;仓库保存了很多第三方已经做好的镜像。 基本指令 查找镜像 docker search nginx 拉取nginx镜像 do…

亚信安慧AntDB:AntDB-M元数据锁(十)

5.8 锁等待及通知 每个线程的锁上下文都有一个条件变量来进行锁等待。线程在没有获取锁的授权时&#xff0c;会将自己的ticket添加到锁对象的等待队列&#xff0c;并进入等待状态。等待队列的锁授予检测有3个时机&#xff1a; 1&#xff09;加锁申请阶段&#xff0c;hog,pigl…

海外云手机开辟企业跨境电商新道路

近几年&#xff0c;海外云手机为跨境电商、海外媒体引流、游戏行业等互联网领域注入了蓬勃活力。对于国内跨境电商而言&#xff0c;在亚马逊及其他平台上&#xff0c;短视频引流和社交电商营销成为最为有效的流量来源。如何通过海外云手机的助力&#xff0c;在新兴社交平台为企…

【WPF.NET开发】优化性能:图形呈现层

本文内容 图形硬件呈现层定义其他资源 呈现层为运行 WPF 应用程序的设备定义图形硬件功能和性能级别。 1、图形硬件 对呈现层级别影响最大的图形硬件功能包括&#xff1a; 视频 RAM - 图形硬件中的视频内存量决定了可用于合成图形的缓冲区大小和数量。 像素着色器 - 像素着…

数据结构—栈实现前缀表达式的计算

前缀表达式计算 过程分析 中缀表达式&#xff1a;&#xff08;1 5&#xff09;*3 > 前缀表达式&#xff1a;*153 &#xff08;可参考这篇文章&#xff1a;中缀转前缀&#xff09; 第一步&#xff1a;从右至左扫描前缀表达式&#xff08;已存放在字符数组中&#xff09;&a…

华为VRP系统简介

因为现在国内主流是华为、华三、锐捷的设备趋势&#xff0c;然后考的证书也是相关的&#xff0c;对于华为设备的一个了解也是需要的。 一、VRP概述 华为的VRP(通用路由平台)是华为公司数据通信产品的通用操作系统平台&#xff0c;作为华为公司从低端到核心的全系列路由器、以太…

《Lua程序设计》-- 学习9

迭代器和泛型for 迭代器和闭包 迭代器&#xff08;iterator&#xff09;是一种可以让我们遍历一个集合中所有元素的代码结构。在Lua语言中&#xff0c;通常使用函数表示迭代器&#xff1a;每一次调用函数时&#xff0c;函数会返回集合中的“下一个”元素。 一个闭包就是一个…

App全测试扫描漏洞工具

APP 有漏洞被测要下架&#xff0c;怎么处理&#xff1f; 如题&#xff0c;今天被问到&#xff1a;市面上有什么好的 APP 漏洞扫描工具推荐&#xff1f;我们的 APP 有漏洞&#xff0c;需要下架 APP&#xff1f; 前言 事情的经过是这样的&#xff1a; 1&#xff1a;学员公司测试…

企业网络基础架构监控工具

IT 基础架构已成为提供基本业务服务的基石&#xff0c;无论是内部管理操作还是为客户托管的应用程序服务&#xff0c;监控 IT 基础设施至关重要&#xff0c;并且已经建立起来&#xff0c;SMB IT 基础架构需要简单的网络监控工具来监控性能和报告问题。通常&#xff0c;几个 IT …

存内计算——发展史与近期成果

存内计算的概念早在上个世纪就已经被提出&#xff0c;但当时的人们寄希望于通过优化处理器设计以及工艺制程的升级&#xff0c;来获得性能和能效比的提升&#xff0c;存内计算的研究仅停留在理论阶段。随着大数据时代的到来&#xff0c;存内计算由于其结构特点以及摩尔定律的“…