Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
from ultralytics import YOLO
|
3 |
from PIL import Image, ImageDraw
|
|
|
4 |
|
5 |
# Load YOLO model
|
6 |
YOLO_MODEL_PATH = "best.pt"
|
@@ -47,7 +48,7 @@ def detect_lines(image, resize=False, target_size=(640, 640)):
|
|
47 |
resize: Boolean, whether to resize image before detection
|
48 |
target_size: Tuple (width, height) for resizing
|
49 |
Returns:
|
50 |
-
Image with bounding boxes drawn
|
51 |
"""
|
52 |
image = Image.fromarray(image) # Convert NumPy array to PIL Image
|
53 |
original_image = image.copy() # Keep a copy of the original image
|
@@ -71,11 +72,12 @@ def detect_lines(image, resize=False, target_size=(640, 640)):
|
|
71 |
draw.rectangle([x1, y1, x2, y2], outline="blue", width=2)
|
72 |
draw.text((x1, y1 - 10), f"Line {idx}", fill="blue")
|
73 |
|
74 |
-
return image_with_boxes
|
75 |
|
76 |
# Define Gradio interface with two options: Original & Resized detection
|
77 |
with gr.Blocks() as iface:
|
78 |
-
gr.Markdown("# Text Line Detection
|
|
|
79 |
|
80 |
with gr.Row():
|
81 |
with gr.Column(scale=1):
|
@@ -85,15 +87,17 @@ with gr.Blocks() as iface:
|
|
85 |
with gr.Column(scale=1):
|
86 |
gr.Markdown("### YOLO on Original Image")
|
87 |
output_original = gr.Image(type="pil", label="Detected Lines (Original Size)")
|
|
|
88 |
|
89 |
with gr.Column(scale=1):
|
90 |
gr.Markdown("### YOLO on Resized Image (640x640)")
|
91 |
output_resized = gr.Image(type="pil", label="Detected Lines (Resized to 640x640)")
|
|
|
92 |
|
93 |
image_input.upload(
|
94 |
-
lambda img: (detect_lines(img, resize=False), detect_lines(img, resize=True, target_size=(640, 640))),
|
95 |
inputs=image_input,
|
96 |
-
outputs=[output_original, output_resized]
|
97 |
)
|
98 |
|
99 |
# Launch Gradio interface
|
|
|
1 |
import gradio as gr
|
2 |
from ultralytics import YOLO
|
3 |
from PIL import Image, ImageDraw
|
4 |
+
import numpy as np
|
5 |
|
6 |
# Load YOLO model
|
7 |
YOLO_MODEL_PATH = "best.pt"
|
|
|
48 |
resize: Boolean, whether to resize image before detection
|
49 |
target_size: Tuple (width, height) for resizing
|
50 |
Returns:
|
51 |
+
Image with bounding boxes drawn, Number of detected boxes
|
52 |
"""
|
53 |
image = Image.fromarray(image) # Convert NumPy array to PIL Image
|
54 |
original_image = image.copy() # Keep a copy of the original image
|
|
|
72 |
draw.rectangle([x1, y1, x2, y2], outline="blue", width=2)
|
73 |
draw.text((x1, y1 - 10), f"Line {idx}", fill="blue")
|
74 |
|
75 |
+
return image_with_boxes, len(merged_boxes)
|
76 |
|
77 |
# Define Gradio interface with two options: Original & Resized detection
|
78 |
with gr.Blocks() as iface:
|
79 |
+
gr.Markdown("# Text Line Detection")
|
80 |
+
gr.Markdown("## Input your custom image for text line detection")
|
81 |
|
82 |
with gr.Row():
|
83 |
with gr.Column(scale=1):
|
|
|
87 |
with gr.Column(scale=1):
|
88 |
gr.Markdown("### YOLO on Original Image")
|
89 |
output_original = gr.Image(type="pil", label="Detected Lines (Original Size)")
|
90 |
+
count_original = gr.Textbox(label="Number of Detected Lines (Original Size)")
|
91 |
|
92 |
with gr.Column(scale=1):
|
93 |
gr.Markdown("### YOLO on Resized Image (640x640)")
|
94 |
output_resized = gr.Image(type="pil", label="Detected Lines (Resized to 640x640)")
|
95 |
+
count_resized = gr.Textbox(label="Number of Detected Lines (Resized)")
|
96 |
|
97 |
image_input.upload(
|
98 |
+
lambda img: (*detect_lines(img, resize=False), *detect_lines(img, resize=True, target_size=(640, 640))),
|
99 |
inputs=image_input,
|
100 |
+
outputs=[output_original, count_original, output_resized, count_resized]
|
101 |
)
|
102 |
|
103 |
# Launch Gradio interface
|