Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
-
import
|
| 5 |
import torch
|
| 6 |
-
import
|
|
|
|
| 7 |
|
| 8 |
CUSTOM_CSS = """
|
| 9 |
#output_box textarea {
|
|
@@ -11,6 +14,34 @@ CUSTOM_CSS = """
|
|
| 11 |
}
|
| 12 |
"""
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
zero = torch.Tensor([0]).cuda()
|
| 15 |
print(zero.device) # <-- 'cpu' 🤔
|
| 16 |
|
|
@@ -49,4 +80,22 @@ with gr.Blocks(css=CUSTOM_CSS) as demo:
|
|
| 49 |
|
| 50 |
check.change(run, inputs=[check], outputs=output, every=1)
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
demo.queue().launch(show_api=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
import os
|
| 6 |
+
import requests
|
| 7 |
import torch
|
| 8 |
+
import datetime
|
| 9 |
+
import subprocess
|
| 10 |
|
| 11 |
CUSTOM_CSS = """
|
| 12 |
#output_box textarea {
|
|
|
|
| 14 |
}
|
| 15 |
"""
|
| 16 |
|
| 17 |
+
# Ensure the model file is in the correct location
|
| 18 |
+
model_path = "yolov8x-doclaynet-epoch64-imgsz640-initiallr1e-4-finallr1e-5.pt"
|
| 19 |
+
if not os.path.exists(model_path):
|
| 20 |
+
# Download the model file if it doesn't exist
|
| 21 |
+
model_url = "https://huggingface.co/DILHTWD/documentlayoutsegmentation_YOLOv8_ondoclaynet/resolve/main/yolov8x-doclaynet-epoch64-imgsz640-initiallr1e-4-finallr1e-5.pt"
|
| 22 |
+
response = requests.get(model_url)
|
| 23 |
+
with open(model_path, "wb") as f:
|
| 24 |
+
f.write(response.content)
|
| 25 |
+
|
| 26 |
+
# Load the document segmentation model
|
| 27 |
+
docseg_model = YOLO(model_path)
|
| 28 |
+
|
| 29 |
+
def process_image(image):
|
| 30 |
+
# Convert image to the format YOLO model expects
|
| 31 |
+
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 32 |
+
results = docseg_model(image)
|
| 33 |
+
|
| 34 |
+
# Extract annotated image from results
|
| 35 |
+
annotated_img = results[0].plot()
|
| 36 |
+
annotated_img = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
|
| 37 |
+
|
| 38 |
+
# Prepare detected areas and labels as text output
|
| 39 |
+
detected_areas_labels = "\n".join(
|
| 40 |
+
[f"{box.label}: {box.conf:.2f}" for box in results[0].boxes]
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
return annotated_img, detected_areas_labels
|
| 44 |
+
|
| 45 |
zero = torch.Tensor([0]).cuda()
|
| 46 |
print(zero.device) # <-- 'cpu' 🤔
|
| 47 |
|
|
|
|
| 80 |
|
| 81 |
check.change(run, inputs=[check], outputs=output, every=1)
|
| 82 |
|
| 83 |
+
# Define the Gradio interface
|
| 84 |
+
with gr.Blocks() as interface:
|
| 85 |
+
gr.Markdown("### Document Segmentation using YOLOv8")
|
| 86 |
+
input_image = gr.Image(type="pil", label="Input Image")
|
| 87 |
+
output_image = gr.Image(type="pil", label="Annotated Image")
|
| 88 |
+
output_text = gr.Textbox(label="Detected Areas and Labels")
|
| 89 |
+
|
| 90 |
+
gr.Button("Run").click(
|
| 91 |
+
fn=process_image,
|
| 92 |
+
inputs=input_image,
|
| 93 |
+
outputs=[output_image, output_text]
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
demo.queue().launch(show_api=False)
|
| 97 |
+
interface.launch()
|
| 98 |
+
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
demo.launch()
|
| 101 |
+
interface.launch()
|