Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,95 +5,141 @@ import easyocr
|
|
| 5 |
import numpy as np
|
| 6 |
import re
|
| 7 |
from datetime import datetime
|
|
|
|
| 8 |
|
| 9 |
-
class
|
| 10 |
def __init__(self):
|
| 11 |
-
|
| 12 |
-
self.
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
try:
|
| 17 |
# 確保輸入是PIL Image
|
| 18 |
if isinstance(image, np.ndarray):
|
| 19 |
image = Image.fromarray(image)
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
bottom_right = bbox[2]
|
| 29 |
-
center_y = (top_left[1] + bottom_right[1]) / 2
|
| 30 |
-
center_x = (top_left[0] + bottom_right[0]) / 2
|
| 31 |
-
|
| 32 |
-
all_data.append({
|
| 33 |
-
'識別文字': text.strip(),
|
| 34 |
-
'信心度': round(confidence, 3),
|
| 35 |
-
'中心X': int(center_x),
|
| 36 |
-
'中心Y': int(center_y),
|
| 37 |
-
'左上角X': int(top_left[0]),
|
| 38 |
-
'左上角Y': int(top_left[1]),
|
| 39 |
-
'右下角X': int(bottom_right[0]),
|
| 40 |
-
'右下角Y': int(bottom_right[1]),
|
| 41 |
-
'寬度': int(bottom_right[0] - top_left[0]),
|
| 42 |
-
'高度': int(bottom_right[1] - top_left[1]),
|
| 43 |
-
'文字類型': self.classify_text_detailed(text)
|
| 44 |
-
})
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
df = df.sort_values(['中心Y', '中心X']).reset_index(drop=True)
|
| 52 |
|
| 53 |
-
# 嘗試重建表格
|
| 54 |
-
table_df = self.
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
return df, table_df,
|
| 57 |
else:
|
| 58 |
-
|
| 59 |
-
return df, empty_table, "未識別到任何文字"
|
| 60 |
|
| 61 |
except Exception as e:
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
def
|
| 66 |
-
"""
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
def
|
| 90 |
-
"""
|
| 91 |
if df.empty:
|
| 92 |
return pd.DataFrame()
|
| 93 |
|
| 94 |
try:
|
| 95 |
-
#
|
| 96 |
-
y_threshold =
|
| 97 |
rows = []
|
| 98 |
current_row = []
|
| 99 |
last_y = None
|
|
@@ -105,204 +151,210 @@ class TableOCRProcessor:
|
|
| 105 |
current_row.append(row)
|
| 106 |
else:
|
| 107 |
if current_row:
|
| 108 |
-
# 按X座標排序當前行
|
| 109 |
current_row.sort(key=lambda x: x['中心X'])
|
| 110 |
rows.append(current_row)
|
| 111 |
current_row = [row]
|
| 112 |
|
| 113 |
last_y = current_y
|
| 114 |
|
| 115 |
-
# 添加最後一行
|
| 116 |
if current_row:
|
| 117 |
current_row.sort(key=lambda x: x['中心X'])
|
| 118 |
rows.append(current_row)
|
| 119 |
|
| 120 |
# 構建表格
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
| 122 |
table_data = []
|
| 123 |
|
| 124 |
for i, row in enumerate(rows):
|
| 125 |
-
row_data = {'行
|
| 126 |
for j, cell in enumerate(row):
|
| 127 |
-
|
| 128 |
-
row_data[col_name] = cell['識別文字']
|
| 129 |
|
| 130 |
# 填充空列
|
| 131 |
for j in range(len(row), max_cols):
|
| 132 |
-
|
| 133 |
-
row_data[col_name] = ''
|
| 134 |
|
| 135 |
table_data.append(row_data)
|
| 136 |
|
| 137 |
return pd.DataFrame(table_data)
|
| 138 |
|
| 139 |
except Exception as e:
|
| 140 |
-
print(f"重建表格
|
| 141 |
-
return pd.DataFrame()
|
| 142 |
|
| 143 |
-
def
|
| 144 |
-
"""創建
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
📝 文字類型分布:
|
| 157 |
"""
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
summary += f"• {text_type}: {count} 個 ({percentage:.1f}%)\n"
|
| 163 |
-
|
| 164 |
-
# 添加品質評估
|
| 165 |
-
high_confidence = (df['信心度'] >= 0.8).sum()
|
| 166 |
-
medium_confidence = ((df['信心度'] >= 0.6) & (df['信心度'] < 0.8)).sum()
|
| 167 |
-
low_confidence = (df['信心度'] < 0.6).sum()
|
| 168 |
-
|
| 169 |
-
summary += f"""
|
| 170 |
-
🎯 識別品質評估:
|
| 171 |
-
• 高信心度 (≥0.8): {high_confidence} 個
|
| 172 |
-
• 中信心度 (0.6-0.8): {medium_confidence} 個
|
| 173 |
-
• 低信心度 (<0.6): {low_confidence} 個
|
| 174 |
-
|
| 175 |
-
💡 處理建議:
|
| 176 |
-
"""
|
| 177 |
-
if low_confidence > 0:
|
| 178 |
-
summary += "• 發現低信心度文字,建議檢查原圖品質\n"
|
| 179 |
-
if df['信心度'].mean() >= 0.8:
|
| 180 |
-
summary += "• 整體識別品質良好✓\n"
|
| 181 |
-
else:
|
| 182 |
-
summary += "• 建議提高圖片解析度或清晰度\n"
|
| 183 |
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
-
def
|
| 187 |
-
"""創建
|
| 188 |
-
processor =
|
| 189 |
|
| 190 |
-
def
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
-
# 創建
|
| 205 |
-
with gr.Blocks(title="
|
| 206 |
gr.HTML("""
|
| 207 |
-
<div style="text-align: center; padding: 20px;">
|
| 208 |
-
<h1>
|
| 209 |
-
<p
|
| 210 |
-
專為表格和結構化文檔設計的OCR識別工具
|
| 211 |
-
</p>
|
| 212 |
</div>
|
| 213 |
""")
|
| 214 |
|
| 215 |
with gr.Row():
|
| 216 |
-
with gr.Column(scale=
|
| 217 |
image_input = gr.Image(
|
| 218 |
-
label="📤 上傳
|
| 219 |
type="pil",
|
| 220 |
-
height=
|
| 221 |
)
|
| 222 |
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
)
|
| 238 |
|
| 239 |
-
with gr.Column(scale=
|
| 240 |
-
|
| 241 |
-
label="📊 識別
|
| 242 |
-
lines=
|
| 243 |
-
max_lines=
|
| 244 |
)
|
| 245 |
|
| 246 |
-
with gr.
|
| 247 |
-
with gr.
|
| 248 |
-
|
| 249 |
-
|
| 250 |
interactive=True,
|
| 251 |
wrap=True,
|
| 252 |
-
height=
|
| 253 |
)
|
| 254 |
|
| 255 |
-
with gr.
|
| 256 |
-
|
| 257 |
-
|
| 258 |
interactive=True,
|
| 259 |
wrap=True,
|
| 260 |
-
height=
|
| 261 |
)
|
| 262 |
|
| 263 |
-
#
|
| 264 |
-
with gr.Accordion("
|
| 265 |
gr.Markdown("""
|
| 266 |
-
###
|
| 267 |
-
|
| 268 |
-
**
|
| 269 |
-
-
|
| 270 |
-
-
|
| 271 |
-
|
| 272 |
-
**重建表格**:
|
| 273 |
-
- 嘗試根據文字位置重建原始表格結構
|
| 274 |
-
- 按行列組織數據,便於進一步處理
|
| 275 |
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
|
|
|
|
|
|
| 279 |
|
| 280 |
-
###
|
| 281 |
-
-
|
| 282 |
-
-
|
| 283 |
-
-
|
|
|
|
| 284 |
""")
|
| 285 |
|
| 286 |
# 綁定事件
|
| 287 |
process_btn.click(
|
| 288 |
-
fn=
|
| 289 |
-
inputs=[image_input,
|
| 290 |
-
outputs=[
|
| 291 |
)
|
| 292 |
|
| 293 |
image_input.change(
|
| 294 |
-
fn=
|
| 295 |
-
inputs=[image_input,
|
| 296 |
-
outputs=[
|
| 297 |
)
|
| 298 |
|
| 299 |
return demo
|
| 300 |
|
| 301 |
-
# 創建並啟動應用
|
| 302 |
if __name__ == "__main__":
|
| 303 |
-
demo =
|
| 304 |
demo.launch(
|
| 305 |
server_name="0.0.0.0",
|
| 306 |
server_port=7860,
|
| 307 |
-
share=True
|
|
|
|
| 308 |
)
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import re
|
| 7 |
from datetime import datetime
|
| 8 |
+
import traceback
|
| 9 |
|
| 10 |
+
class StableOCRProcessor:
|
| 11 |
def __init__(self):
|
| 12 |
+
self.reader = None
|
| 13 |
+
self.initialize_reader()
|
| 14 |
|
| 15 |
+
def initialize_reader(self):
|
| 16 |
+
"""安全初始化EasyOCR"""
|
| 17 |
+
try:
|
| 18 |
+
# 首先嘗試繁體中文+英文
|
| 19 |
+
self.reader = easyocr.Reader(['ch_tra', 'en'], gpu=False)
|
| 20 |
+
self.lang_config = "繁體中文 + 英文"
|
| 21 |
+
print("成功初始化:繁體中文 + 英文")
|
| 22 |
+
except Exception as e1:
|
| 23 |
+
try:
|
| 24 |
+
# 如果失敗,嘗試簡體中文+英文
|
| 25 |
+
self.reader = easyocr.Reader(['ch_sim', 'en'], gpu=False)
|
| 26 |
+
self.lang_config = "簡體中文 + 英文"
|
| 27 |
+
print("成功初始化:簡體中文 + 英文")
|
| 28 |
+
except Exception as e2:
|
| 29 |
+
try:
|
| 30 |
+
# 如果還是失敗,只使用英文
|
| 31 |
+
self.reader = easyocr.Reader(['en'], gpu=False)
|
| 32 |
+
self.lang_config = "僅英文"
|
| 33 |
+
print("成功初始化:僅英文")
|
| 34 |
+
except Exception as e3:
|
| 35 |
+
print(f"初始化失敗:{e3}")
|
| 36 |
+
self.reader = None
|
| 37 |
+
self.lang_config = "初始化失敗"
|
| 38 |
+
|
| 39 |
+
def process_image_safely(self, image):
|
| 40 |
+
"""安全處理圖片"""
|
| 41 |
+
if self.reader is None:
|
| 42 |
+
return self.create_error_response("OCR引擎初始化失敗")
|
| 43 |
+
|
| 44 |
try:
|
| 45 |
# 確保輸入是PIL Image
|
| 46 |
if isinstance(image, np.ndarray):
|
| 47 |
image = Image.fromarray(image)
|
| 48 |
+
elif image is None:
|
| 49 |
+
return self.create_error_response("請上傳有效的圖片")
|
| 50 |
|
| 51 |
+
# 轉換為numpy array
|
| 52 |
+
img_array = np.array(image)
|
| 53 |
|
| 54 |
+
# 執行OCR
|
| 55 |
+
print("開始執行OCR...")
|
| 56 |
+
results = self.reader.readtext(img_array)
|
| 57 |
+
print(f"OCR完成,識別到 {len(results)} 個文字區塊")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
# 處理結果
|
| 60 |
+
processed_data = []
|
| 61 |
+
for i, (bbox, text, confidence) in enumerate(results):
|
| 62 |
+
try:
|
| 63 |
+
top_left = bbox[0]
|
| 64 |
+
bottom_right = bbox[2]
|
| 65 |
+
center_y = (top_left[1] + bottom_right[1]) / 2
|
| 66 |
+
center_x = (top_left[0] + bottom_right[0]) / 2
|
| 67 |
+
|
| 68 |
+
processed_data.append({
|
| 69 |
+
'序號': i + 1,
|
| 70 |
+
'識別文字': text.strip(),
|
| 71 |
+
'信心度': round(float(confidence), 3),
|
| 72 |
+
'中心X': int(center_x),
|
| 73 |
+
'中心Y': int(center_y),
|
| 74 |
+
'左上角X': int(top_left[0]),
|
| 75 |
+
'左上角Y': int(top_left[1]),
|
| 76 |
+
'右下角X': int(bottom_right[0]),
|
| 77 |
+
'右下角Y': int(bottom_right[1]),
|
| 78 |
+
'文字類型': self.classify_text(text.strip())
|
| 79 |
+
})
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print(f"處理第{i+1}個結果時出錯:{e}")
|
| 82 |
+
continue
|
| 83 |
|
| 84 |
+
# 創建DataFrame
|
| 85 |
+
if processed_data:
|
| 86 |
+
df = pd.DataFrame(processed_data)
|
| 87 |
+
# 按位置排序
|
| 88 |
df = df.sort_values(['中心Y', '中心X']).reset_index(drop=True)
|
| 89 |
|
| 90 |
+
# 嘗試重建表格
|
| 91 |
+
table_df = self.rebuild_table(df)
|
| 92 |
+
|
| 93 |
+
# 創建摘要
|
| 94 |
+
summary = self.create_summary(df)
|
| 95 |
|
| 96 |
+
return df, table_df, summary
|
| 97 |
else:
|
| 98 |
+
return self.create_empty_response("未識別到任何文字")
|
|
|
|
| 99 |
|
| 100 |
except Exception as e:
|
| 101 |
+
error_msg = f"處理圖片時發生錯誤:{str(e)}"
|
| 102 |
+
print(error_msg)
|
| 103 |
+
print(traceback.format_exc())
|
| 104 |
+
return self.create_error_response(error_msg)
|
| 105 |
|
| 106 |
+
def classify_text(self, text):
|
| 107 |
+
"""分類文字類型"""
|
| 108 |
+
if not text:
|
| 109 |
+
return '空白'
|
| 110 |
+
|
| 111 |
+
try:
|
| 112 |
+
# 日期格式 (如: 5月26日)
|
| 113 |
+
if re.search(r'\d+月\d+日', text):
|
| 114 |
+
return '日期'
|
| 115 |
+
# 時間範圍 (如: 07-15)
|
| 116 |
+
elif re.search(r'\d{2}-\d{2}', text):
|
| 117 |
+
return '時間'
|
| 118 |
+
# 純數字
|
| 119 |
+
elif re.match(r'^\d+$', text):
|
| 120 |
+
return '數字'
|
| 121 |
+
# 中文姓名或地名
|
| 122 |
+
elif re.match(r'^[\u4e00-\u9fff]{2,4}$', text):
|
| 123 |
+
return '中文名稱'
|
| 124 |
+
# 包含中文
|
| 125 |
+
elif re.search(r'[\u4e00-\u9fff]', text):
|
| 126 |
+
return '中文內容'
|
| 127 |
+
# 英文字母
|
| 128 |
+
elif re.match(r'^[a-zA-Z]+$', text):
|
| 129 |
+
return '英文'
|
| 130 |
+
else:
|
| 131 |
+
return '其他'
|
| 132 |
+
except Exception:
|
| 133 |
+
return '未知'
|
| 134 |
|
| 135 |
+
def rebuild_table(self, df):
|
| 136 |
+
"""重建表格結構"""
|
| 137 |
if df.empty:
|
| 138 |
return pd.DataFrame()
|
| 139 |
|
| 140 |
try:
|
| 141 |
+
# 按Y座標分組
|
| 142 |
+
y_threshold = 30
|
| 143 |
rows = []
|
| 144 |
current_row = []
|
| 145 |
last_y = None
|
|
|
|
| 151 |
current_row.append(row)
|
| 152 |
else:
|
| 153 |
if current_row:
|
|
|
|
| 154 |
current_row.sort(key=lambda x: x['中心X'])
|
| 155 |
rows.append(current_row)
|
| 156 |
current_row = [row]
|
| 157 |
|
| 158 |
last_y = current_y
|
| 159 |
|
|
|
|
| 160 |
if current_row:
|
| 161 |
current_row.sort(key=lambda x: x['中心X'])
|
| 162 |
rows.append(current_row)
|
| 163 |
|
| 164 |
# 構建表格
|
| 165 |
+
if not rows:
|
| 166 |
+
return pd.DataFrame()
|
| 167 |
+
|
| 168 |
+
max_cols = max(len(row) for row in rows)
|
| 169 |
table_data = []
|
| 170 |
|
| 171 |
for i, row in enumerate(rows):
|
| 172 |
+
row_data = {'行': i + 1}
|
| 173 |
for j, cell in enumerate(row):
|
| 174 |
+
row_data[f'列{j+1}'] = cell['識別文字']
|
|
|
|
| 175 |
|
| 176 |
# 填充空列
|
| 177 |
for j in range(len(row), max_cols):
|
| 178 |
+
row_data[f'列{j+1}'] = ''
|
|
|
|
| 179 |
|
| 180 |
table_data.append(row_data)
|
| 181 |
|
| 182 |
return pd.DataFrame(table_data)
|
| 183 |
|
| 184 |
except Exception as e:
|
| 185 |
+
print(f"重建表格失敗:{e}")
|
| 186 |
+
return pd.DataFrame([{'錯誤': '表格重建失敗'}])
|
| 187 |
|
| 188 |
+
def create_summary(self, df):
|
| 189 |
+
"""創建摘要報告"""
|
| 190 |
+
try:
|
| 191 |
+
if df.empty:
|
| 192 |
+
return "未識別到任何內容"
|
| 193 |
+
|
| 194 |
+
avg_confidence = df['信心度'].mean()
|
| 195 |
+
high_conf = (df['信心度'] >= 0.8).sum()
|
| 196 |
+
total_count = len(df)
|
| 197 |
+
|
| 198 |
+
summary = f"""
|
| 199 |
+
🔍 OCR識別報告
|
| 200 |
+
═══════════════════════════════════
|
| 201 |
+
📊 基本統計:
|
| 202 |
+
• 語言配置:{self.lang_config}
|
| 203 |
+
• 識別區塊:{total_count} 個
|
| 204 |
+
• 平均信心度:{avg_confidence:.3f}
|
| 205 |
+
• 高信心度區塊:{high_conf} 個 ({high_conf/total_count*100:.1f}%)
|
| 206 |
|
| 207 |
📝 文字類型分布:
|
| 208 |
"""
|
| 209 |
+
type_counts = df['文字類型'].value_counts()
|
| 210 |
+
for text_type, count in type_counts.items():
|
| 211 |
+
percentage = (count / total_count) * 100
|
| 212 |
+
summary += f"• {text_type}:{count} 個 ({percentage:.1f}%)\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
+
# 品質評估
|
| 215 |
+
if avg_confidence >= 0.8:
|
| 216 |
+
summary += "\n✅ 識別品質:優秀"
|
| 217 |
+
elif avg_confidence >= 0.6:
|
| 218 |
+
summary += "\n⚠️ 識別品質:良好"
|
| 219 |
+
else:
|
| 220 |
+
summary += "\n❌ 識別品質:需改進"
|
| 221 |
+
|
| 222 |
+
return summary
|
| 223 |
+
|
| 224 |
+
except Exception as e:
|
| 225 |
+
return f"生成報告時發生錯誤:{str(e)}"
|
| 226 |
+
|
| 227 |
+
def create_error_response(self, error_msg):
|
| 228 |
+
"""創建錯誤響應"""
|
| 229 |
+
error_df = pd.DataFrame([{'錯誤': error_msg}])
|
| 230 |
+
return error_df, error_df, f"❌ 錯誤:{error_msg}"
|
| 231 |
+
|
| 232 |
+
def create_empty_response(self, msg):
|
| 233 |
+
"""創建空響應"""
|
| 234 |
+
empty_df = pd.DataFrame()
|
| 235 |
+
return empty_df, empty_df, f"ℹ️ {msg}"
|
| 236 |
|
| 237 |
+
def create_stable_interface():
|
| 238 |
+
"""創建穩定的Gradio界面"""
|
| 239 |
+
processor = StableOCRProcessor()
|
| 240 |
|
| 241 |
+
def process_with_error_handling(image, min_confidence):
|
| 242 |
+
"""帶錯誤處理的處理函數"""
|
| 243 |
+
try:
|
| 244 |
+
df, table_df, summary = processor.process_image_safely(image)
|
| 245 |
+
|
| 246 |
+
# 應用信心度過濾
|
| 247 |
+
if not df.empty and '信心度' in df.columns:
|
| 248 |
+
filtered_df = df[df['信心度'] >= min_confidence].reset_index(drop=True)
|
| 249 |
+
if len(filtered_df) != len(df):
|
| 250 |
+
summary += f"\n\n🔍 已過濾低信心度結果:{len(df) - len(filtered_df)} 個"
|
| 251 |
+
df = filtered_df
|
| 252 |
+
|
| 253 |
+
return summary, df, table_df
|
| 254 |
+
|
| 255 |
+
except Exception as e:
|
| 256 |
+
error_msg = f"處��失敗:{str(e)}"
|
| 257 |
+
error_df = pd.DataFrame([{'錯誤': error_msg}])
|
| 258 |
+
return error_msg, error_df, error_df
|
| 259 |
|
| 260 |
+
# 創建界面
|
| 261 |
+
with gr.Blocks(title="穩定版中文OCR系統", theme=gr.themes.Soft()) as demo:
|
| 262 |
gr.HTML("""
|
| 263 |
+
<div style="text-align: center; padding: 20px; background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin-bottom: 20px;">
|
| 264 |
+
<h1>🔍 穩定版中文OCR識別系統</h1>
|
| 265 |
+
<p>支持中文表格和文檔的OCR識別,自動容錯處理</p>
|
|
|
|
|
|
|
| 266 |
</div>
|
| 267 |
""")
|
| 268 |
|
| 269 |
with gr.Row():
|
| 270 |
+
with gr.Column(scale=1):
|
| 271 |
image_input = gr.Image(
|
| 272 |
+
label="📤 上傳圖片",
|
| 273 |
type="pil",
|
| 274 |
+
height=400
|
| 275 |
)
|
| 276 |
|
| 277 |
+
min_confidence = gr.Slider(
|
| 278 |
+
minimum=0.0,
|
| 279 |
+
maximum=1.0,
|
| 280 |
+
value=0.3,
|
| 281 |
+
step=0.1,
|
| 282 |
+
label="🎯 最低信心度",
|
| 283 |
+
info="過濾掉信心度低於此值的結果"
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
process_btn = gr.Button(
|
| 287 |
+
"🚀 開始識別",
|
| 288 |
+
variant="primary",
|
| 289 |
+
size="lg"
|
| 290 |
+
)
|
|
|
|
| 291 |
|
| 292 |
+
with gr.Column(scale=1):
|
| 293 |
+
summary_text = gr.Textbox(
|
| 294 |
+
label="📊 識別報告",
|
| 295 |
+
lines=12,
|
| 296 |
+
max_lines=15
|
| 297 |
)
|
| 298 |
|
| 299 |
+
with gr.Row():
|
| 300 |
+
with gr.Column():
|
| 301 |
+
gr.Markdown("### 📋 詳細識別結果")
|
| 302 |
+
detail_table = gr.Dataframe(
|
| 303 |
interactive=True,
|
| 304 |
wrap=True,
|
| 305 |
+
height=300
|
| 306 |
)
|
| 307 |
|
| 308 |
+
with gr.Column():
|
| 309 |
+
gr.Markdown("### 🔄 重建表格")
|
| 310 |
+
rebuilt_table = gr.Dataframe(
|
| 311 |
interactive=True,
|
| 312 |
wrap=True,
|
| 313 |
+
height=300
|
| 314 |
)
|
| 315 |
|
| 316 |
+
# 說明區域
|
| 317 |
+
with gr.Accordion("💡 使用說明", open=False):
|
| 318 |
gr.Markdown("""
|
| 319 |
+
### 系統特色
|
| 320 |
+
- ✅ **智能容錯**:自動處理各種錯誤情況
|
| 321 |
+
- 🔧 **多語言支持**:自動選擇最佳語言配置
|
| 322 |
+
- 📊 **表格重建**:智能重建原始表格結構
|
| 323 |
+
- 🎯 **品質控制**:可調整信心度閾值
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
|
| 325 |
+
### 使用技巧
|
| 326 |
+
1. **圖片要求**:清晰、對比度高的圖片效果最佳
|
| 327 |
+
2. **信心度調整**:降低閾值可獲得更多結果,提高閾值可���得更準確結果
|
| 328 |
+
3. **表格處理**:系統會自動識別行列結構並重建表格
|
| 329 |
+
4. **結果導出**:可以複製表格數據到其他應用程序
|
| 330 |
|
| 331 |
+
### 支持內容
|
| 332 |
+
- 中文文字(繁體/簡體)
|
| 333 |
+
- 英文字母和數字
|
| 334 |
+
- 日期和時間格式
|
| 335 |
+
- 表格和結構化數據
|
| 336 |
""")
|
| 337 |
|
| 338 |
# 綁定事件
|
| 339 |
process_btn.click(
|
| 340 |
+
fn=process_with_error_handling,
|
| 341 |
+
inputs=[image_input, min_confidence],
|
| 342 |
+
outputs=[summary_text, detail_table, rebuilt_table]
|
| 343 |
)
|
| 344 |
|
| 345 |
image_input.change(
|
| 346 |
+
fn=process_with_error_handling,
|
| 347 |
+
inputs=[image_input, min_confidence],
|
| 348 |
+
outputs=[summary_text, detail_table, rebuilt_table]
|
| 349 |
)
|
| 350 |
|
| 351 |
return demo
|
| 352 |
|
|
|
|
| 353 |
if __name__ == "__main__":
|
| 354 |
+
demo = create_stable_interface()
|
| 355 |
demo.launch(
|
| 356 |
server_name="0.0.0.0",
|
| 357 |
server_port=7860,
|
| 358 |
+
share=True,
|
| 359 |
+
show_error=True
|
| 360 |
)
|