竞赛保研 基于深度学习的人脸表情识别

news2024/10/7 12:20:09

文章目录

  • 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 cv2
    import sys
    import json
    import numpy as np
    from keras.models import model_from_json


    emotions = ['angry', 'fear', 'happy', 'sad', 'surprise', 'neutral']
    cascPath = sys.argv[1]
    
    faceCascade = cv2.CascadeClassifier(cascPath)
    noseCascade = cv2.CascadeClassifier(cascPath)


    # load json and create model arch
    json_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 model
    model.load_weights('model.h5')
    
    # overlay meme face
    def 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 face
        resized_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-frame
        ret, 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 faces
        for (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 filename
            meme = 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.shape
            orig_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 nose
                mustacheWidth =  20 * nw
                mustacheHeight = mustacheWidth * origMustacheHeight / origMustacheWidth
    
                # Center the mustache on the bottom of the nose
                x1 = nx - (mustacheWidth/4)
                x2 = nx + nw + (mustacheWidth/4)
                y1 = ny + nh - (mustacheHeight/2)
                y2 = ny + nh + (mustacheHeight/2)
    
                # Check for clipping
                if x1 < 0:
                    x1 = 0
                if y1 < 0:
                    y1 = 0
                if x2 > w:
                    x2 = w
                if y2 > h:
                    y2 = h


                # Re-calculate the width and height of the mustache image
                mustacheWidth = (x2 - x1)
                mustacheHeight = (y2 - y1)
    
                # Re-size the original image and the masks to the mustache sizes
                # calcualted above
                mustache = 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 image
                roi = 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 is
                roi_fg = cv2.bitwise_and(mustache,mustache,mask = mask)
    
                # join the roi_bg and roi_fg
                dst = cv2.add(roi_bg,roi_fg)
    
                # place the joined image, saved to dst back over the original image
                roi_color[y1:y2, x1:x2] = dst
    
                break
    
        #     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 frame
        cv2.imshow('Video', frame)
    
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    # When everything is done, release the capture
    video_capture.release()
    cv2.destroyAllWindows()



4 最后

🧿 更多资料, 项目分享:

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

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

结构型设计模式——适配器模式

适配器模式 这个更加好理解&#xff0c;就是做适配功能的类&#xff0c;例如&#xff0c;现在手机没有了圆形耳机接口&#xff0c;只有Type-C接口&#xff0c;因此你如果还想要使用圆形耳机的话需要买个圆形接口转Type-C的转换器&#xff08;适配器&#xff09;&#xff0c;这…

一分钟带你了解深度学习算法

深度学习是一种受到生物学启发的机器学习方法&#xff0c;其目标是通过构建多层神经网络来模拟人脑的工作原理。它在过去几十年来取得了巨大的进展&#xff0c;并在图像识别、语音识别、自然语言处理等领域取得了突破性的成果。 深度学习的核心思想是模仿人脑的神经网络。人脑中…

HarmonyOS应用开发学习笔记 Want概述Ability跳转

一、Want的定义与用途 Want是对象间信息传递的载体&#xff0c;可以用于应用组件间的信息传递。其使用场景之一是作为startAbility()的参数&#xff0c;包含了指定的启动目标以及启动时需携带的相关数据&#xff0c;如bundleName和abilityName字段分别指明目标Ability所在应用…

vue element ui table表格--实现列的显示与隐藏

前言 实现效果 提示&#xff1a;代码段太简单就不解释了&#xff0c;自己看代码自己更改&#xff0c;下面代码直接无脑复制更改就行 一、实现代码&#xff1f; <template><div id"app"><el-table :data"tableData" border style"w…

Spring 见解 7 基于注解的AOP控制事务

8.基于注解的AOP控制事务 8.1.拷贝上一章代码 8.2.applicationContext.xml <!-- 开启spring对注解事务的支持 --> <tx:annotation-driven transaction-manager"transactionManager"/> 8.3.service Service Transactional(readOnlytrue,propagation Pr…

学生用台灯对眼睛好吗?五款考研党台灯推荐

对于一名深耕家电多年的测评师&#xff0c;对于各品牌的台灯产品测评做了不少&#xff0c;本期我将给大家带来一期超全的热门护眼台灯深度测评。 据世界卫生组织最新研究报告称&#xff0c;我国近视患者人数多达6亿左右&#xff0c;几乎占据了中国总人口数量的一半&#xff0c;…

进程管理及计划任务

一、定义 程序&#xff1a;硬盘上躺着。执行特定任务的一串代码 进程&#xff1a;加载到内存中运行。进程是程序的副本&#xff0c;进程是有生命周期 进程的控制&#xff1a;每开启一个进程会消耗相应的硬件资源&#xff0c;内存&#xff0c;CPU&#xff0c;磁盘io 内存不足&am…

MongoDB高级集群架构设计

两地三中心集群架构设计 容灾级别 RPO & RTO RPO&#xff08;Recovery Point Objective&#xff09;&#xff1a;即数据恢复点目标&#xff0c;主要指的是业务系统所能容忍的数据丢失量。RTO&#xff08;Recovery Time Objective&#xff09;&#xff1a;即恢复时间目标&…

余震强度预测能力升级,Nature 刊文认证基于神经网络的模型性能优于传统模型

作者&#xff1a;李宝珠 编辑&#xff1a;李玮栋、xixi&#xff0c;三羊 地震的发生涉及诸多变量&#xff0c;「预测」存在挑战&#xff0c;但余震发生次数及强度的预测已取得重大进展。 2023 年 12 月 18 日 23 时 59 分&#xff0c;甘肃省临夏州积石山县发生 6.2 级地震&…

DQL-条件查询

1.语法 SELECT 字段列表 FROM 表名 WHERE 条件列表; 2.条件

RDD算子——Action 操作

reduce reduce 和 reduceByKey 有什么区别&#xff1a; reduce 是一个 Action 算子&#xff0c;reduceByKey 是一个转换算子 假设一个 RDD 里面有一万条数据&#xff0c;大部分 Key 是相同的&#xff0c;有十个不同的 Key。 rdd.reduceByKey 生成 10 条数据 而rdd.reduce 生成…

VScode/Xshell连接学校服务器

vscode连学校服务器 1.连接atrust VPN2.Xshell连接服务器2.1创建一个自己的用户 3.xftp传文件4.vscode连接服务器4.1下载remote-ssh4.2连接服务器4.3激活conda环境4.4运行代码 5. pytorch版本不兼容解决方案 1.连接atrust VPN 如果是使用的是校园网&#xff0c;可以不连接 2…

已删除数据恢复,4个简单有效方法分享!

“我前段时间不小心将电脑里重要的几个文件删除了&#xff0c;今天查找这些文件时才发现我的数据都没有了&#xff0c;这可怎么办呢&#xff1f;还有恢复的机会吗&#xff1f;” 很多用户在使用电脑时&#xff0c;会选择直接将重要的文件保存在电脑上&#xff0c;这就不可避免在…

Android 架构 - 模块化

参考文章 谷歌官方指南 一、概念 将大型、复杂问题拆解成一个个小的、简单问题&#xff0c;从而可以做到各个击破。模块化简单讲就是把多功能高耦合的代码逻辑拆散成多个功能单一职责明确的模块。模块指 Android 项目中的 module&#xff0c;通常会包含 Gradle 构建脚本、源代…

TS 36.213 V12.0.0-PDSCH相关过程(1)-收到PDSCH的UE过程

本文的内容主要涉及TS 36.213&#xff0c;版本是C00&#xff0c;也就是V12.0.0。

基础数据结构第九期 堆(数组+STL)

前言 堆是一种重要的数据结构&#xff0c;因此应该熟练掌握。 一、堆的基本概念 堆的基本&#xff1a; 堆的结构实际上是一棵完全二叉树&#xff0c;堆可以分为大根堆和小根堆 大根堆&#xff1a; 小根堆&#xff1a; 堆的储存&#xff1a; 若节点小标为i&#xff0c;则左子…

虚拟机VMware安装Linux

关于安装&#xff0c;安装版本是CentOS 7&#xff0c;选择最小安装即可 第一步&#xff1a;选择创建新的虚拟机 第二步&#xff1a;默认典型&#xff0c;点击下一步 第三步&#xff1a;选择稍后安装操作系统 第四步&#xff1a;选择Linux和版本 第五步&#xff1a;输入虚拟机名…

鸿蒙Ability开发-Stage模型下Ability的创建和使用

创建Ability和Page页面 创建两个Ability&#xff1a;EntryAbility&#xff0c;DetailsAbility&#xff0c;其中EntryAbility是由工程默认创建的&#xff0c;这里我们只讲如何创建DetailsAbility。 使用DevEco Studio&#xff0c;选中对应的模块&#xff0c;单击鼠标右键&…

蓝桥杯省赛无忧 竞赛常用库函数 课件7 二分查找

01 二分查找的前提 02 binary_search函数 #include<bits/stdc.h> using namespace std; int main(){vector<int> numbers{1,3,5,7,9};int target 5;//使用binary_search 查找目标元素bool found binary_search(numbers.begin(),numbers.end(),target);if(found){…

利用矩阵特征值解决微分方程【1】

目录 一. 特征值介绍 二. 单变量常微分方程 三. 利用矩阵解决微分方程问题 四. 小结 4.1 矩阵论 4.2 特征值与特征向量内涵 4.3 应用 一. 特征值介绍 线性代数有两大基础问题&#xff1a; 如果A为对角阵的话&#xff0c;那么问题就很好解决。需要注意的是&#xff0c;矩…