CV:基于keras利用cv2自带两步检测法对《跑男第六季第五期》之如花片段(或调用摄像头)进行实时性别&脸部表情检测

简介: CV:基于keras利用cv2自带两步检测法对《跑男第六季第五期》之如花片段(或调用摄像头)进行实时性别&脸部表情检测

输出结果

image.png


设计思路

image.png


核心代码

from statistics import mode

import cv2

from keras.models import load_model

import numpy as np

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

frame_window = 10  

gender_offsets = (30, 60)

emotion_offsets = (20, 40)

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)

emotion_target_size = emotion_classifier.input_shape[1:3]

gender_target_size = gender_classifier.input_shape[1:3]

gender_window = []

emotion_window = []

cv2.namedWindow('window_frame_by_Jason_Niu')

# video_capture = cv2.VideoCapture(0)

video_capture = cv2.VideoCapture("F:\File_Python\Python_example\YOLOv3_use_TF\RunMan5.mp4")

while True:

   bgr_image = video_capture.read()[1]

   gray_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY) #分别将读取的图像进行灰化、RGB化处理

   rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)

   faces = detect_faces(face_detection, gray_image)  

   for face_coordinates in faces:

 

       x1, x2, y1, y2 = apply_offsets(face_coordinates, gender_offsets)

       rgb_face = rgb_image[y1:y2, x1:x2]  

       x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets)

       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

       gray_face = preprocess_input(gray_face, False)  

       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]

       emotion_window.append(emotion_text)    

       rgb_face = np.expand_dims(rgb_face, 0)

       rgb_face = preprocess_input(rgb_face, False)

       gender_prediction = gender_classifier.predict(rgb_face)

       gender_label_arg = np.argmax(gender_prediction)

       gender_text = gender_labels[gender_label_arg]

       gender_window.append(gender_text)

       if len(gender_window) > frame_window:

           emotion_window.pop(0)        

           gender_window.pop(0)

       try:

           emotion_mode = mode(emotion_window)

           gender_mode = mode(gender_window)

       except:

           continue

       if gender_text == gender_labels[0]:

           color = (0, 0, 255)

       else:

           color = (255, 0, 0)

       draw_bounding_box(face_coordinates, rgb_image, color)

       draw_text(face_coordinates, rgb_image, gender_mode,  

                 color, 0, -20, 1, 4)

       draw_text(face_coordinates, rgb_image, emotion_mode,

                 color, 0, -45, 1, 4)

     

   bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)  

   cv2.namedWindow("window_frame_by_Jason_Niu",0);

   cv2.resizeWindow("window_frame_by_Jason_Niu", 640, 380);

   cv2.imshow('window_frame_by_Jason_Niu', bgr_image)

   if cv2.waitKey(1) & 0xFF == ord('q'):

       break


相关文章
|
存储 缓存 前端开发
SPA 单页面的优缺点
单页面应用(SPA)优点包括:用户体验流畅、无需刷新页面、减少服务器压力。缺点有:SEO优化困难、初始加载时间长、前端逻辑复杂。
|
JavaScript 计算机视觉
FLUX.1 Tools 全家桶开源!文末附一键ComfyUI启动链接
Black Forest Labs 发布了 FLUX.1 Tools,一套增强 FLUX.1 文本转图像模型的工具集,包括 FLUX.1 Fill、FLUX.1 Depth、FLUX.1 Canny 和 FLUX.1 Redux,分别用于图像修复、深度引导、边缘检测和图像重组。提供详细的安装指南和模型下载链接,支持用户快速上手并优化图像处理流程。
4242 4
FLUX.1 Tools 全家桶开源!文末附一键ComfyUI启动链接
|
关系型数据库 MySQL
Authentication plugin ‘caching_sha2_password‘ cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2
Authentication plugin ‘caching_sha2_password‘ cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2
1280 0
BXA
|
机器学习/深度学习 人工智能 算法
C++与Python:哪种语言更适合机器学习
机器学习是一种人工智能的应用,它可以让计算机通过对大量数据的学习和分析,自动地寻找数据中的规律和模式,并且利用这些规律和模式进行预测和决策,从而达到人工智能的效果。机器学习技术在人脸识别、语音识别、推荐系统、自然语言处理等多个领域都有广泛的应用
BXA
959 0
|
机器学习/深度学习 C++
DFS经典问题——八皇后
DFS经典问题——八皇后
454 0
DFS经典问题——八皇后
|
关系型数据库 数据库
PostgreSQL9.6+新增空闲事务自动查杀功能
[TOC] 概述 PostgreSQL9.6 版本较之前版本新增了一个参数:idle_in_transaction_session_timeout ,该参数是为了自动查杀存在的空闲事物idle in transaction。
2619 0
|
6天前
|
云安全 人工智能 安全
AI被攻击怎么办?
阿里云提供 AI 全栈安全能力,其中对网络攻击的主动识别、智能阻断与快速响应构成其核心防线,依托原生安全防护为客户筑牢免疫屏障。
|
15天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~