Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,54 @@
|
|
1 |
import gradio as gr
|
2 |
-
import torch
|
3 |
from ultralytics import YOLO
|
4 |
from PIL import Image, ImageDraw
|
5 |
|
6 |
-
# Load YOLO model
|
7 |
YOLO_MODEL_PATH = "best.pt"
|
8 |
model = YOLO(YOLO_MODEL_PATH, task='detect').to("cpu") # Force CPU usage
|
9 |
|
10 |
-
def
|
11 |
-
"""
|
12 |
-
|
13 |
-
|
14 |
-
boxes: List of bounding boxes [x1, y1, x2, y2]
|
15 |
-
y_threshold: Max distance between words to consider as the same line
|
16 |
-
Returns:
|
17 |
-
List of merged line bounding boxes without overlap
|
18 |
-
"""
|
19 |
-
if len(boxes) == 0:
|
20 |
-
return []
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
x1, y1, x2, y2 = boxes[i]
|
30 |
-
|
31 |
-
# Merge boxes that are close in the y-axis
|
32 |
-
if abs(y1 - current_line[1]) < y_threshold:
|
33 |
-
current_line[0] = min(current_line[0], x1) # Expand left boundary
|
34 |
-
current_line[2] = max(current_line[2], x2) # Expand right boundary
|
35 |
-
current_line[3] = max(current_line[3], y2) # Expand bottom boundary
|
36 |
-
else:
|
37 |
-
merged_lines.append(current_line)
|
38 |
-
current_line = list(boxes[i])
|
39 |
|
40 |
-
|
41 |
-
return merged_lines
|
42 |
|
43 |
def detect_lines(image):
|
44 |
"""Runs YOLOv8 detection on the input image to detect lines."""
|
45 |
image = Image.fromarray(image) # Convert NumPy array to PIL Image
|
46 |
|
47 |
-
#
|
48 |
-
|
|
|
|
|
|
|
49 |
|
50 |
detected_boxes = results[0].boxes.xyxy.tolist()
|
51 |
detected_boxes = [list(map(int, box)) for box in detected_boxes] # Convert to integer
|
52 |
|
53 |
-
#
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
-
# Draw bounding boxes
|
57 |
image_with_boxes = image.copy()
|
58 |
draw = ImageDraw.Draw(image_with_boxes)
|
59 |
|
60 |
-
for idx, (x1, y1, x2, y2) in enumerate(
|
61 |
draw.rectangle([x1, y1, x2, y2], outline="blue", width=2)
|
62 |
draw.text((x1, y1 - 10), f"Line {idx}", fill="blue")
|
63 |
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from ultralytics import YOLO
|
3 |
from PIL import Image, ImageDraw
|
4 |
|
5 |
+
# Load YOLO model (trained on 640x640)
|
6 |
YOLO_MODEL_PATH = "best.pt"
|
7 |
model = YOLO(YOLO_MODEL_PATH, task='detect').to("cpu") # Force CPU usage
|
8 |
|
9 |
+
def resize_and_pad(image, target_size=(640, 640)):
|
10 |
+
"""Resize image while keeping aspect ratio and padding to fit target size."""
|
11 |
+
original_size = image.size # (width, height)
|
12 |
+
image.thumbnail((target_size[0], target_size[1]), Image.ANTIALIAS) # Resize while keeping aspect ratio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
# Create a new white background image
|
15 |
+
new_image = Image.new("RGB", target_size, (255, 255, 255))
|
16 |
+
|
17 |
+
# Paste the resized image in the center
|
18 |
+
paste_x = (target_size[0] - image.size[0]) // 2
|
19 |
+
paste_y = (target_size[1] - image.size[1]) // 2
|
20 |
+
new_image.paste(image, (paste_x, paste_y))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
return new_image, original_size, paste_x, paste_y
|
|
|
23 |
|
24 |
def detect_lines(image):
|
25 |
"""Runs YOLOv8 detection on the input image to detect lines."""
|
26 |
image = Image.fromarray(image) # Convert NumPy array to PIL Image
|
27 |
|
28 |
+
# Resize & pad A4 images
|
29 |
+
resized_image, original_size, pad_x, pad_y = resize_and_pad(image)
|
30 |
+
|
31 |
+
# Run YOLO detection
|
32 |
+
results = model.predict(resized_image, conf=0.3, iou=0.5, device="cpu")
|
33 |
|
34 |
detected_boxes = results[0].boxes.xyxy.tolist()
|
35 |
detected_boxes = [list(map(int, box)) for box in detected_boxes] # Convert to integer
|
36 |
|
37 |
+
# Scale boxes back to original image size
|
38 |
+
width_ratio = original_size[0] / 640
|
39 |
+
height_ratio = original_size[1] / 640
|
40 |
+
|
41 |
+
scaled_boxes = [
|
42 |
+
[int((x1 - pad_x) * width_ratio), int((y1 - pad_y) * height_ratio),
|
43 |
+
int((x2 - pad_x) * width_ratio), int((y2 - pad_y) * height_ratio)]
|
44 |
+
for x1, y1, x2, y2 in detected_boxes
|
45 |
+
]
|
46 |
|
47 |
+
# Draw bounding boxes on the original image
|
48 |
image_with_boxes = image.copy()
|
49 |
draw = ImageDraw.Draw(image_with_boxes)
|
50 |
|
51 |
+
for idx, (x1, y1, x2, y2) in enumerate(scaled_boxes):
|
52 |
draw.rectangle([x1, y1, x2, y2], outline="blue", width=2)
|
53 |
draw.text((x1, y1 - 10), f"Line {idx}", fill="blue")
|
54 |
|