GCLing commited on
Commit
babd923
·
verified ·
1 Parent(s): 0422b31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -40
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
- 臉部情緒分析:使用 DeepFace 分析單張影像 (numpy array, HxWx3)。
46
- 強制使用 OpenCV 後端以避免 retinaface/tf 版本衝突。
47
- 回傳格式為 dict,例如 {"happy": 0.80, "sad": 0.05, ...}
48
- """
49
- # DeepFace.analyze 可能較耗時,建議在 Space 上需有適當硬體
50
- result = DeepFace.analyze(img, actions=["emotion"], detector_backend="opencv")
51
- # result["emotion"] 是字典
52
- return result["emotion"]
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
- def build_interface():
80
- """
81
- 建立一個 TabbedInterface,包含三個子 Interface:
82
- - 臉部情緒 (Webcam 拍照或上傳)
83
- - 語音情緒 (錄音或上傳音檔)
84
- - 文字情緒 (文字輸入)
85
- """
86
- # 臉部情緒:使用 gr.Interface Blocks?
87
- face_interface = gr.Interface(
88
- fn=predict_face,
89
- inputs=gr.Image(sources="webcam", streaming=True, type="numpy"),
90
- outputs=gr.Label(num_top_classes=1),
91
- title="臉部情緒 (即時 Webcam)",
92
- description="允許攝影機拍照後自動分析當前表情的情緒分佈。"
93
- )
94
 
95
- # 語音情緒:錄音或上傳
96
- voice_interface = gr.Interface(
97
- fn=predict_voice,
98
- inputs=gr.Audio(sources="microphone", type="filepath"),
99
- outputs=gr.Label(num_top_classes=1),
100
- title="語音情緒",
101
- description="錄製語音或上傳音訊檔,模型會回傳「驚訝/生氣/開心/悲傷/害怕」五種情緒機率。"
102
- )
103
 
104
- # 文字情緒:輸入中文
105
- text_interface = gr.Interface(
106
- fn=predict_text,
107
- inputs=gr.Textbox(lines=3, placeholder="請輸入中文文字…"),
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(