Spaces:
Runtime error
Runtime error
upadate model
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import threading
|
| 2 |
import time
|
| 3 |
|
| 4 |
import cv2
|
|
@@ -11,6 +10,7 @@ from table_cls import TableCls
|
|
| 11 |
from wired_table_rec import WiredTableRecognition
|
| 12 |
|
| 13 |
from utils import plot_rec_box, LoadImage, format_html, box_4_2_poly_to_box_4_1
|
|
|
|
| 14 |
img_loader = LoadImage()
|
| 15 |
table_rec_path = "models/table_rec/ch_ppstructure_mobile_v2_SLANet.onnx"
|
| 16 |
det_model_dir = {
|
|
@@ -39,8 +39,9 @@ example_images = [
|
|
| 39 |
"images/wired4.jpg",
|
| 40 |
"images/lineless2.png",
|
| 41 |
"images/wired5.jpg",
|
| 42 |
-
"images/
|
| 43 |
-
"images/
|
|
|
|
| 44 |
]
|
| 45 |
rapid_table_engine = RapidTable(model_path=table_rec_path)
|
| 46 |
SLANet_plus_table_Engine = RapidTable()
|
|
@@ -93,7 +94,7 @@ def select_table_model(img, table_engine_type, det_model, rec_model):
|
|
| 93 |
return lineless_table_engine, "lineless_table"
|
| 94 |
|
| 95 |
|
| 96 |
-
def process_image(img, table_engine_type, det_model, rec_model):
|
| 97 |
img = img_loader(img)
|
| 98 |
start = time.time()
|
| 99 |
table_engine, talbe_type = select_table_model(img, table_engine_type, det_model, rec_model)
|
|
@@ -115,6 +116,15 @@ def process_image(img, table_engine_type, det_model, rec_model):
|
|
| 115 |
polygons = [[polygon[0], polygon[1], polygon[4], polygon[5]] for polygon in polygons]
|
| 116 |
elif isinstance(table_engine, (WiredTableRecognition, LinelessTableRecognition)):
|
| 117 |
html, table_rec_elapse, polygons, _, _ = table_engine(img, ocr_result=ocr_res)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
sum_elapse = time.time() - start
|
| 120 |
all_elapse = f"- table_type: {talbe_type}\n table all cost: {sum_elapse:.5f}\n - table rec cost: {table_rec_elapse:.5f}\n - ocr cost: {det_cost + cls_cost + rec_cost:.5f}"
|
|
@@ -136,23 +146,51 @@ def main():
|
|
| 136 |
overflow-x: auto;
|
| 137 |
white-space: nowrap;
|
| 138 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
""") as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
with gr.Row(): # 两列布局
|
| 141 |
with gr.Tab("Options"):
|
| 142 |
with gr.Column(variant="panel", scale=1): # 侧边栏,宽度比例为1
|
| 143 |
-
img_input = gr.Image(label="Upload or Select Image",
|
| 144 |
|
| 145 |
# 示例图片选择器
|
| 146 |
examples = gr.Examples(
|
| 147 |
examples=example_images,
|
|
|
|
| 148 |
inputs=img_input,
|
| 149 |
fn=lambda x: x, # 简单返回图片路径
|
| 150 |
outputs=img_input,
|
| 151 |
-
cache_examples=
|
| 152 |
)
|
| 153 |
|
| 154 |
table_engine_type = gr.Dropdown(table_engine_list, label="Select Recognition Table Engine",
|
| 155 |
value=table_engine_list[0])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
det_model = gr.Dropdown(det_models_labels, label="Select OCR Detection Model",
|
| 157 |
value=det_models_labels[0])
|
| 158 |
rec_model = gr.Dropdown(rec_models_labels, label="Select OCR Recognition Model",
|
|
@@ -172,7 +210,7 @@ def main():
|
|
| 172 |
|
| 173 |
run_button.click(
|
| 174 |
fn=process_image,
|
| 175 |
-
inputs=[img_input, table_engine_type, det_model, rec_model],
|
| 176 |
outputs=[html_output, table_boxes_output, ocr_boxes_output, elapse_text]
|
| 177 |
)
|
| 178 |
|
|
@@ -180,4 +218,4 @@ def main():
|
|
| 180 |
|
| 181 |
|
| 182 |
if __name__ == '__main__':
|
| 183 |
-
main()
|
|
|
|
|
|
|
| 1 |
import time
|
| 2 |
|
| 3 |
import cv2
|
|
|
|
| 10 |
from wired_table_rec import WiredTableRecognition
|
| 11 |
|
| 12 |
from utils import plot_rec_box, LoadImage, format_html, box_4_2_poly_to_box_4_1
|
| 13 |
+
|
| 14 |
img_loader = LoadImage()
|
| 15 |
table_rec_path = "models/table_rec/ch_ppstructure_mobile_v2_SLANet.onnx"
|
| 16 |
det_model_dir = {
|
|
|
|
| 39 |
"images/wired4.jpg",
|
| 40 |
"images/lineless2.png",
|
| 41 |
"images/wired5.jpg",
|
| 42 |
+
"images/lineless4.jpg",
|
| 43 |
+
"images/wired7.jpg",
|
| 44 |
+
"images/wired9.jpg",
|
| 45 |
]
|
| 46 |
rapid_table_engine = RapidTable(model_path=table_rec_path)
|
| 47 |
SLANet_plus_table_Engine = RapidTable()
|
|
|
|
| 94 |
return lineless_table_engine, "lineless_table"
|
| 95 |
|
| 96 |
|
| 97 |
+
def process_image(img, table_engine_type, det_model, rec_model, small_box_cut_enhance):
|
| 98 |
img = img_loader(img)
|
| 99 |
start = time.time()
|
| 100 |
table_engine, talbe_type = select_table_model(img, table_engine_type, det_model, rec_model)
|
|
|
|
| 116 |
polygons = [[polygon[0], polygon[1], polygon[4], polygon[5]] for polygon in polygons]
|
| 117 |
elif isinstance(table_engine, (WiredTableRecognition, LinelessTableRecognition)):
|
| 118 |
html, table_rec_elapse, polygons, _, _ = table_engine(img, ocr_result=ocr_res)
|
| 119 |
+
if not small_box_cut_enhance:
|
| 120 |
+
html, table_rec_elapse, polygons, logic_points, ocr_res = table_engine(
|
| 121 |
+
img, ocr_result=ocr_res,
|
| 122 |
+
morph_close=False, more_h_lines=False, more_v_lines=False, extend_line=False
|
| 123 |
+
)
|
| 124 |
+
else:
|
| 125 |
+
html, table_rec_elapse, polygons, logic_points, ocr_res = table_engine(
|
| 126 |
+
img, ocr_result=ocr_res
|
| 127 |
+
)
|
| 128 |
|
| 129 |
sum_elapse = time.time() - start
|
| 130 |
all_elapse = f"- table_type: {talbe_type}\n table all cost: {sum_elapse:.5f}\n - table rec cost: {table_rec_elapse:.5f}\n - ocr cost: {det_cost + cls_cost + rec_cost:.5f}"
|
|
|
|
| 146 |
overflow-x: auto;
|
| 147 |
white-space: nowrap;
|
| 148 |
}
|
| 149 |
+
.header-links {
|
| 150 |
+
text-align: center;
|
| 151 |
+
}
|
| 152 |
+
.header-links a {
|
| 153 |
+
display: inline-block;
|
| 154 |
+
text-align: center;
|
| 155 |
+
margin-right: 10px; /* 调整间距 */
|
| 156 |
+
}
|
| 157 |
""") as demo:
|
| 158 |
+
gr.HTML(
|
| 159 |
+
"<h1 style='text-align: center;'><a href='https://github.com/RapidAI/TableStructureRec?tab=readme-ov-file'>TableStructureRec</a></h1>"
|
| 160 |
+
)
|
| 161 |
+
gr.HTML('''
|
| 162 |
+
<div class="header-links">
|
| 163 |
+
<a href=""><img src="https://img.shields.io/badge/Python->=3.6,<3.12-aff.svg"></a>
|
| 164 |
+
<a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Mac%2C%20Win-pink.svg"></a>
|
| 165 |
+
<a href="https://pypi.org/project/lineless-table-rec/"><img alt="PyPI" src="https://img.shields.io/pypi/v/lineless-table-rec"></a>
|
| 166 |
+
<a href="https://pepy.tech/project/lineless-table-rec"><img src="https://static.pepy.tech/personalized-badge/lineless-table-rec?period=total&units=abbreviation&left_color=grey&right_color=blue&left_text=Downloads%20Lineless"></a>
|
| 167 |
+
<a href="https://pepy.tech/project/wired-table-rec"><img src="https://static.pepy.tech/personalized-badge/wired-table-rec?period=total&units=abbreviation&left_color=grey&right_color=blue&left_text=Downloads%20Wired"></a>
|
| 168 |
+
<a href="https://semver.org/"><img alt="SemVer2.0" src="https://img.shields.io/badge/SemVer-2.0-brightgreen"></a>
|
| 169 |
+
<a href="https://github.com/psf/black"><img src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
|
| 170 |
+
<a href="https://github.com/RapidAI/TableStructureRec/blob/c41bbd23898cb27a957ed962b0ffee3c74dfeff1/LICENSE"><img alt="GitHub" src="https://img.shields.io/badge/license-Apache 2.0-blue"></a>
|
| 171 |
+
</div>
|
| 172 |
+
''')
|
| 173 |
with gr.Row(): # 两列布局
|
| 174 |
with gr.Tab("Options"):
|
| 175 |
with gr.Column(variant="panel", scale=1): # 侧边栏,宽度比例为1
|
| 176 |
+
img_input = gr.Image(label="Upload or Select Image", sources="upload", value="images/lineless3.jpg")
|
| 177 |
|
| 178 |
# 示例图片选择器
|
| 179 |
examples = gr.Examples(
|
| 180 |
examples=example_images,
|
| 181 |
+
examples_per_page=len(example_images),
|
| 182 |
inputs=img_input,
|
| 183 |
fn=lambda x: x, # 简单返回图片路径
|
| 184 |
outputs=img_input,
|
| 185 |
+
cache_examples=False
|
| 186 |
)
|
| 187 |
|
| 188 |
table_engine_type = gr.Dropdown(table_engine_list, label="Select Recognition Table Engine",
|
| 189 |
value=table_engine_list[0])
|
| 190 |
+
small_box_cut_enhance = gr.Checkbox(
|
| 191 |
+
label="Box Cutting Enhancement (Disable to avoid excessive cutting, Enable to reduce missed cutting)",
|
| 192 |
+
value=True
|
| 193 |
+
)
|
| 194 |
det_model = gr.Dropdown(det_models_labels, label="Select OCR Detection Model",
|
| 195 |
value=det_models_labels[0])
|
| 196 |
rec_model = gr.Dropdown(rec_models_labels, label="Select OCR Recognition Model",
|
|
|
|
| 210 |
|
| 211 |
run_button.click(
|
| 212 |
fn=process_image,
|
| 213 |
+
inputs=[img_input, table_engine_type, det_model, rec_model, small_box_cut_enhance],
|
| 214 |
outputs=[html_output, table_boxes_output, ocr_boxes_output, elapse_text]
|
| 215 |
)
|
| 216 |
|
|
|
|
| 218 |
|
| 219 |
|
| 220 |
if __name__ == '__main__':
|
| 221 |
+
main()
|