YuAnTsai's picture
Update emotion.py
74a97af verified
Raw History Blame
9.38 kB
import time
from dataclasses import dataclass, field
from typing import Optional, Dict, Tuple, List
import gradio as gr
import numpy as np
import cv2
from PIL import Image
import mediapipe as mp
from mediapipe.tasks import python
from mediapipe.tasks.vision import FaceDetector, FaceDetectorOptions, RunningMode
from deepface import DeepFace
# =========================
# 參數與 MediaPipe 初始化
# =========================
ANALYZE_EVERY_SEC = 0.6 # MediaPipe 很快,可以稍微縮短間隔
DOWNSAMPLE_W = 720
EMA_ALPHA = 0.45
CONF_TH = 40.0
SWITCH_CONFIRM_SEC = 2.0
IOU_TH = 0.25
ZOOM_FACTOR = 1.3
# MediaPipe 設定
MODEL_PATH = 'model/blaze_face_short_range.tflite'
base_options = python.BaseOptions(model_asset_path=MODEL_PATH)
options = FaceDetectorOptions(
base_options=base_options,
running_mode=RunningMode.IMAGE
)
# 建立全域偵測器
mp_detector = FaceDetector.create_from_options(options)
# =========================
# 情緒映射與標籤
# =========================
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 = {
1: "1(心情差)", 2: "2(不太好)", 3: "3(普通)",
4: "4(不錯)", 5: "5(超棒)",
}
SCORE_CHOICES = [SCORE_LABELS[i] for i in [1, 2, 3, 4, 5]]
@dataclass
class AppState:
running: bool = True
finished: bool = False
last_analyze_t: float = 0.0
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
# =========================
# UI 工具與輔助函式
# =========================
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}%"
score_desc = SCORE_LABELS[score].split("(")[1].rstrip(")")
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}({score_desc})</div>
<div style="font-size:14px;">信心值:{conf_txt}</div>
</div>
"""
def _score_radio_value(score: int) -> str:
return SCORE_LABELS[min(5, max(1, int(score)))]
def _iou(a, b) -> float:
ax, ay, aw, ah = a
bx, by, bw, bh = b
ix, iy = max(ax, bx), max(ay, by)
iw, ih = max(0, min(ax+aw, bx+bw)-ix), max(0, min(ay+ah, by+bh)-iy)
area_i = iw * ih
if area_i <= 0: return 0.0
return area_i / (aw*ah + bw*bh - area_i)
def _zoom_center(frame: np.ndarray, zoom: float) -> np.ndarray:
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]
# =========================
# MediaPipe 偵測邏輯
# =========================
def get_face_mediapipe(frame: np.ndarray, prev_bbox: Optional[Tuple[int, int, int, int]]):
h, w = frame.shape[:2]
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=frame)
res = mp_detector.detect(mp_image)
if not res.detections:
return None, None
candidates = []
for det in res.detections:
b = det.bounding_box
# 數值校正,避免超出邊界導致 DeepFace 報錯
nx, ny = max(0, b.origin_x), max(0, b.origin_y)
nw, nh = min(b.width, w - nx), min(b.height, h - ny)
if nw > 10 and nh > 10:
candidates.append(((nx, ny, nw, nh), frame[ny:ny+nh, nx:nx+nw]))
if not candidates:
return None, None
# 優先匹配上一幀的位置
if prev_bbox:
best_match = max(candidates, key=lambda c: _iou(prev_bbox, c[0]), default=(None, None))
if best_match[0] and _iou(prev_bbox, best_match[0]) > IOU_TH:
return best_match
# 否則回傳面積最大者
return max(candidates, key=lambda c: c[0][2] * c[0][3])
# =========================
# Gradio 事件處理
# =========================
def on_stream(frame_rgb: np.ndarray, st: AppState):
if not st.running or st.finished:
res_html = _result_card_html(st.final_emo, st.final_conf) if st.final_emo else _hint_html("辨識停止")
return gr.update(visible=False), res_html, _score_radio_value(st.final_score), _hint_html("已完成"), st, gr.update()
if frame_rgb is None:
return gr.update(), gr.update(), gr.update(), _hint_html("等待攝影機..."), st, gr.update()
frame_rgb = _zoom_center(frame_rgb, ZOOM_FACTOR)
now = time.time()
# 頻率控制
if now - st.last_analyze_t < ANALYZE_EVERY_SEC:
return gr.update(), gr.update(), gr.update(), gr.update(), st, gr.update()
st.last_analyze_t = now
# 使用 MediaPipe 找臉
bbox, face_crop = get_face_mediapipe(frame_rgb, st.track_bbox)
if face_crop is None:
return gr.update(), gr.update(), gr.update(), _hint_html("未偵測到人臉"), st, gr.update()
st.track_bbox = bbox
# DeepFace 情緒分析 (傳入已裁切的人臉)
try:
res = DeepFace.analyze(face_crop, actions=['emotion'], enforce_detection=False, silent=True)
emo_dict = res[0]['emotion'] if isinstance(res, list) else res['emotion']
except:
return gr.update(), gr.update(), gr.update(), gr.update(), st, gr.update()
top_key = max(emo_dict, key=lambda k: float(emo_dict[k]))
top_conf = float(emo_dict[top_key])
# 平滑處理 (EMA)
if top_conf >= CONF_TH:
keys = list(emo_dict.keys())
vals = np.array([float(emo_dict[k]) for k in keys])
if st.ema_scores is None or st.emo_keys != keys:
st.ema_scores, st.emo_keys = vals, keys
else:
st.ema_scores = EMA_ALPHA * vals + (1.0 - EMA_ALPHA) * st.ema_scores
st.stable_emo = st.emo_keys[int(np.argmax(st.ema_scores))]
# 穩定切換邏輯
if st.candidate_emo != st.stable_emo:
st.candidate_emo, st.candidate_since = st.stable_emo, now
elif st.candidate_since and (now - st.candidate_since) >= SWITCH_CONFIRM_SEC:
st.final_emo, st.final_conf = st.stable_emo, top_conf
st.final_score = EMO_TO_SCORE.get(st.stable_emo, 3)
st.finished, st.running = True, False
return (
gr.update(visible=not st.finished),
gr.update(value=_result_card_html(st.stable_emo, top_conf)),
gr.update(value=_score_radio_value(EMO_TO_SCORE.get(st.stable_emo, 3))),
gr.update(value=_hint_html("正在辨識中,請保持表情穩定...")),
st,
gr.update()
)
def on_restart(st: AppState):
new_st = AppState()
return (
gr.update(visible=True),
_hint_html("等待影像..."),
_score_radio_value(3),
_hint_html("請點擊 Record 開始"),
new_st,
gr.update()
)
def on_stop(st: AppState):
st.running, st.finished = False, True
return gr.update(visible=False), gr.update(), gr.update(), _hint_html("已手動停止"), st, gr.update()
# =========================
# Gradio UI 介面
# =========================
css = ".gradio-container { background-color: #f7f9fc; }"
with gr.Blocks(title="AI 情緒偵測系統", css=css) as demo:
state = gr.State(AppState())
gr.Markdown("# 🎭 AI 情緒辨識評分系統")
status_hint = gr.HTML(_hint_html("請開啟攝影機並點擊錄製"))
with gr.Row():
with gr.Column(scale=1):
webcam = gr.Image(sources=["webcam"], streaming=True, type="numpy", label="攝影機畫面")
with gr.Row():
btn_re = gr.Button("🔄 重新辨識", variant="primary")
btn_stop = gr.Button("🛑 停止", variant="secondary")
with gr.Column(scale=1):
res_display = gr.HTML(_result_card_html("unknown", 0))
gr.Markdown("### 系統判定心情分數")
mood_radio = gr.Radio(choices=SCORE_CHOICES, value=SCORE_LABELS[3], interactive=False, label="")
# 事件綁定
webcam.stream(
fn=on_stream,
inputs=[webcam, state],
outputs=[webcam, res_display, mood_radio, status_hint, state, btn_re]
)
btn_re.click(fn=on_restart, inputs=[state], outputs=[webcam, res_display, mood_radio, status_hint, state, btn_re])
btn_stop.click(fn=on_stop, inputs=[state], outputs=[webcam, res_display, mood_radio, status_hint, state, btn_re])
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)