YuAnTsai's picture
Update emotion.py
41ef79e verified
Raw History Blame
8.46 kB
import time
import cv2
from dataclasses import dataclass, field
from typing import Optional, Dict, Tuple, List
import gradio as gr
import numpy as np
from PIL import Image
from deepface import DeepFace
# =========================
# 參數(效能優化版)
# =========================
ANALYZE_EVERY_SEC = 0.8
DOWNSAMPLE_W = 720
EMA_ALPHA = 0.45
CONF_TH = 40.0
SWITCH_CONFIRM_SEC = 2.5
IOU_TH = 0.25
ZOOM_FACTOR = 1.3
DETECTOR_BACKEND = "mtcnn"
ALIGN_FACE = True
# =========================
# 狀態與映射
# =========================
EMO_ZH = {"angry": "生氣", "disgust": "厭惡", "fear": "害怕", "happy": "開心", "sad": "難過", "surprise": "驚訝", "neutral": "平靜", "unknown": "未知"}
EMO_TO_SCORE = {"angry": 1, "disgust": 1, "fear": 1, "sad": 2, "neutral": 3, "surprise": 4, "happy": 5, "unknown": 3}
SCORE_LABELS = {i: f"{i}({desc})" for i, desc in zip(range(1,6), ["心情差","不太好","普通","不錯","超棒"])}
SCORE_CHOICES = [SCORE_LABELS[i] for i in range(1,6)]
@dataclass
class AppState:
mode: str = "攝影機辨識"
running: bool = True
finished: bool = False
last_analyze_t: float = 0.0
last_check_t: float = 0.0 # 新增:控制檢查位置的頻率
is_aligned: bool = False # 新增:快取對準狀態
track_bbox: Optional[Tuple[int, int, int, int]] = None
ema_scores: Optional[np.ndarray] = None
emo_keys: List[str] = field(default_factory=list)
stable_emo: str = "unknown"
candidate_emo: Optional[str] = None
candidate_since: Optional[float] = None
final_emo: Optional[str] = None
final_conf: Optional[float] = None
final_score: int = 3
# =========================
# 介面 HTML
# =========================
def _hint_html(msg: str) -> str:
return f'<div style="border-radius:14px;padding:14px;border:1px dashed #DADADA;background:#FAFAFA;color:#000000;">{msg}</div>'
def _result_card_html(emo_key: str, conf: Optional[float]) -> str:
zh = EMO_ZH.get(emo_key, emo_key)
score = EMO_TO_SCORE.get(emo_key, 3)
conf_txt = "" if conf is None else f"{conf:.1f}%"
return f'<div style="border-radius:16px;padding:20px;border:1px solid #E6E6E6;background:#FFFFFF;color:#000000;"><div style="font-size:14px;margin-bottom:8px;">辨識結果</div><div style="font-size:42px;font-weight:800;line-height:1.1;margin-bottom:14px;">{zh}</div><div style="font-size:16px;margin-bottom:6px;">心情分數:{score}</div><div style="font-size:14px;">信心值:{conf_txt}</div></div>'
# =========================
# 影像處理
# =========================
def _zoom_center(frame, zoom):
if frame is None or zoom <= 1.0: return frame
h, w = frame.shape[:2]
nw, nh = int(w / zoom), int(h / zoom)
sx, sy = (w - nw) // 2, (h - nh) // 2
return frame[sy:sy+nh, sx:sx+nw]
def _draw_ui_overlay(frame: np.ndarray, is_aligned: bool):
canvas = frame.copy()
h, w = canvas.shape[:2]
bw, bh = int(w * 0.45), int(h * 0.55)
x1, y1 = (w - bw) // 2, (h - bh) // 2
x2, y2 = x1 + bw, y1 + bh
color = (0, 255, 0) if is_aligned else (255, 0, 0)
text = "SCANNING..." if is_aligned else "PLACE FACE HERE"
# 效能優化:不再使用 addWeighted 製作全畫面遮罩,改用簡單線條
cv2.rectangle(canvas, (x1, y1), (x2, y2), color, 2)
l = 40
cv2.line(canvas, (x1, y1), (x1+l, y1), color, 5)
cv2.line(canvas, (x1, y1), (x1, y1+l), color, 5)
cv2.line(canvas, (x2, y2), (x2-l, y2), color, 5)
cv2.line(canvas, (x2, y2), (x2, y2-l), color, 5)
cv2.putText(canvas, text, (x1, y1 - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)
return canvas
# =========================
# 串流核心
# =========================
def on_stream(frame_rgb: np.ndarray, st: AppState):
if frame_rgb is None or st.finished or (not st.running):
return (None, gr.update(), gr.update(), gr.update(), st, gr.update())
now = time.time()
frame_rgb = _zoom_center(frame_rgb, ZOOM_FACTOR)
# --- 效能優化點:降低檢查頻率 (每 0.3 秒才檢查一次人臉位置) ---
if now - st.last_check_t > 0.3:
st.last_check_t = now
# 極小化影像以利快速檢查
small_check = np.array(Image.fromarray(frame_rgb).resize((250, 250)))
try:
faces = DeepFace.extract_faces(img_path=small_check, detector_backend="opencv", enforce_detection=False)
if faces and faces[0]['face'] is not None:
fa = faces[0]['facial_area']
# 將座標還原到原圖比例
fx, fy = fa['x'] * (frame_rgb.shape[1]/250), fa['y'] * (frame_rgb.shape[0]/250)
fw, fh = fa['w'] * (frame_rgb.shape[1]/250), fa['h'] * (frame_rgb.shape[0]/250)
# 檢查中心點是否在導引框內 (中央 45%x55%)
h, w = frame_rgb.shape[:2]
gx1, gy1 = (w - int(w*0.45))//2, (h - int(h*0.55))//2
gx2, gy2 = gx1 + int(w*0.45), gy1 + int(h*0.55)
st.is_aligned = (gx1 < fx + fw/2 < gx2) and (gy1 < fy + fh/2 < gy2)
else:
st.is_aligned = False
except:
st.is_aligned = False
# 繪製畫面 (這個步驟很快)
frame_with_ui = _draw_ui_overlay(frame_rgb, st.is_aligned)
# --- 深度分析邏輯 (只有對準時執行) ---
if st.is_aligned and (now - st.last_analyze_t >= ANALYZE_EVERY_SEC):
st.last_analyze_t = now
# 進行高品質偵測與分析
res = DeepFace.analyze(img_path=frame_rgb, actions=["emotion"], enforce_detection=False, detector_backend="opencv")
emo_dict = res[0].get("emotion") if isinstance(res, list) else res.get("emotion")
if emo_dict:
top_key = max(emo_dict, key=lambda k: float(emo_dict[k]))
keys = list(emo_dict.keys())
vec = np.array([float(emo_dict[k]) for k in keys])
if st.ema_scores is None: st.ema_scores, st.emo_keys = vec, keys
else: st.ema_scores = EMA_ALPHA * vec + (1.0 - EMA_ALPHA) * st.ema_scores
stable_key = st.emo_keys[np.argmax(st.ema_scores)]
st.stable_emo = stable_key
if st.candidate_emo != stable_key:
st.candidate_emo, st.candidate_since = stable_key, now
elif st.candidate_since and (now - st.candidate_since >= SWITCH_CONFIRM_SEC):
st.final_emo, st.final_conf, st.running, st.finished = stable_key, float(emo_dict[top_key]), False, True
return (frame_with_ui, _result_card_html(stable_key, st.final_conf), SCORE_LABELS[EMO_TO_SCORE.get(stable_key, 3)], "辨識完成", st, gr.update(value="重新攝影機辨識"))
return (frame_with_ui, gr.update(value=_result_card_html(st.stable_emo, st.final_conf)), gr.update(), gr.update(), st, gr.update())
# =========================
# 其他 Button 函式 (簡化版以維持穩定)
# =========================
def on_restart(st: AppState):
st.running, st.finished, st.ema_scores, st.stable_emo, st.final_emo = True, False, None, "unknown", None
return (gr.update(visible=True), _hint_html("重新開始..."), SCORE_LABELS[3], "請對準方框", st, "重新攝影機辨識")
def on_stop(st: AppState):
st.running, st.finished = False, True
return (gr.update(visible=False), _hint_html("已停止"), SCORE_LABELS[3], "停止", st, "重新攝影機辨識")
# =========================
# UI
# =========================
with gr.Blocks(title="Emotion Detector") as demo:
state = gr.State(AppState())
gr.Markdown("## 情緒辨識系統 (效能優化版)")
hint = gr.HTML("請將臉部對準螢幕中央方框")
cam = gr.Image(sources=["webcam"], streaming=True, type="numpy", label="攝影機預覽")
with gr.Row():
btn_restart = gr.Button("重新攝影機辨識", variant="primary")
btn_stop = gr.Button("停止", variant="secondary")
result = gr.HTML(_hint_html("等待串流..."))
mood = gr.Radio(choices=SCORE_CHOICES, value=SCORE_LABELS[3], label="判定心情", interactive=False)
cam.stream(on_stream, [cam, state], [cam, result, mood, hint, state, btn_restart], show_progress="minimal")
btn_restart.click(on_restart, [state], [cam, result, mood, hint, state, btn_restart])
btn_stop.click(on_stop, [state], [cam, result, mood, hint, state, btn_restart])
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)