python情绪实时检测

一、实验环境

1.我这里下载的是2020版本

2.下载Pycharm地址

由于涉及到侵权问题,此处省略,需要的私聊我。

不会安装的私聊我,发你教程。

二、实验步骤

1.新建一个空白项目并导入文件包(文件包上传到资源)

2.下载文件包直接导入项目中,实验代码

import cv2 as cv
import imutils
import numpy as np
import keras

# define all constants
model_path = "./model/CNN-Emotion-Model"
face_path = cv.haarcascades + 'haarcascade_frontalface_alt.xml'
emotion_label = ['Anger', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']

# create video capture object
cap = cv.VideoCapture(0)
# load face detection model
face_model = cv.CascadeClassifier()
if not face_model.load(face_path):
    print('load face model failed')
    exit(0)

# load trained emotion model
emotion_model = keras.models.load_model(model_path)

def drawFace(frame, frame_grey):
    faces = face_model.detectMultiScale(frame, minSize=(50,50))
    emotion = np.array([[0,0,0,0,0,0,0]])
    for a,b,c,d in faces:
        # print('%s %s %s %s' % (a,b,c,d))
        face = frame_grey[b:b+d,a:a+c]
        face = cv.resize(face, (48, 48))

        face = np.array(face)
        face = face.reshape(1, 48, 48, 1)

        emotion = emotion_model.predict(face/255)
        text = emotion_label[np.argmax(emotion)]

        cv.rectangle(frame, (a,b), (a+c, b+d), (0, 0, 255), 1)
        cv.putText(frame, text, (a,b-10),cv.FONT_HERSHEY_COMPLEX, 0.8, (0,0,255), 2)
    return emotion

这是部分代码,全部代码参考我的资源里面。 

 3.运行实验结果

实验成功,头像进行了打码。

评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值