emotion_app / app.py
KPDGrp3's picture
Update app.py
73279b4 verified
Raw History Blame Contribute Delete
5.08 kB
import cv2
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array
import gradio as gr
import json
# ==========================================
# 1. 加载模型与标签
# ==========================================
emotion_model = load_model('ferplus_cnn_best.h5')
emotion_labels = ['anger', 'contempt', 'disgust', 'fear', 'happiness', 'neutral', 'sadness', 'surprise']
# 载入复合情绪规则
try:
with open('compound_rules.json', 'r', encoding='utf-8') as f:
compound_rules = json.load(f)['compound_emotions']
except FileNotFoundError:
print("[Error] compound_rules.json not found! Please upload it.")
compound_rules = []
# 加载人脸检测器
net = cv2.dnn.readNetFromCaffe("deploy.prototxt", "res10_300x300_ssd_iter_140000.caffemodel")
# ==========================================
# 2. 复合情绪逻辑
# ==========================================
def get_compound_emotion(predictions):
for rule in compound_rules:
meets = True
for cond in rule['conditions']:
idx = emotion_labels.index(cond['emotion'])
if predictions[idx] < cond['min']:
meets = False
break
if meets:
conf = np.mean([predictions[emotion_labels.index(c['emotion'])] for c in rule['conditions']])
return rule['name'], float(conf)
return None, None
# ==========================================
# 3. 核心检测逻辑 (适配网页)
# ==========================================
def predict_emotion(input_img):
if input_img is None:
return None, "Please upload or take a photo first."
# Gradio RGB 转 OpenCV BGR
frame = cv2.cvtColor(input_img, cv2.COLOR_RGB2BGR)
h, w = frame.shape[:2]
blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300), (104.0, 177.0, 123.0))
net.setInput(blob)
detections = net.forward()
confidences = {label: 0.0 for label in emotion_labels}
faces_found = False
for i in range(detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > 0.5:
faces_found = True
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(x, y, x2, y2) = box.astype("int")
x, y = max(0, x), max(0, y)
x2, y2 = min(w, x2), min(h, y2)
gray_frame = cv2.cvtColor(frame[y:y2, x:x2], cv2.COLOR_BGR2GRAY)
if gray_frame.size == 0: continue
face_roi = cv2.resize(gray_frame, (112, 112))
face_roi = face_roi.astype('float32') / 255.0
face_roi = img_to_array(face_roi)
face_roi = np.expand_dims(face_roi, axis=0)
# 模型预测基础情绪
preds = emotion_model.predict(face_roi, verbose=0)[0]
# 更新右侧统计图表的数据 (始终显示8个基础情绪的原始分布)
confidences = {emotion_labels[i]: float(preds[i]) for i in range(len(emotion_labels))}
# 复合情绪判断
compound_name, compound_conf = get_compound_emotion(preds)
if compound_name:
label_text = f"{compound_name} ({compound_conf * 100:.1f}%)"
color = (0, 255, 0) if compound_conf > 0.7 else (0, 255, 255) if compound_conf > 0.4 else (0, 0, 255)
else:
emotion_index = np.argmax(preds)
emotion_label = emotion_labels[emotion_index]
conf = preds[emotion_index]
label_text = f"{emotion_label} ({conf * 100:.1f}%)"
color = (0, 255, 0) if conf > 0.7 else (0, 255, 255) if conf > 0.4 else (0, 0, 255)
# 画框和文字
cv2.rectangle(frame, (x, y), (x2, y2), color, 3)
cv2.putText(frame, label_text, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)
if not faces_found:
return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), "No face detected"
# 返回处理后的图片 和 用于生成进度条的字典
return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), confidences
# ==========================================
# 4. Gradio 界面设计
# ==========================================
with gr.Blocks(theme=gr.themes.Default()) as demo:
gr.Markdown("# Facial Emotion Recognition")
gr.Markdown("Identify complex human emotions (e.g., Frustrated, Embarrassed) from images or webcam using customized JSON rules.")
with gr.Row():
with gr.Column():
input_image = gr.Image(label="Input Source", sources=["upload", "webcam"], type="numpy")
run_btn = gr.Button("Analyze Emotion", variant="primary")
with gr.Column():
output_image = gr.Image(label="Detection Result")
output_labels = gr.Label(label="Base Emotion Probabilities", num_top_classes=4)
run_btn.click(fn=predict_emotion, inputs=input_image, outputs=[output_image, output_labels])
if __name__ == "__main__":
demo.launch()