Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,102 +1,99 @@
|
|
| 1 |
# -*- encoding: utf-8 -*-
|
| 2 |
# @Author: SWHL
|
| 3 |
# @Contact: [email protected]
|
| 4 |
-
import
|
| 5 |
-
import streamlit as st
|
| 6 |
-
from PIL import Image
|
| 7 |
-
import cv2
|
| 8 |
|
| 9 |
-
from rapid_layout import RapidLayout,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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 |
-
with
|
| 87 |
-
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
)
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
|
|
|
|
|
|
| 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)
|