人脸实时情绪与性别识别

最近弄一个情绪识别与性别识别的东东。
opencv + keras
opencv用于人脸检测
keras用于训练出识别模型

数据集用于kaggle的(FER2013)

CNN进行训练。

代码如下:

import cv2
import sys
import json
import time
import numpy as np
from keras.models import model_from_json
from keras.models import load_model


emotion_labels = ['angry', 'fear', 'happy', 'sad', 'surprise', 'neutral']
gender_labels = {0: 'womam', 1: 'man'}
#cascPath = sys.argv[1]

faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

# 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)

gender_classifier = load_model('model/gender/simple_CNN.81-0.96.hdf5')

# load weights into new model
model.load_weights('model.h5')
def overlay_memeface(probs):

        emotion = emotion_labels[np.argmax(probs)]
        return emotion


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]

def predict_gender(face_image_gray):
    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, 48, 48, 3)
    gender_label_arg = np.argmax(gender_classifier.predict(image))
    gender = gender_labels[gender_label_arg]
    return gender

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)
    #img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        img_gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        #flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    )

    emotions = []
    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:

        face_image_gray = img_gray[y:y+h, x:x+w]
        face_image = frame[y:y+h, x:x+w]
        print(face_image_gray.shape)
        #face = np.expand_dims(face_image_gray, 0)
        #face = face / 255.0
        #gender_label_arg = np.argmax(gender_classifier.predict(face))
       # gender = gender_labels[gender_label_arg]
        gender = predict_gender(face_image)
        emotion = overlay_memeface(predict_emotion(face_image_gray))
        print(predict_emotion(face_image_gray))
        print(emotion)
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

        #angry, fear, happy, sad, surprise, neutral = predict_emotion(face_image_gray)
        #with open('emotion.txt', 'a') as f:
            #f.write('{},{},{},{},{},{},{}\n'.format(time.time(), angry, fear, happy, sad, surprise, neutral))
        cv2.putText(frame, gender, (x, y - 30), cv2.FONT_HERSHEY_SIMPLEX, .7, (0, 255, 0), 1, cv2.LINE_AA)
        cv2.putText(frame, emotion, (x + 90, y - 30), cv2.FONT_HERSHEY_SIMPLEX, .7, (255, 0, 0), 1, cv2.LINE_AA)

    # 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()

结果如下:

这里写图片描述

长得丑请别喷,,,,,,

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值