Norakneath commited on
Commit
e6b1342
·
verified ·
1 Parent(s): c2cffb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -38
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 for line detection
7
  YOLO_MODEL_PATH = "best.pt"
8
  model = YOLO(YOLO_MODEL_PATH, task='detect').to("cpu") # Force CPU usage
9
 
10
- def merge_boxes_into_lines(boxes, y_threshold=20):
11
- """
12
- Merge bounding boxes that are close together in the y-axis (same line).
13
- Args:
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
- # Sort boxes by y1 (top position)
23
- boxes = sorted(boxes, key=lambda b: b[1])
24
-
25
- merged_lines = []
26
- current_line = list(boxes[0])
27
-
28
- for i in range(1, len(boxes)):
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
- merged_lines.append(current_line)
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
- # Run YOLO detection with confidence threshold 0.2
48
- results = model.predict(image, conf=0.3, iou=0.5, device="cpu")
 
 
 
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
- # Merge detected bounding boxes into line-level boxes
54
- merged_boxes = merge_boxes_into_lines(detected_boxes)
 
 
 
 
 
 
 
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(merged_boxes):
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