Update app.py
Browse files
app.py
CHANGED
|
@@ -41,15 +41,15 @@ def extract_feature(signal: np.ndarray, sr: int) -> np.ndarray:
|
|
| 41 |
# --- 4. 三種預測函式 ---
|
| 42 |
|
| 43 |
def predict_face(img: np.ndarray):
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
def predict_voice(audio):
|
| 55 |
"""
|
|
@@ -76,39 +76,29 @@ def predict_text(text: str):
|
|
| 76 |
return {pred["label"]: float(pred["score"])}
|
| 77 |
|
| 78 |
# --- 5. 建立 Gradio 介面 ---
|
| 79 |
-
|
| 80 |
-
""
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
outputs=gr.Label(num_top_classes=1),
|
| 91 |
-
title="臉部情緒 (即時 Webcam)",
|
| 92 |
-
description="允許攝影機拍照後自動分析當前表情的情緒分佈。"
|
| 93 |
-
)
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
description="錄製語音或上傳音訊檔,模型會回傳「驚訝/生氣/開心/悲傷/害怕」五種情緒機率。"
|
| 102 |
-
)
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
outputs=gr.Label(num_top_classes=1),
|
| 109 |
-
title="文字情緒",
|
| 110 |
-
description="輸入中文文字,即時判斷文字情緒並回傳標籤與信心分數。"
|
| 111 |
-
)
|
| 112 |
|
| 113 |
# 三合一 Tabs
|
| 114 |
app = gr.TabbedInterface(
|
|
|
|
| 41 |
# --- 4. 三種預測函式 ---
|
| 42 |
|
| 43 |
def predict_face(img: np.ndarray):
|
| 44 |
+
if img is None:
|
| 45 |
+
return {} # 没有帧时返回空
|
| 46 |
+
try:
|
| 47 |
+
result = DeepFace.analyze(img, actions=["emotion"], detector_backend="opencv")
|
| 48 |
+
return result.get("emotion", {})
|
| 49 |
+
except Exception as e:
|
| 50 |
+
# 遇到错误时,可返回空或日志
|
| 51 |
+
print("DeepFace 分析错误:", e)
|
| 52 |
+
return {}
|
| 53 |
|
| 54 |
def predict_voice(audio):
|
| 55 |
"""
|
|
|
|
| 76 |
return {pred["label"]: float(pred["score"])}
|
| 77 |
|
| 78 |
# --- 5. 建立 Gradio 介面 ---
|
| 79 |
+
with gr.Blocks() as demo:
|
| 80 |
+
gr.Markdown("## 多模態即時情緒分析")
|
| 81 |
+
with gr.Tabs():
|
| 82 |
+
# 臉部情緒 Tab
|
| 83 |
+
with gr.TabItem("臉部情緒"):
|
| 84 |
+
gr.Markdown("### 臉部情緒 (即時 Webcam Streaming 分析)")
|
| 85 |
+
with gr.Row():
|
| 86 |
+
webcam = gr.Image(sources="webcam", streaming=True, type="numpy", label="攝像頭畫面")
|
| 87 |
+
emotion_output = gr.Label(label="情緒分布")
|
| 88 |
+
# 关键:用 stream 让每帧到达时调用 predict_face 并更新 emotion_output
|
| 89 |
+
webcam.stream(fn=predict_face, inputs=webcam, outputs=emotion_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
# 其餘 Tab 可按原先寫法,或用 Blocks 方式
|
| 92 |
+
with gr.TabItem("語音情緒"):
|
| 93 |
+
audio = gr.Audio(sources="microphone", streaming=False, type="filepath", label="錄音")
|
| 94 |
+
audio_output = gr.Label(label="語音情緒結果")
|
| 95 |
+
# 用 change/submit 触发:录音结束后调用 predict_voice
|
| 96 |
+
audio.change(fn=predict_voice, inputs=audio, outputs=audio_output)
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
with gr.TabItem("文字情緒"):
|
| 99 |
+
text = gr.Textbox(lines=3, placeholder="請輸入中文文字…")
|
| 100 |
+
text_output = gr.Label(label="文字情緒結果")
|
| 101 |
+
text.submit(fn=predict_text, inputs=text, outputs=text_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
# 三合一 Tabs
|
| 104 |
app = gr.TabbedInterface(
|