人臉實時情緒與性別識別

最近弄一個情緒識別與性別識別的東東。
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()

結果如下:

這裏寫圖片描述

長得醜請別噴,,,,,,

發佈了79 篇原創文章 · 獲贊 23 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章