# -*- 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( """

Rapid Layout v1

PyPI SemVer2.0
""" ) 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)