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

2024-03-06 11:44

本文主要是介绍挑战杯 基于深度学习的人脸表情识别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 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.chinasem.cn/article/779957

相关文章

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操

五大特性引领创新! 深度操作系统 deepin 25 Preview预览版发布

《五大特性引领创新!深度操作系统deepin25Preview预览版发布》今日,深度操作系统正式推出deepin25Preview版本,该版本集成了五大核心特性:磐石系统、全新DDE、Tr... 深度操作系统今日发布了 deepin 25 Preview,新版本囊括五大特性:磐石系统、全新 DDE、Tree

Node.js 中 http 模块的深度剖析与实战应用小结

《Node.js中http模块的深度剖析与实战应用小结》本文详细介绍了Node.js中的http模块,从创建HTTP服务器、处理请求与响应,到获取请求参数,每个环节都通过代码示例进行解析,旨在帮... 目录Node.js 中 http 模块的深度剖析与实战应用一、引言二、创建 HTTP 服务器:基石搭建(一

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;