Spaces:
Sleeping
Sleeping
Commit
·
86d38f2
1
Parent(s):
ab4f6fb
簡化 Gradio 介面設計 - 修復 JSON schema 錯誤和移除 share=True
Browse files
app.py
DELETED
|
@@ -1,187 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import cv2
|
| 3 |
-
import numpy as np
|
| 4 |
-
import gradio as gr
|
| 5 |
-
from realtime_sign_prediction import RealtimeSignPredictor
|
| 6 |
-
|
| 7 |
-
class GradioSignPredictor:
|
| 8 |
-
def __init__(self):
|
| 9 |
-
"""初始化手語辨識預測器"""
|
| 10 |
-
self.predictor = RealtimeSignPredictor(
|
| 11 |
-
model_path="tsflow/models/best_model.pt",
|
| 12 |
-
config_path="tsflow/results/test_results.json",
|
| 13 |
-
sequence_length=50,
|
| 14 |
-
use_segmentation=True
|
| 15 |
-
)
|
| 16 |
-
print("✅ 手語辨識系統初始化完成!")
|
| 17 |
-
|
| 18 |
-
def process_frame(self, frame):
|
| 19 |
-
"""處理單一畫面並返回結果"""
|
| 20 |
-
if frame is None:
|
| 21 |
-
return frame, "等待攝像頭輸入..."
|
| 22 |
-
|
| 23 |
-
try:
|
| 24 |
-
# 處理畫面
|
| 25 |
-
results, keypoints, flow_features = self.predictor.process_frame(frame)
|
| 26 |
-
|
| 27 |
-
# 繪製關鍵點
|
| 28 |
-
annotated_frame = self.predictor.draw_landmarks(frame.copy(), results)
|
| 29 |
-
|
| 30 |
-
# 獲取預測結果
|
| 31 |
-
top_predictions = self.predictor.get_top_predictions(top_k=3)
|
| 32 |
-
|
| 33 |
-
# 格式化預測結果
|
| 34 |
-
if top_predictions:
|
| 35 |
-
prediction_text = "🎯 即時預測結果:\n\n"
|
| 36 |
-
for i, (label, confidence) in enumerate(top_predictions, 1):
|
| 37 |
-
confidence_bar = "█" * int(confidence * 20)
|
| 38 |
-
prediction_text += f"{i}. {label}: {confidence:.2%} {confidence_bar}\n"
|
| 39 |
-
|
| 40 |
-
# 添加序列資訊
|
| 41 |
-
prediction_text += f"\n📊 序列進度: {len(self.predictor.keypoint_sequence)}/{self.predictor.sequence_length}"
|
| 42 |
-
else:
|
| 43 |
-
prediction_text = "📡 正在收集動作序列...\n請在攝像頭前做手語動作"
|
| 44 |
-
|
| 45 |
-
return annotated_frame, prediction_text
|
| 46 |
-
|
| 47 |
-
except Exception as e:
|
| 48 |
-
return frame, f"❌ 處理錯誤: {str(e)}"
|
| 49 |
-
|
| 50 |
-
def clear_predictions(self):
|
| 51 |
-
"""清除預測序列"""
|
| 52 |
-
self.predictor.keypoint_sequence.clear()
|
| 53 |
-
self.predictor.flow_sequence.clear()
|
| 54 |
-
return "✅ 已清除預測序列,請重新開始"
|
| 55 |
-
|
| 56 |
-
# 初始化全域預測器
|
| 57 |
-
print("🚀 正在初始化手語辨識系統...")
|
| 58 |
-
global_predictor = GradioSignPredictor()
|
| 59 |
-
|
| 60 |
-
def process_video_frame(frame):
|
| 61 |
-
"""處理視訊畫面的包裝函數"""
|
| 62 |
-
return global_predictor.process_frame(frame)
|
| 63 |
-
|
| 64 |
-
def clear_predictions():
|
| 65 |
-
"""清除預測的包裝函數"""
|
| 66 |
-
return global_predictor.clear_predictions()
|
| 67 |
-
|
| 68 |
-
# 創建 Gradio 介面 - 修復 Gradio 4.44.x 兼容性
|
| 69 |
-
with gr.Blocks(
|
| 70 |
-
title="SignView2.0 - 手語辨識系統",
|
| 71 |
-
theme=gr.themes.Soft(),
|
| 72 |
-
css="""
|
| 73 |
-
.container { max-width: 1200px; margin: auto; }
|
| 74 |
-
.header { text-align: center; margin-bottom: 2rem; }
|
| 75 |
-
.prediction-box {
|
| 76 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 77 |
-
color: white;
|
| 78 |
-
padding: 1rem;
|
| 79 |
-
border-radius: 10px;
|
| 80 |
-
font-family: monospace;
|
| 81 |
-
}
|
| 82 |
-
"""
|
| 83 |
-
) as demo:
|
| 84 |
-
|
| 85 |
-
gr.Markdown("""
|
| 86 |
-
# 🤟 SignView2.0 - 手語辨識系統
|
| 87 |
-
|
| 88 |
-
**支援34種手語詞彙的即時辨識系統,準確率達94.25%**
|
| 89 |
-
|
| 90 |
-
## 📋 支援詞彙
|
| 91 |
-
`again`, `all`, `apple`, `bad`, `bathroom`, `beautiful`, `bird`, `black`, `blue`, `book`,
|
| 92 |
-
`bored`, `boy`, `brother`, `brown`, `but`, `computer`, `cousin`, `dance`, `day`, `deaf`,
|
| 93 |
-
`doctor`, `dog`, `draw`, `drink`, `eat`, `english`, `family`, `father`, `fine`, `finish`,
|
| 94 |
-
`fish`, `forget`, `friend`, `girl`
|
| 95 |
-
|
| 96 |
-
## 🚀 使用說明
|
| 97 |
-
1. 上傳影像或使用攝像頭拍攝手語動作
|
| 98 |
-
2. 系統會自動辨識並顯示結果
|
| 99 |
-
3. 點擊「清除預測」重新開始
|
| 100 |
-
""", elem_classes=["header"])
|
| 101 |
-
|
| 102 |
-
with gr.Row():
|
| 103 |
-
with gr.Column(scale=2):
|
| 104 |
-
# 影像輸入 - 使用 Image 組件解決 Gradio 4.44.x 兼容性問題
|
| 105 |
-
image_input = gr.Image(
|
| 106 |
-
label="📹 影像輸入 (攝像頭/上傳)",
|
| 107 |
-
sources=["webcam", "upload"],
|
| 108 |
-
height=400
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
# 處理按鈕
|
| 112 |
-
process_btn = gr.Button(
|
| 113 |
-
"🔍 分析手語動作",
|
| 114 |
-
variant="primary",
|
| 115 |
-
size="lg"
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
# 清除按鈕
|
| 119 |
-
clear_btn = gr.Button(
|
| 120 |
-
"🗑️ 清除預測序列",
|
| 121 |
-
variant="secondary",
|
| 122 |
-
size="lg"
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
with gr.Column(scale=1):
|
| 126 |
-
# 處理後的影像輸出
|
| 127 |
-
output_image = gr.Image(
|
| 128 |
-
label="🎯 辨識結果影像",
|
| 129 |
-
height=400
|
| 130 |
-
)
|
| 131 |
-
|
| 132 |
-
# 預測輸出
|
| 133 |
-
prediction_output = gr.Textbox(
|
| 134 |
-
label="📊 辨識結果",
|
| 135 |
-
lines=8,
|
| 136 |
-
value="等待影像輸入...",
|
| 137 |
-
elem_classes=["prediction-box"]
|
| 138 |
-
)
|
| 139 |
-
|
| 140 |
-
# 系統資訊
|
| 141 |
-
gr.Markdown("""
|
| 142 |
-
## 📊 系統資訊
|
| 143 |
-
- **模型準確率**: 94.25%
|
| 144 |
-
- **F1分數**: 94.24%
|
| 145 |
-
- **處理速度**: 30 FPS
|
| 146 |
-
- **特徵提取**: MediaPipe + 光流
|
| 147 |
-
- **模型架構**: BiLSTM + 注意力機制
|
| 148 |
-
- **背景分割**: MediaPipe Segmentation
|
| 149 |
-
|
| 150 |
-
## 🔧 技術特色
|
| 151 |
-
- MediaPipe Holistic 關鍵點檢測
|
| 152 |
-
- 光流動作特徵捕捉
|
| 153 |
-
- 人體分割背景去除
|
| 154 |
-
- 深度學習時序建模
|
| 155 |
-
""")
|
| 156 |
-
|
| 157 |
-
# 事件處理器
|
| 158 |
-
process_btn.click(
|
| 159 |
-
fn=process_video_frame,
|
| 160 |
-
inputs=[image_input],
|
| 161 |
-
outputs=[output_image, prediction_output]
|
| 162 |
-
)
|
| 163 |
-
|
| 164 |
-
clear_btn.click(
|
| 165 |
-
fn=clear_predictions,
|
| 166 |
-
outputs=[prediction_output]
|
| 167 |
-
)
|
| 168 |
-
|
| 169 |
-
gr.Markdown("""
|
| 170 |
-
---
|
| 171 |
-
### 📈 關於此系統
|
| 172 |
-
|
| 173 |
-
SignView2.0 使用最先進的深度學習技術進行手語辨識:
|
| 174 |
-
|
| 175 |
-
- **特徵提取**: 使用 MediaPipe 提取手部、身體關鍵點 + 光流特徵
|
| 176 |
-
- **背景處理**: MediaPipe Segmentation 自動去除背景干擾
|
| 177 |
-
- **時序建模**: 雙向LSTM + GRU + 多頭注意力機制
|
| 178 |
-
- **訓練資料**: 2380個手語視頻,經過數據增強和正規化
|
| 179 |
-
|
| 180 |
-
**開發者**: XiaoBai1221 | **平台**: Hugging Face Spaces
|
| 181 |
-
""")
|
| 182 |
-
|
| 183 |
-
# 啟動應用程式 - Hugging Face Spaces 會自動檢測 demo 變數
|
| 184 |
-
print("🎉 SignView2.0 手語辨識系統已啟動!")
|
| 185 |
-
|
| 186 |
-
# 直接啟動,不使用條件判斷
|
| 187 |
-
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|