Spaces:
Sleeping
Sleeping
Commit
·
6484498
1
Parent(s):
86d38f2
重新創建簡化版 app.py - 修復 Gradio 兼容性問題
Browse files
app.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# 初始化預測器
|
| 8 |
+
print("🚀 正在初始化手語辨識系統...")
|
| 9 |
+
predictor = RealtimeSignPredictor(
|
| 10 |
+
model_path="tsflow/models/best_model.pt",
|
| 11 |
+
config_path="tsflow/results/test_results.json",
|
| 12 |
+
sequence_length=50,
|
| 13 |
+
use_segmentation=True
|
| 14 |
+
)
|
| 15 |
+
print("✅ 手語辨識系統初始化完成!")
|
| 16 |
+
|
| 17 |
+
def process_image(image):
|
| 18 |
+
"""處理上傳的影像"""
|
| 19 |
+
if image is None:
|
| 20 |
+
return None, "請上傳影像或使用攝像頭拍攝"
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
# 處理畫面
|
| 24 |
+
results, keypoints, flow_features = predictor.process_frame(image)
|
| 25 |
+
|
| 26 |
+
# 繪製關鍵點
|
| 27 |
+
annotated_frame = predictor.draw_landmarks(image.copy(), results)
|
| 28 |
+
|
| 29 |
+
# 獲取預測結果
|
| 30 |
+
top_predictions = predictor.get_top_predictions(top_k=3)
|
| 31 |
+
|
| 32 |
+
# 格式化預測結果
|
| 33 |
+
if top_predictions:
|
| 34 |
+
prediction_text = "🎯 手語辨識結果:\n\n"
|
| 35 |
+
for i, (label, confidence) in enumerate(top_predictions, 1):
|
| 36 |
+
prediction_text += f"{i}. {label}: {confidence:.2%}\n"
|
| 37 |
+
|
| 38 |
+
# 添加序列資訊
|
| 39 |
+
prediction_text += f"\n📊 序列進度: {len(predictor.keypoint_sequence)}/{predictor.sequence_length}"
|
| 40 |
+
else:
|
| 41 |
+
prediction_text = "📡 正在收集動作序列...\n請確保手語動作清晰可見"
|
| 42 |
+
|
| 43 |
+
return annotated_frame, prediction_text
|
| 44 |
+
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return image, f"❌ 處理錯誤: {str(e)}"
|
| 47 |
+
|
| 48 |
+
def clear_sequence():
|
| 49 |
+
"""清除預測序列"""
|
| 50 |
+
predictor.keypoint_sequence.clear()
|
| 51 |
+
predictor.flow_sequence.clear()
|
| 52 |
+
return "✅ 已清除預測序列"
|
| 53 |
+
|
| 54 |
+
# 創建簡化的 Gradio 介面
|
| 55 |
+
with gr.Blocks(title="SignView2.0 - 手語辨識系統") as demo:
|
| 56 |
+
|
| 57 |
+
gr.Markdown("""
|
| 58 |
+
# 🤟 SignView2.0 - 手語辨識系統
|
| 59 |
+
|
| 60 |
+
**支援34種手語詞彙的即時辨識系統,準確率達94.25%**
|
| 61 |
+
|
| 62 |
+
## 📋 支援詞彙
|
| 63 |
+
again, all, apple, bad, bathroom, beautiful, bird, black, blue, book,
|
| 64 |
+
bored, boy, brother, brown, but, computer, cousin, dance, day, deaf,
|
| 65 |
+
doctor, dog, draw, drink, eat, english, family, father, fine, finish,
|
| 66 |
+
fish, forget, friend, girl
|
| 67 |
+
|
| 68 |
+
## 🚀 使用說明
|
| 69 |
+
1. 上傳影像或使用攝像頭拍攝手語動作
|
| 70 |
+
2. 點擊「分析手語」按鈕
|
| 71 |
+
3. 查看辨識結果
|
| 72 |
+
""")
|
| 73 |
+
|
| 74 |
+
with gr.Row():
|
| 75 |
+
with gr.Column():
|
| 76 |
+
# 簡化的影像輸入
|
| 77 |
+
input_image = gr.Image(
|
| 78 |
+
label="影像輸入",
|
| 79 |
+
height=300
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
with gr.Row():
|
| 83 |
+
process_btn = gr.Button("🔍 分析手語", variant="primary")
|
| 84 |
+
clear_btn = gr.Button("🗑️ 清除序列", variant="secondary")
|
| 85 |
+
|
| 86 |
+
with gr.Column():
|
| 87 |
+
# 結果輸出
|
| 88 |
+
output_image = gr.Image(
|
| 89 |
+
label="辨識結果",
|
| 90 |
+
height=300
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
prediction_text = gr.Textbox(
|
| 94 |
+
label="預測結果",
|
| 95 |
+
lines=8,
|
| 96 |
+
value="等待影像輸入..."
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# 系統資訊
|
| 100 |
+
gr.Markdown("""
|
| 101 |
+
## 📊 系統資訊
|
| 102 |
+
- **模型準確率**: 94.25%
|
| 103 |
+
- **F1分數**: 94.24%
|
| 104 |
+
- **特徵提取**: MediaPipe + 光流
|
| 105 |
+
- **模型架構**: BiLSTM + 注意力機制
|
| 106 |
+
- **背景分割**: MediaPipe Segmentation
|
| 107 |
+
|
| 108 |
+
**開發者**: XiaoBai1221 | **平台**: Hugging Face Spaces
|
| 109 |
+
""")
|
| 110 |
+
|
| 111 |
+
# 事件處理
|
| 112 |
+
process_btn.click(
|
| 113 |
+
fn=process_image,
|
| 114 |
+
inputs=input_image,
|
| 115 |
+
outputs=[output_image, prediction_text]
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
clear_btn.click(
|
| 119 |
+
fn=clear_sequence,
|
| 120 |
+
outputs=prediction_text
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
# 啟動應用程式 - 不使用 share 參數
|
| 124 |
+
print("🎉 SignView2.0 手語辨識系統已啟動!")
|
| 125 |
+
demo.launch()
|