SWHL commited on
Commit
3a388f2
·
verified ·
1 Parent(s): c82d44f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -91
app.py CHANGED
@@ -1,102 +1,99 @@
1
  # -*- encoding: utf-8 -*-
2
  # @Author: SWHL
3
  # @Contact: [email protected]
4
- import numpy as np
5
- import streamlit as st
6
- from PIL import Image
7
- import cv2
8
 
9
- from rapid_layout import RapidLayout, VisLayout
 
 
 
 
 
 
 
 
 
 
10
 
11
- st.markdown(
12
- "<h1 style='text-align: center;'><a href='https://github.com/RapidAI/RapidLayout' style='text-decoration: none'>Rapid Layout</a></h1>",
13
- unsafe_allow_html=True,
14
- )
15
- st.markdown(
16
- """
17
- <p align="left">
18
- <a href=""><img src="https://img.shields.io/badge/Python->=3.6,<3.13-aff.svg"></a>
19
- <a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
20
- <a href="https://pypi.org/project/rapid-layout/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapid-layout"></a>
21
- <a href="https://pepy.tech/project/rapid-layout"><img src="https://static.pepy.tech/personalized-badge/rapid-layout?period=total&units=abbreviation&left_color=grey&right_color=blue&left_text=Downloads"></a>
22
- <a href="https://semver.org/"><img alt="SemVer2.0" src="https://img.shields.io/badge/SemVer-2.0-brightgreen"></a>
23
- <a href="https://github.com/psf/black"><img src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
24
- </p>
25
- """,
26
- unsafe_allow_html=True,
27
- )
28
 
29
- model_types = {
30
- "pp_layout_cdla": [
31
- "text",
32
- "title",
33
- "figure",
34
- "figure_caption",
35
- "table",
36
- "table_caption",
37
- "header",
38
- "footer",
39
- "reference",
40
- "equation",
41
- ],
42
- "pp_layout_publaynet": ["text", "title", "list", "table", "figure"],
43
- "pp_layout_table": ["table"],
44
- "yolov8n_layout_paper": [
45
- "Text",
46
- "Title",
47
- "Header",
48
- "Footer",
49
- "Figure",
50
- "Table",
51
- "Toc",
52
- "Figure caption",
53
- "Table caption",
54
- ],
55
- "yolov8n_layout_report": [
56
- "Text",
57
- "Title",
58
- "Header",
59
- "Footer",
60
- "Figure",
61
- "Table",
62
- "Toc",
63
- "Figure caption",
64
- "Table caption",
65
- ],
66
- "yolov8n_layout_publaynet": ["Text", "Title", "List", "Table", "Figure"],
67
- "yolov8n_layout_general6": [
68
- "Text",
69
- "Title",
70
- "Figure",
71
- "Table",
72
- "Caption",
73
- "Equation",
74
- ],
75
- "doclayout_docstructbench": ['title', 'plain text', 'abandon', 'figure', 'figure_caption', 'table', 'table_caption', 'table_footnote', 'isolate_formula', 'formula_caption'],
76
- "doclayout_d4la": ['DocTitle', 'ParaTitle', 'ParaText', 'ListText', 'RegionTitle', 'Date', 'LetterHead', 'LetterDear', 'LetterSign', 'Question', 'OtherText', 'RegionKV', 'RegionList', 'Abstract', 'Author', 'TableName', 'Table', 'Figure', 'FigureName', 'Equation', 'Reference', 'Footer', 'PageHeader', 'PageFooter', 'Number', 'Catalog', 'PageNumber'],
77
- "doclayout_docsynth": ['Caption', 'Footnote', 'Formula', 'List-item', 'Page-footer', 'Page-header', 'Picture', 'Section-header', 'Table', 'Text', 'Title']
78
- }
79
- select_model = st.selectbox("选择版面分析模型:", model_types.keys())
80
- st.write("支持检测类型:")
81
- st.code(model_types[select_model], language="python")
82
 
83
- conf_threshold = st.slider("Confidence Threshold", 0.0, 1.0, 0.5)
84
- iou_threshold = st.slider("IoU Threshold", 0.0, 1.0, 0.5)
85
 
86
- with st.spinner(f"Downloading {select_model} model..."):
87
- layout_engine = RapidLayout(model_type=select_model, conf_thres=conf_threshold, iou_thres=iou_threshold)
 
 
88
 
89
- st.write("请上传图像:")
90
- img_suffix = ["png", "jpg", "jpeg"]
91
- img_file_buffer = st.file_uploader(
92
- "Upload an image", type=img_suffix, key="layout", label_visibility="collapsed"
93
- )
94
 
95
- if img_file_buffer:
96
- image = Image.open(img_file_buffer)
97
- img = np.array(image)
98
- with st.spinner("推理中...."):
99
- boxes, scores, class_names, *elapse = layout_engine(img, )
100
- ploted_img = VisLayout.draw_detections(img, boxes, scores, class_names)
 
 
 
 
101
 
102
- st.image(ploted_img, use_column_width=True)
 
 
1
  # -*- encoding: utf-8 -*-
2
  # @Author: SWHL
3
  # @Contact: [email protected]
4
+ import gradio as gr
 
 
 
5
 
6
+ from rapid_layout import ModelType, RapidLayout, RapidLayoutInput
7
+
8
+
9
+ def get_layout_res(img_input, model_type, conf_thresh, iou_thresh):
10
+ cfg = RapidLayoutInput(
11
+ model_type=ModelType(model_type), conf_thresh=conf_thresh, iou_thresh=iou_thresh
12
+ )
13
+ engine = RapidLayout(cfg=cfg)
14
+ result = engine(img_input)
15
+ vised_img = result.vis("layout_vis.jpg")
16
+ return vised_img, f"{result.elapse:.4f}"
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ custom_css = """
20
+ body {font-family: body {font-family: 'Helvetica Neue', Helvetica;}
21
+ .gr-button {background-color: #4CAF50; color: white; border: none; padding: 10px 20px; border-radius: 5px;}
22
+ .gr-button:hover {background-color: #45a049;}
23
+ .gr-textbox {margin-bottom: 15px;}
24
+ .example-button {background-color: #1E90FF; color: white; border: none; padding: 8px 15px; border-radius: 5px; margin: 5px;}
25
+ .example-button:hover {background-color: #FF4500;}
26
+ .tall-radio .gr-radio-item {padding: 15px 0; min-height: 50px; display: flex; align-items: center;}
27
+ .tall-radio label {font-size: 16px;}
28
+ .output-image, .input-image, .image-preview {height: 300px !important}
29
+ """
30
+
31
+ with gr.Blocks(title="Rapid Layout", css="custom_css", theme=gr.themes.Soft()) as demo:
32
+ gr.HTML(
33
+ """
34
+ <h1 style='text-align: center;font-size:40px'>Rapid Layout v1</h1>
35
+
36
+ <div style="display: flex; justify-content: center; gap: 10px;">
37
+ <a><img src="https://img.shields.io/badge/DeployVersion-v1.0.0-brightgree"></a>
38
+ <a href=""><img src="https://img.shields.io/badge/Python->=3.6,<3.13-aff.svg"></a>
39
+ <a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
40
+ <a href="https://pypi.org/project/rapid-layout/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapid-layout"></a>
41
+ <a href="https://pepy.tech/project/rapid-layout"><img src="https://static.pepy.tech/personalized-badge/rapid-layout?period=total&units=abbreviation&left_color=grey&right_color=blue&left_text=Downloads"></a>
42
+ <a href="https://semver.org/"><img alt="SemVer2.0" src="https://img.shields.io/badge/SemVer-2.0-brightgreen"></a>
43
+ <a href="https://github.com/psf/black"><img src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
44
+ </div>
45
+ """
46
+ )
47
+ with gr.Row():
48
+ model_type = gr.Dropdown(
49
+ choices=[v.value for v in ModelType],
50
+ label="版面分析模型",
51
+ value=ModelType.PP_LAYOUT_CDLA.value,
52
+ interactive=True,
53
+ )
54
+ conf_thresh = gr.Slider(
55
+ label="conf_thresh",
56
+ minimum=0,
57
+ maximum=1.0,
58
+ value=0.5,
59
+ step=0.1,
60
+ info="置信度",
61
+ interactive=True,
62
+ )
63
+ iou_thresh = gr.Slider(
64
+ label="IoU Threshold",
65
+ minimum=0,
66
+ maximum=1.0,
67
+ value=0.5,
68
+ step=0.1,
69
+ info="IoU阈值设定",
70
+ interactive=True,
71
+ )
72
 
73
+ with gr.Row():
74
+ run_btn = gr.Button("Run")
75
 
76
+ with gr.Row():
77
+ img_input = gr.Image(label="Upload or Select Image", sources="upload")
78
+ img_output = gr.Image(label="Output Image", show_download_button=True)
79
+ elapse = gr.Textbox(label="Elapse(s)")
80
 
81
+ run_btn.click(
82
+ get_layout_res,
83
+ inputs=[img_input, model_type, conf_thresh, iou_thresh],
84
+ outputs=[img_output, elapse],
85
+ )
86
 
87
+ examples = gr.Examples(
88
+ examples=[
89
+ ["images/layout.jpg", ModelType.PP_LAYOUT_CDLA.value, 0.5, 0.5],
90
+ ],
91
+ examples_per_page=5,
92
+ inputs=[img_input, model_type, conf_thresh, iou_thresh],
93
+ fn=get_layout_res,
94
+ outputs=[img_output, elapse],
95
+ cache_examples=False,
96
+ )
97
 
98
+ if __name__ == "__main__":
99
+ demo.launch(debug=True)