竞赛选题 基于深度学习的人脸表情识别

2023-09-18 13:57:57

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

更多推荐

软件设计师笔记系列(四)

😀前言随着技术的快速发展,软件已经成为我们日常生活中不可或缺的一部分。从智能手机应用到大型企业系统,软件都在为我们提供便利、增强效率和创造价值。然而,随之而来的是对软件质量的日益增长的关注。软件的质量不仅关乎其功能性和性能,还涉及其可靠性、使用性、可维护性和可移植性。为了更好地理解和评估软件的质量,我们需要一个结构化

day49:QT day2,信号与槽、对话框

一、完善登录框点击登录按钮后,判断账号(admin)和密码(123456)是否一致,如果匹配失败,则弹出错误对话框,文本内容“账号密码不匹配,是否重新登录”,给定两个按钮ok和cancel,点击ok后,会清除密码框中的内容,继续进行登录;如果点击cancel按钮,则关闭界面。如果账号和密码匹配,则弹出信息对话框,给出提

比特币 ZK 赏金系列:第 1 部分——支付解密密钥

以前,我们使用零知识赏金(ZKB)来支付比特币上的数独解决方案。在本系列中,我们将使用ZKB来解决范围更广的更实际的问题。在第1部分中,我们应用ZKB来支付解密密钥。假设Alice使用对称密钥K加密她的文件。为了安全起见,她联系了在线备份服务Bob,以保留K的副本。她不想让Bob知道K,所以她使用门限秘密共享方案将K分

FPGA 图像缩放 千兆网 UDP 网络视频传输,基于88E1518 PHY实现,提供工程和QT上位机源码加技术支持

目录1、前言版本更新说明免责声明2、相关方案推荐UDP视频传输--无缩放FPGA图像缩放方案我这里已有的以太网方案3、设计思路框架视频源选择IT6802解码芯片配置及采集动态彩条跨时钟FIFO图像缩放模块详解设计框图代码框图2种插值算法的整合与选择UDP协议栈UDP视频数据组包UDP协议栈数据发送UDP协议栈数据缓冲I

【华为OD机试python】模拟消息队列【2023 B卷|100分】

【华为OD机试】-真题!!点这里!!【华为OD机试】真题考点分类!!点这里!!题目描述让我们来模拟一个消息队列的运作,有一个发布者和若干消费者,发布者会在给定的时刻向消息队列发送消息若此时消息队列有消费者订阅,这个消息会被发送到订阅的消费者中优先级最高(输入中消费者按优先级升序排列)的一个。若此时没有订阅的消费者,该消

【数据链路层】网络基础 -- MAC帧协议与ARP协议

数据链路层认识以太网以太网帧格式(MAC帧)认识MAC地址对比理解MAC地址和IP地址认识MTUMTU对IP协议的影响MTU对UDP协议的影响MTU对于TCP协议的影响再谈局域网转发原理(基于协议)ARP协议ARP协议的作用ARP协议的工作流程ARP数据报的格式数据链路层用于两个设备(同一种数据链路节点)之间进行传递在

抖音短视频矩阵系统搭建

企业在进行短视频矩阵运营时,搭建一个矩阵号是非常必要的。矩阵号可以绑定多个不同平台的账号,批量制作和定时发布短视频,提高企业的曝光量和粉丝互动。但是,如何搭建一个有效的短视频矩阵号呢?以下是几个关键步骤。一、准备做矩阵的账号企业可以注册新账号,也可以使用现有的账号。在选择账号时,需要根据矩阵号的不同分工来做出选择。例如

Android Camera2获取摄像头的视场角(FOV)信息

一、概念FOV(FieldofView)是一个用于描述视野范围的术语。它通常用于计算设备(如摄像机、虚拟现实头显或眼睛)所能捕捉到的可见区域。水平FOV(HorizontalFOV):描述视野在水平方向上的范围,通常以度(°)或弧度(rad)为单位。垂直FOV(VerticalFOV):描述视野在垂直方向上的范围,同样

【Verilog教程】3.2 Verilog 时延

关键词:时延,惯性时延连续赋值延时语句中的延时,用于控制任意操作数发生变化到语句左端赋予新值之间的时间延时。时延一般是不可综合的。寄存器的时延也是可以控制的,这部分在时序控制里加以说明。连续赋值时延一般可分为普通赋值时延、隐式时延、声明时延。下面3个例子实现的功能是等效的,分别对应3种不同连续赋值时延的写法。//普通时

服务器资源监控工具Nmon工具搭建教程

nmon是IBM公司推出的一款免费性能监控工具,可以时时监控服务器资源,还可以定时监控服务器资源,并生成数据文件,记录服务器的资源消耗情况操作步骤:下载地址:https://nmon.sourceforge.net/pmwiki.php?n=Site.Download1,通过ssh工具连接服务器2,进入usr目录下创建

IP模块组装网络包及转发网络包链路

目录引言网络包网络包的组成​编辑网络包的转发转发设备大致流程​编辑ip模块发送网络包添加网络包的头部控制信息ip头部中添加发送方ip地址路由表查找规则​编辑添加协议号添加mac头部​编辑arp协议转换ip地址为mac地址​编辑arp缓存arp缓存失效​编辑ip模块对应的发送接受发送接受职责界定引言之前协议栈系列的文章讲

热文推荐