|
|
@@ -92,7 +92,7 @@ def analyze_emotion(state: GraphState) -> GraphState:
|
|
|
result = DeepFace.analyze(
|
|
|
img_path=state["image_path"],
|
|
|
actions=["emotion"],
|
|
|
- detector_backend="mediapipe", # 比 opencv 快 3x
|
|
|
+ detector_backend="opencv", # 内置 Haar Cascade,无需额外模型
|
|
|
enforce_detection=False,
|
|
|
silent=True,
|
|
|
)
|
|
|
@@ -113,22 +113,27 @@ def format_result(state: GraphState) -> GraphState:
|
|
|
return state
|
|
|
|
|
|
emo = state["emotion_result"]
|
|
|
- # emo 格式:{'emotion': {'happy': 0.85, 'neutral': 0.10, ...}, ...}
|
|
|
+ # DeepFace 返回 0-100 百分比(如 {'happy': 85.0, ...}),需归一化为 0.0-1.0
|
|
|
raw_emotions = emo.get("emotion", {})
|
|
|
|
|
|
+ # 归一化:值 >1 视为百分比,除以 100
|
|
|
+ norm = {}
|
|
|
+ for k, v in raw_emotions.items():
|
|
|
+ norm[k] = round(v / 100.0, 4) if v > 1.0 else round(v, 4)
|
|
|
+
|
|
|
# 找出主导情绪
|
|
|
- dominant = max(raw_emotions, key=raw_emotions.get) if raw_emotions else "neutral"
|
|
|
+ dominant = max(norm, key=norm.get) if norm else "neutral"
|
|
|
dominant_zh = EMOTION_ZH.get(dominant, dominant)
|
|
|
|
|
|
# 结构化列表
|
|
|
emotions = [
|
|
|
- {"emotion": k, "confidence": round(v, 4)}
|
|
|
- for k, v in sorted(raw_emotions.items(), key=lambda x: -x[1])
|
|
|
+ {"emotion": k, "confidence": v}
|
|
|
+ for k, v in sorted(norm.items(), key=lambda x: -x[1])
|
|
|
]
|
|
|
|
|
|
state["dominant_emotion"] = dominant
|
|
|
state["dominant_label_zh"] = dominant_zh
|
|
|
- state["all_emotions"] = {k: round(v, 4) for k, v in raw_emotions.items()}
|
|
|
+ state["all_emotions"] = norm
|
|
|
state["emotions"] = emotions
|
|
|
return state
|
|
|
|