Spaces:
Sleeping
Sleeping
fix: got_ocr
Browse files
app.py
CHANGED
@@ -75,7 +75,7 @@ def perform_ocr(selected_index, image_gallery, task, fine_grained_type, color, b
|
|
75 |
if selected_index is None or len(image_gallery) == 0:
|
76 |
return "请先选择一张图片"
|
77 |
|
78 |
-
selected_image = image_gallery[selected_index]
|
79 |
|
80 |
# 根据选择的任务和参数调用GOT OCR
|
81 |
got_mode = task
|
@@ -86,19 +86,28 @@ def perform_ocr(selected_index, image_gallery, task, fine_grained_type, color, b
|
|
86 |
return result
|
87 |
|
88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
with gr.Blocks() as demo:
|
90 |
pdf_input = gr.File(label="上传PDF文件")
|
91 |
image_gallery = gr.Gallery(label="PDF页面预览", columns=3, height="auto")
|
92 |
selected_index = gr.State(None)
|
93 |
task_dropdown = gr.Dropdown(
|
94 |
-
choices=[
|
95 |
-
"plain texts OCR",
|
96 |
-
"format texts OCR",
|
97 |
-
"plain multi-crop OCR",
|
98 |
-
"format multi-crop OCR",
|
99 |
-
"plain fine-grained OCR",
|
100 |
-
"format fine-grained OCR",
|
101 |
-
],
|
102 |
label="选择GOT模式",
|
103 |
value="plain texts OCR",
|
104 |
)
|
@@ -106,43 +115,12 @@ with gr.Blocks() as demo:
|
|
106 |
color_dropdown = gr.Dropdown(choices=["red", "green", "blue"], label="颜色列表", visible=False)
|
107 |
box_input = gr.Textbox(label="输入框: [x1,y1,x2,y2]", placeholder="例如: [0,0,100,100]", visible=False)
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
return [
|
112 |
-
gr.update(visible=True),
|
113 |
-
gr.update(visible=False),
|
114 |
-
gr.update(visible=False),
|
115 |
-
]
|
116 |
-
else:
|
117 |
-
return [
|
118 |
-
gr.update(visible=False),
|
119 |
-
gr.update(visible=False),
|
120 |
-
gr.update(visible=False),
|
121 |
-
]
|
122 |
-
|
123 |
-
def fine_grained_update(fine_grained_type):
|
124 |
-
if fine_grained_type == "color":
|
125 |
-
return [
|
126 |
-
gr.update(visible=True),
|
127 |
-
gr.update(visible=False),
|
128 |
-
]
|
129 |
-
elif fine_grained_type == "box":
|
130 |
-
return [
|
131 |
-
gr.update(visible=False),
|
132 |
-
gr.update(visible=True),
|
133 |
-
]
|
134 |
-
else:
|
135 |
-
return [
|
136 |
-
gr.update(visible=False),
|
137 |
-
gr.update(visible=False),
|
138 |
-
]
|
139 |
|
140 |
task_dropdown.change(task_update, inputs=[task_dropdown], outputs=[fine_grained_dropdown, color_dropdown, box_input])
|
141 |
fine_grained_dropdown.change(fine_grained_update, inputs=[fine_grained_dropdown], outputs=[color_dropdown, box_input])
|
142 |
|
143 |
-
ocr_button = gr.Button("开始OCR识别")
|
144 |
-
ocr_result = gr.Textbox(label="OCR结果")
|
145 |
-
|
146 |
# 更新ocr_button的click事件,传递所有必要的参数
|
147 |
ocr_button.click(
|
148 |
fn=perform_ocr, inputs=[selected_index, image_gallery, task_dropdown, fine_grained_dropdown, color_dropdown, box_input], outputs=ocr_result
|
|
|
75 |
if selected_index is None or len(image_gallery) == 0:
|
76 |
return "请先选择一张图片"
|
77 |
|
78 |
+
selected_image = image_gallery[selected_index][0]
|
79 |
|
80 |
# 根据选择的任务和参数调用GOT OCR
|
81 |
got_mode = task
|
|
|
86 |
return result
|
87 |
|
88 |
|
89 |
+
def task_update(task):
|
90 |
+
if "fine-grained" in task:
|
91 |
+
return [gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)]
|
92 |
+
else:
|
93 |
+
return [gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)]
|
94 |
+
|
95 |
+
|
96 |
+
def fine_grained_update(fine_grained_type):
|
97 |
+
if fine_grained_type == "color":
|
98 |
+
return [gr.update(visible=True), gr.update(visible=False)]
|
99 |
+
elif fine_grained_type == "box":
|
100 |
+
return [gr.update(visible=False), gr.update(visible=True)]
|
101 |
+
else:
|
102 |
+
return [gr.update(visible=False), gr.update(visible=False)]
|
103 |
+
|
104 |
+
|
105 |
with gr.Blocks() as demo:
|
106 |
pdf_input = gr.File(label="上传PDF文件")
|
107 |
image_gallery = gr.Gallery(label="PDF页面预览", columns=3, height="auto")
|
108 |
selected_index = gr.State(None)
|
109 |
task_dropdown = gr.Dropdown(
|
110 |
+
choices=["plain texts OCR", "format texts OCR", "plain multi-crop OCR", "format multi-crop OCR", "plain fine-grained OCR", "format fine-grained OCR"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
label="选择GOT模式",
|
112 |
value="plain texts OCR",
|
113 |
)
|
|
|
115 |
color_dropdown = gr.Dropdown(choices=["red", "green", "blue"], label="颜色列表", visible=False)
|
116 |
box_input = gr.Textbox(label="输入框: [x1,y1,x2,y2]", placeholder="例如: [0,0,100,100]", visible=False)
|
117 |
|
118 |
+
ocr_button = gr.Button("开始OCR识别")
|
119 |
+
ocr_result = gr.Textbox(label="OCR结果")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
task_dropdown.change(task_update, inputs=[task_dropdown], outputs=[fine_grained_dropdown, color_dropdown, box_input])
|
122 |
fine_grained_dropdown.change(fine_grained_update, inputs=[fine_grained_dropdown], outputs=[color_dropdown, box_input])
|
123 |
|
|
|
|
|
|
|
124 |
# 更新ocr_button的click事件,传递所有必要的参数
|
125 |
ocr_button.click(
|
126 |
fn=perform_ocr, inputs=[selected_index, image_gallery, task_dropdown, fine_grained_dropdown, color_dropdown, box_input], outputs=ocr_result
|