Spaces:
Runtime error
Runtime error
Update app.py
Browse files12 unmodified lines131415161718192021222343 unmodified lines676869707172737475767778798081828384858687888990919293949510 unmodified lines1061071081091101111121131141151169 unmodified lines12612712812913013113215 unmodified lines14814915015115215315412 unmodified linesimg_loader = LoadImage()table_rec_path = "models/table_rec/ch_ppstructure_mobile_v2_SLANet.onnx"det_model_dir = {"mobile_det": "models/ocr/ch_PP-OCRv4_det_infer.onnx"}rec_model_dir = {"mobile_rec": "models/ocr/ch_PP-OCRv4_rec_infer.onnx"}table_engine_list = ["auto",43 unmodified linesdef select_table_model(img, table_engine_type, det_model, rec_model):if table_engine_type == "rapid_table":return rapid_table_engine, 0elif table_engine_type == "wired_table_v1":return wired_table_engine_v1, 0elif table_engine_type == "wired_table_v2":print("使用v2 wired table")return wired_table_engine_v2, 0elif table_engine_type == "lineless_table":return lineless_table_engine, 0elif table_engine_type == "pp_table":return pp_engine_dict[f"{det_model}_{rec_model}"], 0elif table_engine_type == "auto":cls, elasp = table_cls(img)if cls == 'wired':table_engine = wired_table_engine_v2else:table_engine = lineless_table_enginereturn table_engine, elaspdef process_image(img, table_engine_type, det_model, rec_model):img = img_loader(img)start = time.time()table_engine, select_elapse = select_table_model(img, table_engine_type, det_model, rec_model)ocr_engine = select_ocr_model(det_model, rec_model)if isinstance(table_engine, PPStructure):10 unmodified linesif isinstance(table_engine, RapidTable):html, polygons, table_rec_elapse = table_engine(img, ocr_result=ocr_res)elif isinstance(table_engine, (WiredTableRecognition, LinelessTableRecognition)):html, table_rec_elapse, polygons, _, _ = table_engine(img, ocr_result=ocr_res)sum_elapse = time.time() - startall_elapse = f"- table all cost: {sum_elapse:.5f}\n - table rec cost: {table_rec_elapse:.5f}\n - ocr cost: {det_cost + cls_cost + rec_cost:.5f}"img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)table_boxes_img = plot_rec_box(img.copy(), polygons)9 unmodified lineswith gr.Blocks() as demo:with gr.Row(): # 两列布局with gr.Column(): # 左边列img_input = gr.Image(label="Upload or Select Image", sources="upload")# 示例图片选择器15 unmodified linesrun_button = gr.Button("Run")gr.Markdown("# Elapsed Time")elapse_text = gr.Text(label="") # 使用 `gr.Text` 组件展示字符串with gr.Column(): # 右边列# 使用 Markdown 标题分隔各个组件gr.Markdown("# Html Render")html_output = gr.HTML(label="", elem_classes="scrollable-container")12 unmodified lines131415161718192021222343 unmodified lines6768697071727374757677787980818283848586878889909192939410 unmodified lines1051061071081091101111121131141151169 unmodified lines12612712812913013113215 unmodified lines14814915015115215315412 unmodified linesimg_loader = LoadImage()table_rec_path = "models/table_rec/ch_ppstructure_mobile_v2_SLANet.onnx"det_model_dir = {"mobile_det": "models/ocr/ch_PP-OCRv4_det_infer.onnx",}rec_model_dir = {"mobile_rec": "models/ocr/ch_PP-OCRv4_rec_infer.onnx",}table_engine_list = ["auto",43 unmodified linesdef select_table_model(img, table_engine_type, det_model, rec_model):if table_engine_type == "rapid_table":return rapid_table_engine, table_engine_typeelif table_engine_type == "wired_table_v1":return wired_table_engine_v1, table_engine_typeelif table_engine_type == "wired_table_v2":print("使用v2 wired table")return wired_table_engine_v2, table_engine_typeelif table_engine_type == "lineless_table":return lineless_table_engine, table_engine_typeelif table_engine_type == "pp_table":return pp_engine_dict[f"{det_model}_{rec_model}"], 0elif table_engine_type == "auto":cls, elasp = table_cls(img)if cls == 'wired':table_engine = wired_table_engine_v2return table_engine, "wired_table_v2"return lineless_table_engine, "lineless_table"def process_image(img, table_engine_type, det_model, rec_model):img = img_loader(img)start = time.time()table_engine, talbe_type = select_table_model(img, table_engine_type, det_model, rec_model)ocr_engine = select_ocr_model(det_model, rec_model)if isinstance(table_engine, PPStructure):10 unmodified linesif isinstance(table_engine, RapidTable):html, polygons, table_rec_elapse = table_engine(img, ocr_result=ocr_res)polygons = [[polygon[0], polygon[1], polygon[4], polygon[5]] for polygon in polygons]elif isinstance(table_engine, (WiredTableRecognition, LinelessTableRecognition)):html, table_rec_elapse, polygons, _, _ = table_engine(img, ocr_result=ocr_res)sum_elapse = time.time() - startall_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}"img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)table_boxes_img = plot_rec_box(img.copy(), polygons)9 unmodified lineswith gr.Blocks() as demo:with gr.Row(): # 两列布局with gr.Column(variant="panel"): # 左边列img_input = gr.Image(label="Upload or Select Image", sources="upload")# 示例图片选择器15 unmodified linesrun_button = gr.Button("Run")gr.Markdown("# Elapsed Time")elapse_text = gr.Text(label="") # 使用 `gr.Text` 组件展示字符串with gr.Column(scale=2): # 右边列# 使用 Markdown 标题分隔各个组件gr.Markdown("# Html Render")html_output = gr.HTML(label="", elem_classes="scrollable-container")
app.py+13-13