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

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

相关文章

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

Python 中的异步与同步深度解析(实践记录)

《Python中的异步与同步深度解析(实践记录)》在Python编程世界里,异步和同步的概念是理解程序执行流程和性能优化的关键,这篇文章将带你深入了解它们的差异,以及阻塞和非阻塞的特性,同时通过实际... 目录python中的异步与同步:深度解析与实践异步与同步的定义异步同步阻塞与非阻塞的概念阻塞非阻塞同步

使用PyTorch实现手写数字识别功能

《使用PyTorch实现手写数字识别功能》在人工智能的世界里,计算机视觉是最具魅力的领域之一,通过PyTorch这一强大的深度学习框架,我们将在经典的MNIST数据集上,见证一个神经网络从零开始学会识... 目录当计算机学会“看”数字搭建开发环境MNIST数据集解析1. 认识手写数字数据库2. 数据预处理的

Redis中高并发读写性能的深度解析与优化

《Redis中高并发读写性能的深度解析与优化》Redis作为一款高性能的内存数据库,广泛应用于缓存、消息队列、实时统计等场景,本文将深入探讨Redis的读写并发能力,感兴趣的小伙伴可以了解下... 目录引言一、Redis 并发能力概述1.1 Redis 的读写性能1.2 影响 Redis 并发能力的因素二、

Pytorch微调BERT实现命名实体识别

《Pytorch微调BERT实现命名实体识别》命名实体识别(NER)是自然语言处理(NLP)中的一项关键任务,它涉及识别和分类文本中的关键实体,BERT是一种强大的语言表示模型,在各种NLP任务中显著... 目录环境准备加载预训练BERT模型准备数据集标记与对齐微调 BERT最后总结环境准备在继续之前,确

最新Spring Security实战教程之表单登录定制到处理逻辑的深度改造(最新推荐)

《最新SpringSecurity实战教程之表单登录定制到处理逻辑的深度改造(最新推荐)》本章节介绍了如何通过SpringSecurity实现从配置自定义登录页面、表单登录处理逻辑的配置,并简单模拟... 目录前言改造准备开始登录页改造自定义用户名密码登陆成功失败跳转问题自定义登出前后端分离适配方案结语前言

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

讯飞webapi语音识别接口调用示例代码(python)

《讯飞webapi语音识别接口调用示例代码(python)》:本文主要介绍如何使用Python3调用讯飞WebAPI语音识别接口,重点解决了在处理语音识别结果时判断是否为最后一帧的问题,通过运行代... 目录前言一、环境二、引入库三、代码实例四、运行结果五、总结前言基于python3 讯飞webAPI语音

Redis 内存淘汰策略深度解析(最新推荐)

《Redis内存淘汰策略深度解析(最新推荐)》本文详细探讨了Redis的内存淘汰策略、实现原理、适用场景及最佳实践,介绍了八种内存淘汰策略,包括noeviction、LRU、LFU、TTL、Rand... 目录一、 内存淘汰策略概述二、内存淘汰策略详解2.1 ​noeviction(不淘汰)​2.2 ​LR

使用Python开发一个图像标注与OCR识别工具

《使用Python开发一个图像标注与OCR识别工具》:本文主要介绍一个使用Python开发的工具,允许用户在图像上进行矩形标注,使用OCR对标注区域进行文本识别,并将结果保存为Excel文件,感兴... 目录项目简介1. 图像加载与显示2. 矩形标注3. OCR识别4. 标注的保存与加载5. 裁剪与重置图像