Spaces:
Sleeping
Sleeping
Fix bounding box offsets, scale clamping, and column-aware reading order
Browse files- gradio_api.py +63 -36
gradio_api.py
CHANGED
|
@@ -398,32 +398,38 @@ def insert_ocr_text_box(page, font, text, p0, p1, p3, zoom=1.0):
|
|
| 398 |
ascender = font.ascender if hasattr(font, 'ascender') and font.ascender > 0 else 0.928
|
| 399 |
descender = font.descender if hasattr(font, 'descender') and font.descender < 0 else -0.236
|
| 400 |
font_ratio = ascender - descender
|
| 401 |
-
|
| 402 |
-
fontsize = max(min(box_height_pdf / font_ratio, 20.0), 5.5)
|
| 403 |
|
| 404 |
dx_up = p0_pdf[0] - p3_pdf[0]
|
| 405 |
dy_up = p0_pdf[1] - p3_pdf[1]
|
| 406 |
base_x = p3_pdf[0] + dx_up * (-descender / font_ratio)
|
| 407 |
base_y = p3_pdf[1] + dy_up * (-descender / font_ratio)
|
| 408 |
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
else:
|
| 417 |
scale_x = 1.0
|
| 418 |
|
| 419 |
-
# STRICT RIGHT PAGE BOUNDARY CLAMP:
|
| 420 |
-
max_allowed_w =
|
| 421 |
-
if
|
| 422 |
-
scale_x = max_allowed_w /
|
| 423 |
-
if scale_x < 0.
|
| 424 |
-
fontsize = max(
|
| 425 |
-
|
| 426 |
-
scale_x = max_allowed_w /
|
| 427 |
|
| 428 |
matrix = fitz.Matrix(scale_x, 1.0) * fitz.Matrix(-angle_deg)
|
| 429 |
try:
|
|
@@ -444,25 +450,37 @@ def insert_clamped_rect_text(page, font, text, rect):
|
|
| 444 |
"""
|
| 445 |
if not text or not text.strip():
|
| 446 |
return
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
|
|
|
|
|
|
|
|
|
| 451 |
|
| 452 |
box_w = x1 - x0
|
| 453 |
box_h = y1 - y0
|
| 454 |
|
| 455 |
-
fontsize = max(min(box_h
|
| 456 |
-
|
| 457 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
|
| 459 |
-
max_allowed_w =
|
| 460 |
-
if
|
| 461 |
-
scale_x = max_allowed_w /
|
| 462 |
-
if scale_x < 0.
|
| 463 |
-
fontsize = max(
|
| 464 |
-
|
| 465 |
-
scale_x = max_allowed_w /
|
| 466 |
|
| 467 |
y_baseline = y1 - (box_h * 0.18)
|
| 468 |
point = fitz.Point(x0, y_baseline)
|
|
@@ -874,6 +892,10 @@ def process_pdf(input_file_path, mode, smart_skip=True, user_gemini_key=None, pr
|
|
| 874 |
min_y = min(p0_pdf[1], p1_pdf[1], p2_pdf[1], p3_pdf[1])
|
| 875 |
max_y = max(p0_pdf[1], p1_pdf[1], p2_pdf[1], p3_pdf[1])
|
| 876 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 877 |
pdf_rect = fitz.Rect(min_x, min_y, max_x, max_y)
|
| 878 |
pdf_paddle_items.append({
|
| 879 |
"poly": (p0, p1, p2, p3),
|
|
@@ -1040,11 +1062,16 @@ def process_pdf(input_file_path, mode, smart_skip=True, user_gemini_key=None, pr
|
|
| 1040 |
text = line[1][0]
|
| 1041 |
if not text.strip(): continue
|
| 1042 |
|
| 1043 |
-
xmin = min(p[0] for p in box)
|
| 1044 |
-
ymin = min(p[1] for p in box)
|
| 1045 |
-
xmax = max(p[0] for p in box)
|
| 1046 |
-
ymax = max(p[1] for p in box)
|
| 1047 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1048 |
|
| 1049 |
sorted_data = recursive_xy_cut(boxes_with_data)
|
| 1050 |
|
|
|
|
| 398 |
ascender = font.ascender if hasattr(font, 'ascender') and font.ascender > 0 else 0.928
|
| 399 |
descender = font.descender if hasattr(font, 'descender') and font.descender < 0 else -0.236
|
| 400 |
font_ratio = ascender - descender
|
| 401 |
+
fontsize = max(min(box_height_pdf / font_ratio, 13.5), 5.5)
|
|
|
|
| 402 |
|
| 403 |
dx_up = p0_pdf[0] - p3_pdf[0]
|
| 404 |
dy_up = p0_pdf[1] - p3_pdf[1]
|
| 405 |
base_x = p3_pdf[0] + dx_up * (-descender / font_ratio)
|
| 406 |
base_y = p3_pdf[1] + dy_up * (-descender / font_ratio)
|
| 407 |
|
| 408 |
+
max_right = page.rect.width - 35.0
|
| 409 |
+
max_bottom = page.rect.height - 25.0
|
| 410 |
+
base_x = max(page.rect.x0 + 15.0, min(max_right - 20.0, base_x))
|
| 411 |
+
base_y = max(page.rect.y0 + 15.0, min(max_bottom - 5.0, base_y))
|
| 412 |
+
|
| 413 |
+
natural_len = font.text_length(text, fontsize=fontsize)
|
| 414 |
+
if natural_len > 0 and box_width_pdf > 0:
|
| 415 |
+
raw_scale = box_width_pdf / natural_len
|
| 416 |
+
if 0.80 <= raw_scale <= 1.05:
|
| 417 |
+
scale_x = raw_scale
|
| 418 |
+
elif raw_scale > 1.05:
|
| 419 |
+
scale_x = 1.0
|
| 420 |
+
else:
|
| 421 |
+
scale_x = max(0.70, raw_scale)
|
| 422 |
else:
|
| 423 |
scale_x = 1.0
|
| 424 |
|
| 425 |
+
# STRICT RIGHT PAGE BOUNDARY CLAMP: Ensure (base_x + natural_len * scale_x) <= max_right
|
| 426 |
+
max_allowed_w = max(5.0, max_right - base_x)
|
| 427 |
+
if (natural_len * scale_x) > max_allowed_w:
|
| 428 |
+
scale_x = max_allowed_w / natural_len if natural_len > 0 else 1.0
|
| 429 |
+
if scale_x < 0.60:
|
| 430 |
+
fontsize = max(4.5, fontsize * (scale_x / 0.60))
|
| 431 |
+
natural_len = font.text_length(text, fontsize=fontsize)
|
| 432 |
+
scale_x = max_allowed_w / natural_len if natural_len > 0 else 1.0
|
| 433 |
|
| 434 |
matrix = fitz.Matrix(scale_x, 1.0) * fitz.Matrix(-angle_deg)
|
| 435 |
try:
|
|
|
|
| 450 |
"""
|
| 451 |
if not text or not text.strip():
|
| 452 |
return
|
| 453 |
+
max_right = page.rect.width - 35.0
|
| 454 |
+
max_bottom = page.rect.height - 25.0
|
| 455 |
+
|
| 456 |
+
x0 = max(page.rect.x0 + 15.0, min(max_right - 20.0, rect.x0))
|
| 457 |
+
y0 = max(page.rect.y0 + 15.0, min(max_bottom - 10.0, rect.y0))
|
| 458 |
+
x1 = max(x0 + 10.0, min(max_right, rect.x1))
|
| 459 |
+
y1 = max(y0 + 5.0, min(max_bottom, rect.y1))
|
| 460 |
|
| 461 |
box_w = x1 - x0
|
| 462 |
box_h = y1 - y0
|
| 463 |
|
| 464 |
+
fontsize = max(min(box_h * 0.75, 13.5), 5.5)
|
| 465 |
+
natural_len = font.text_length(text, fontsize=fontsize)
|
| 466 |
+
if natural_len > 0 and box_w > 0:
|
| 467 |
+
raw_scale = box_w / natural_len
|
| 468 |
+
if 0.80 <= raw_scale <= 1.05:
|
| 469 |
+
scale_x = raw_scale
|
| 470 |
+
elif raw_scale > 1.05:
|
| 471 |
+
scale_x = 1.0
|
| 472 |
+
else:
|
| 473 |
+
scale_x = max(0.70, raw_scale)
|
| 474 |
+
else:
|
| 475 |
+
scale_x = 1.0
|
| 476 |
|
| 477 |
+
max_allowed_w = max(5.0, max_right - x0)
|
| 478 |
+
if (natural_len * scale_x) > max_allowed_w:
|
| 479 |
+
scale_x = max_allowed_w / natural_len if natural_len > 0 else 1.0
|
| 480 |
+
if scale_x < 0.60:
|
| 481 |
+
fontsize = max(4.5, fontsize * (scale_x / 0.60))
|
| 482 |
+
natural_len = font.text_length(text, fontsize=fontsize)
|
| 483 |
+
scale_x = max_allowed_w / natural_len if natural_len > 0 else 1.0
|
| 484 |
|
| 485 |
y_baseline = y1 - (box_h * 0.18)
|
| 486 |
point = fitz.Point(x0, y_baseline)
|
|
|
|
| 892 |
min_y = min(p0_pdf[1], p1_pdf[1], p2_pdf[1], p3_pdf[1])
|
| 893 |
max_y = max(p0_pdf[1], p1_pdf[1], p2_pdf[1], p3_pdf[1])
|
| 894 |
|
| 895 |
+
# Filter out scanner edge noise boxes outside page printable area
|
| 896 |
+
if min_x > (page.rect.width - 30.0) or max_x < 15.0 or min_y > (page.rect.height - 20.0):
|
| 897 |
+
continue
|
| 898 |
+
|
| 899 |
pdf_rect = fitz.Rect(min_x, min_y, max_x, max_y)
|
| 900 |
pdf_paddle_items.append({
|
| 901 |
"poly": (p0, p1, p2, p3),
|
|
|
|
| 1062 |
text = line[1][0]
|
| 1063 |
if not text.strip(): continue
|
| 1064 |
|
| 1065 |
+
xmin = min(p[0] for p in box) / zoom
|
| 1066 |
+
ymin = min(p[1] for p in box) / zoom
|
| 1067 |
+
xmax = max(p[0] for p in box) / zoom
|
| 1068 |
+
ymax = max(p[1] for p in box) / zoom
|
| 1069 |
+
|
| 1070 |
+
# Filter out scanner edge noise boxes outside page printable area
|
| 1071 |
+
if xmin > (page.rect.width - 30.0) or xmax < 15.0 or ymin > (page.rect.height - 20.0):
|
| 1072 |
+
continue
|
| 1073 |
+
|
| 1074 |
+
boxes_with_data.append(([xmin * zoom, ymin * zoom, xmax * zoom, ymax * zoom], (box, text)))
|
| 1075 |
|
| 1076 |
sorted_data = recursive_xy_cut(boxes_with_data)
|
| 1077 |
|