Spaces:
Sleeping
Sleeping
Commit
·
8b41cc3
1
Parent(s):
6484498
Fix Gradio app.py: resolve TypeError and localhost accessibility issues
Browse files- Fixed TypeError: argument of type 'bool' is not iterable
- Added share=True to resolve localhost accessibility
- Improved error handling and model loading
- Enhanced UI with better parameter specifications
- Added path auto-detection for flexible deployment
app.py
CHANGED
|
@@ -2,23 +2,43 @@ 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 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
|
@@ -47,79 +67,111 @@ def process_image(image):
|
|
| 47 |
|
| 48 |
def clear_sequence():
|
| 49 |
"""清除預測序列"""
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
# 創建簡化的 Gradio 介面
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 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 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
|
| 112 |
-
process_btn.click(
|
| 113 |
-
fn=process_image,
|
| 114 |
-
inputs=input_image,
|
| 115 |
-
outputs=[output_image, prediction_text]
|
| 116 |
-
)
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
#
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
+
# 檢查檔案是否存在
|
| 7 |
+
model_path = "tsflow/models/best_model.pt"
|
| 8 |
+
config_path = "tsflow/results/test_results.json"
|
| 9 |
+
|
| 10 |
+
# 如果是在SignView2.0目錄下運行,調整路徑
|
| 11 |
+
if not os.path.exists(model_path):
|
| 12 |
+
model_path = "../tsflow/models/best_model.pt"
|
| 13 |
+
config_path = "../tsflow/results/test_results.json"
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
from realtime_sign_prediction import RealtimeSignPredictor
|
| 17 |
+
|
| 18 |
+
# 初始化預測器
|
| 19 |
+
print("🚀 正在初始化手語辨識系統...")
|
| 20 |
+
predictor = RealtimeSignPredictor(
|
| 21 |
+
model_path=model_path,
|
| 22 |
+
config_path=config_path,
|
| 23 |
+
sequence_length=50,
|
| 24 |
+
use_segmentation=True
|
| 25 |
+
)
|
| 26 |
+
print("✅ 手語辨識系統初始化完成!")
|
| 27 |
+
MODEL_LOADED = True
|
| 28 |
+
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"⚠️ 模型載入失敗: {e}")
|
| 31 |
+
print("🔄 使用模擬模式運行...")
|
| 32 |
+
MODEL_LOADED = False
|
| 33 |
|
| 34 |
def process_image(image):
|
| 35 |
"""處理上傳的影像"""
|
| 36 |
if image is None:
|
| 37 |
return None, "請上傳影像或使用攝像頭拍攝"
|
| 38 |
|
| 39 |
+
if not MODEL_LOADED:
|
| 40 |
+
return image, "⚠️ 模型未載入,無法進行預測\n請檢查模型檔案路徑"
|
| 41 |
+
|
| 42 |
try:
|
| 43 |
# 處理畫面
|
| 44 |
results, keypoints, flow_features = predictor.process_frame(image)
|
|
|
|
| 67 |
|
| 68 |
def clear_sequence():
|
| 69 |
"""清除預測序列"""
|
| 70 |
+
if MODEL_LOADED:
|
| 71 |
+
predictor.keypoint_sequence.clear()
|
| 72 |
+
predictor.flow_sequence.clear()
|
| 73 |
+
return "✅ 已清除預測序列"
|
| 74 |
+
else:
|
| 75 |
+
return "⚠️ 模型未載入"
|
| 76 |
|
| 77 |
# 創建簡化的 Gradio 介面
|
| 78 |
+
def create_interface():
|
| 79 |
+
with gr.Blocks(
|
| 80 |
+
title="SignView2.0 - 手語辨識系統",
|
| 81 |
+
theme=gr.themes.Soft(),
|
| 82 |
+
css="""
|
| 83 |
+
.gradio-container {
|
| 84 |
+
max-width: 1200px !important;
|
| 85 |
+
}
|
| 86 |
+
"""
|
| 87 |
+
) as demo:
|
| 88 |
+
|
| 89 |
+
gr.Markdown("""
|
| 90 |
+
# 🤟 SignView2.0 - 手語辨識系統
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
**支援34種手語詞彙的即時辨識系統,準確率達94.25%**
|
| 93 |
+
|
| 94 |
+
## 📋 支援詞彙
|
| 95 |
+
again, all, apple, bad, bathroom, beautiful, bird, black, blue, book,
|
| 96 |
+
bored, boy, brother, brown, but, computer, cousin, dance, day, deaf,
|
| 97 |
+
doctor, dog, draw, drink, eat, english, family, father, fine, finish,
|
| 98 |
+
fish, forget, friend, girl
|
| 99 |
+
|
| 100 |
+
## 🚀 使用說明
|
| 101 |
+
1. 上傳影像或使用攝像頭拍攝手語動作
|
| 102 |
+
2. 點擊「分析手語」按鈕
|
| 103 |
+
3. 查看辨識結果
|
| 104 |
+
""")
|
| 105 |
+
|
| 106 |
+
with gr.Row():
|
| 107 |
+
with gr.Column():
|
| 108 |
+
# 簡化的影像輸入 - 移除可能導致schema錯誤的複雜參數
|
| 109 |
+
input_image = gr.Image(
|
| 110 |
+
label="影像輸入",
|
| 111 |
+
type="numpy",
|
| 112 |
+
height=300
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
with gr.Row():
|
| 116 |
+
process_btn = gr.Button("🔍 分析手語", variant="primary")
|
| 117 |
+
clear_btn = gr.Button("🗑️ 清除序列", variant="secondary")
|
| 118 |
|
| 119 |
+
with gr.Column():
|
| 120 |
+
# 結果輸出
|
| 121 |
+
output_image = gr.Image(
|
| 122 |
+
label="辨識結果",
|
| 123 |
+
type="numpy",
|
| 124 |
+
height=300
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
prediction_text = gr.Textbox(
|
| 128 |
+
label="預測結果",
|
| 129 |
+
lines=8,
|
| 130 |
+
value="等待影像輸入...",
|
| 131 |
+
interactive=False
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
# 系統資訊
|
| 135 |
+
gr.Markdown("""
|
| 136 |
+
## 📊 系統資訊
|
| 137 |
+
- **模型準確率**: 94.25%
|
| 138 |
+
- **F1分數**: 94.24%
|
| 139 |
+
- **特徵提取**: MediaPipe + 光流
|
| 140 |
+
- **模型架構**: BiLSTM + 注意力機制
|
| 141 |
+
- **背景分割**: MediaPipe Segmentation
|
| 142 |
+
|
| 143 |
+
**開發者**: XiaoBai1221 | **平台**: Hugging Face Spaces
|
| 144 |
+
""")
|
| 145 |
+
|
| 146 |
+
# 事件處理
|
| 147 |
+
process_btn.click(
|
| 148 |
+
fn=process_image,
|
| 149 |
+
inputs=[input_image],
|
| 150 |
+
outputs=[output_image, prediction_text],
|
| 151 |
+
api_name="predict"
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
clear_btn.click(
|
| 155 |
+
fn=clear_sequence,
|
| 156 |
+
inputs=[],
|
| 157 |
+
outputs=[prediction_text],
|
| 158 |
+
api_name="clear"
|
| 159 |
+
)
|
| 160 |
|
| 161 |
+
return demo
|
| 162 |
+
|
| 163 |
+
if __name__ == "__main__":
|
| 164 |
+
# 啟動應用程式
|
| 165 |
+
print("🎉 SignView2.0 手語辨識系統已啟動!")
|
| 166 |
|
| 167 |
+
demo = create_interface()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
+
# 修復錯誤:設定share=True和其他參數
|
| 170 |
+
demo.launch(
|
| 171 |
+
share=True, # 解決localhost accessibility問題
|
| 172 |
+
server_name="0.0.0.0", # 允許外部訪問
|
| 173 |
+
server_port=7860, # 指定端口
|
| 174 |
+
debug=False, # 避免debug模式的schema問題
|
| 175 |
+
show_error=True, # 顯示錯誤訊息
|
| 176 |
+
quiet=False # 顯示啟動訊息
|
| 177 |
+
)
|