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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 介面
|
| 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 |
+
4. 點擊「清除預測」重新開始
|
| 101 |
+
""", elem_classes=["header"])
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
with gr.Column(scale=2):
|
| 105 |
+
# 視訊輸入
|
| 106 |
+
video_input = gr.Video(
|
| 107 |
+
label="📹 攝像頭輸入",
|
| 108 |
+
sources=["webcam"],
|
| 109 |
+
streaming=True,
|
| 110 |
+
height=400
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
# 清除按鈕
|
| 114 |
+
clear_btn = gr.Button(
|
| 115 |
+
"🗑️ 清除預測序列",
|
| 116 |
+
variant="secondary",
|
| 117 |
+
size="lg"
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
with gr.Column(scale=1):
|
| 121 |
+
# 預測輸出
|
| 122 |
+
prediction_output = gr.Textbox(
|
| 123 |
+
label="🎯 辨識結果",
|
| 124 |
+
lines=12,
|
| 125 |
+
value="等待攝像頭輸入...",
|
| 126 |
+
elem_classes=["prediction-box"]
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
# 系統資訊
|
| 130 |
+
gr.Markdown("""
|
| 131 |
+
## 📊 系統資訊
|
| 132 |
+
- **模型準確率**: 94.25%
|
| 133 |
+
- **F1分數**: 94.24%
|
| 134 |
+
- **處理速度**: 30 FPS
|
| 135 |
+
- **特徵提取**: MediaPipe + 光流
|
| 136 |
+
- **模型架構**: BiLSTM + 注意力機制
|
| 137 |
+
- **背景分割**: MediaPipe Segmentation
|
| 138 |
+
|
| 139 |
+
## 🔧 技術特色
|
| 140 |
+
- MediaPipe Holistic 關鍵點���測
|
| 141 |
+
- 光流動作特徵捕捉
|
| 142 |
+
- 人體分割背景去除
|
| 143 |
+
- 深度學習時序建模
|
| 144 |
+
""")
|
| 145 |
+
|
| 146 |
+
# 事件處理器
|
| 147 |
+
video_input.stream(
|
| 148 |
+
fn=process_video_frame,
|
| 149 |
+
inputs=[video_input],
|
| 150 |
+
outputs=[video_input, prediction_output],
|
| 151 |
+
stream_every=0.1,
|
| 152 |
+
show_progress=False
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
clear_btn.click(
|
| 156 |
+
fn=clear_predictions,
|
| 157 |
+
outputs=[prediction_output]
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
gr.Markdown("""
|
| 161 |
+
---
|
| 162 |
+
### 📈 關於此系統
|
| 163 |
+
|
| 164 |
+
SignView2.0 使用最先進的深度學習技術進行手語辨識:
|
| 165 |
+
|
| 166 |
+
- **特徵提取**: 使用 MediaPipe 提取手部、身體關鍵點 + 光流特徵
|
| 167 |
+
- **背景處理**: MediaPipe Segmentation 自動去除背景干擾
|
| 168 |
+
- **時序建模**: 雙向LSTM + GRU + 多頭注意力機制
|
| 169 |
+
- **訓練資料**: 2380個手語視頻,經過數據增強和正規化
|
| 170 |
+
|
| 171 |
+
**開發者**: XiaoBai1221 | **平台**: Hugging Face Spaces
|
| 172 |
+
""")
|
| 173 |
+
|
| 174 |
+
# 啟動應用程式 - Hugging Face Spaces 會自動檢測 demo 變數
|
| 175 |
+
print("🎉 SignView2.0 手語辨識系統已啟動!")
|
| 176 |
+
|
| 177 |
+
# 直接啟動,不使用條件判斷
|
| 178 |
+
demo.launch()
|