File size: 3,961 Bytes
60c966b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36af02d
60c966b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1842d1
60c966b
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: liekkaskono@163.com
import gradio as gr

from rapid_layout import ModelType, RapidLayout, RapidLayoutInput


def get_layout_res(img_input, model_type, conf_thresh, iou_thresh):
    cfg = RapidLayoutInput(
        model_type=ModelType(model_type), conf_thresh=conf_thresh, iou_thresh=iou_thresh
    )
    engine = RapidLayout(cfg=cfg)
    result = engine(img_input)
    vised_img = result.vis("layout_vis.jpg")
    return vised_img, f"{result.elapse:.4f}"


custom_css = """
    body {font-family: body {font-family: 'Helvetica Neue', Helvetica;}
    .gr-button {background-color: #4CAF50; color: white; border: none; padding: 10px 20px; border-radius: 5px;}
    .gr-button:hover {background-color: #45a049;}
    .gr-textbox {margin-bottom: 15px;}
    .example-button {background-color: #1E90FF; color: white; border: none; padding: 8px 15px; border-radius: 5px; margin: 5px;}
    .example-button:hover {background-color: #FF4500;}
    .tall-radio .gr-radio-item {padding: 15px 0; min-height: 50px; display: flex; align-items: center;}
    .tall-radio label {font-size: 16px;}
    .output-image, .input-image, .image-preview {height: 300px !important}
"""

with gr.Blocks(title="Rapid Layout", css="custom_css", theme=gr.themes.Soft()) as demo:
    gr.HTML(
        """
        <h1 style='text-align: center;font-size:40px'><a href="https://github.com/RapidAI/RapidLayout">Rapid Layout v1</a></h1>

        <div style="display: flex; justify-content: center; gap: 10px;">
            <a><img src="https://img.shields.io/badge/DeployVersion-v1.0.0-brightgree"></a>
            <a href=""><img src="https://img.shields.io/badge/Python->=3.6,<3.13-aff.svg"></a>
            <a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
            <a href="https://pypi.org/project/rapid-layout/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapid-layout"></a>
            <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>
            <a href="https://semver.org/"><img alt="SemVer2.0" src="https://img.shields.io/badge/SemVer-2.0-brightgreen"></a>
            <a href="https://github.com/psf/black"><img src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
        </div>
    """
    )
    with gr.Row():
        model_type = gr.Dropdown(
            choices=[v.value for v in ModelType],
            label="版面分析模型",
            value=ModelType.PP_LAYOUT_CDLA.value,
            interactive=True,
        )
        conf_thresh = gr.Slider(
            label="conf_thresh",
            minimum=0,
            maximum=1.0,
            value=0.5,
            step=0.1,
            info="置信度",
            interactive=True,
        )
        iou_thresh = gr.Slider(
            label="IoU Threshold",
            minimum=0,
            maximum=1.0,
            value=0.5,
            step=0.1,
            info="IoU阈值设定",
            interactive=True,
        )

    with gr.Row():
        run_btn = gr.Button("Run")

    with gr.Row():
        img_input = gr.Image(label="Upload or Select Image", sources="upload")
        img_output = gr.Image(label="Output Image", show_download_button=True)
    elapse = gr.Textbox(label="Elapse(s)")

    run_btn.click(
        get_layout_res,
        inputs=[img_input, model_type, conf_thresh, iou_thresh],
        outputs=[img_output, elapse],
    )

    examples = gr.Examples(
        examples=[
            ["images/layout.jpg", ModelType.PP_LAYOUT_CDLA.value, 0.5, 0.5],
        ],
        examples_per_page=5,
        inputs=[img_input, model_type, conf_thresh, iou_thresh],
        fn=get_layout_res,
        outputs=[img_output, elapse],
        cache_examples=False,
    )

if __name__ == "__main__":
    demo.launch(debug=True)