Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load the document segmentation model
|
| 7 |
+
docseg_model = YOLO("https://huggingface.co/DILHTWD/documentlayoutsegmentation_YOLOv8_ondoclaynet/blob/main/yolov8x-doclaynet-epoch64-imgsz640-initiallr1e-4-finallr1e-5.pt")
|
| 8 |
+
|
| 9 |
+
def process_image(image):
|
| 10 |
+
# Convert image to the format YOLO model expects
|
| 11 |
+
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 12 |
+
results = docseg_model(source=image, save=False, show_labels=True, show_conf=True, show_boxes=True)
|
| 13 |
+
|
| 14 |
+
# Extract annotated image from results
|
| 15 |
+
annotated_img = results[0].plot()
|
| 16 |
+
|
| 17 |
+
return annotated_img, results[0].boxes
|
| 18 |
+
|
| 19 |
+
# Define the Gradio interface
|
| 20 |
+
interface = gr.Interface(
|
| 21 |
+
fn=process_image,
|
| 22 |
+
inputs=gr.inputs.Image(type="pil"),
|
| 23 |
+
outputs=[gr.outputs.Image(type="pil", label="Annotated Image"),
|
| 24 |
+
gr.outputs.Textbox(label="Detected Areas and Labels")]
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
if __name__ == "__main__":
|
| 28 |
+
interface.launch()
|