基于深度学习的性别与表情实时检测(图片与视频)------(一)

https://github.com/zouyuwuse/face_classification

這個工程項目真的很好玩,不僅可以識別圖像中人的性別與表情,與此同時也可以識別视频中的性别与表情。给大家上传一些搞笑的图片。





感觉在第一个图片中识别率不太高,还有很多改善空间。上面的图片都是我在自己电脑上跑出来的,运行过程请看下图:

代码分析如下:


###face_classification/src/image_emotion_gender_demo.pyimport sys

import cv2
from keras.models import load_model
import numpy as np
##以上都是一些模块的导入,从上面可以看到我们需要安装OPENCV、keras、numpy、tensorflow等等。ZOUYU
from utils.datasets import get_labels
from utils.inference import detect_faces
from utils.inference import draw_text
from utils.inference import draw_bounding_box
from utils.inference import apply_offsets
from utils.inference import load_detection_model
from utils.inference import load_image
from utils.preprocessor import preprocess_input
##上面的这些函数都是自己定义的,可以在工程中找到。ZOUYU


# parameters for loading data and images  装载图片与模型 ZOUYU
image_path = sys.argv[1]
detection_model_path = '../trained_models/detection_models/haarcascade_frontalface_default.xml'
emotion_model_path = '../trained_models/emotion_models/fer2013_mini_XCEPTION.102-0.66.hdf5'
gender_model_path = '../trained_models/gender_models/simple_CNN.81-0.96.hdf5'
emotion_labels = get_labels('fer2013')
gender_labels = get_labels('imdb')
font = cv2.FONT_HERSHEY_SIMPLEX

# hyper-parameters for bounding boxes shape 这里表示的是在神经网络中,BBOX画框的大小。ZOUYU
gender_offsets = (30, 60)
gender_offsets = (10, 10)
emotion_offsets = (20, 40)
emotion_offsets = (0, 0)

# loading models 这里很重要了,这是要装载你训练的各种模型,也就是你的算法跑出来的模型 ,具体的效果就看你的调试能力了。ZOUYU
face_detection = load_detection_model(detection_model_path)
emotion_classifier = load_model(emotion_model_path, compile=False)
gender_classifier = load_model(gender_model_path, compile=False)

# getting input model shapes for inference
emotion_target_size = emotion_classifier.input_shape[1:3]
gender_target_size = gender_classifier.input_shape[1:3]

# loading images
rgb_image = load_image(image_path, grayscale=False)
gray_image = load_image(image_path, grayscale=True)
gray_image = np.squeeze(gray_image)
gray_image = gray_image.astype('uint8')##在这里把图片或者视频全部转成灰度图。zouyu

faces = detect_faces(face_detection, gray_image)
for face_coordinates in faces:
    x1, x2, y1, y2 = apply_offsets(face_coordinates, gender_offsets)##人脸性别检测。zouyu
    rgb_face = rgb_image[y1:y2, x1:x2]

    x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets)##人脸情感检测。zouyu
    gray_face = gray_image[y1:y2, x1:x2]

    try:
        rgb_face = cv2.resize(rgb_face, (gender_target_size))
        gray_face = cv2.resize(gray_face, (emotion_target_size))
    except:
        continue

    rgb_face = preprocess_input(rgb_face, False)
    rgb_face = np.expand_dims(rgb_face, 0)
    gender_prediction = gender_classifier.predict(rgb_face)
    gender_label_arg = np.argmax(gender_prediction)
    gender_text = gender_labels[gender_label_arg]

    gray_face = preprocess_input(gray_face, True)
    gray_face = np.expand_dims(gray_face, 0)
    gray_face = np.expand_dims(gray_face, -1)
    emotion_label_arg = np.argmax(emotion_classifier.predict(gray_face))
    emotion_text = emotion_labels[emotion_label_arg]

    if gender_text == gender_labels[0]:
        color = (0, 0, 255)
    else:
        color = (255, 0, 0)

    draw_bounding_box(face_coordinates, rgb_image, color)##这里face_coordinates很重要,这是一个人脸的具体坐标。zouyu
    draw_text(face_coordinates, rgb_image, gender_text, color, 0, -20, 1, 2)##性别标记zouyu
    draw_text(face_coordinates, rgb_image, emotion_text, color, 0, -50, 1, 2)##表情标记zouyu

bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)
cv2.imwrite('../images/predicted_test_image.png', bgr_image)##在原图的基础上,把性别与表情标记出来后,用OPENCV写图片,保存图片。zouyu
 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值