feat: detect and remove page-border decoration strip artifacts (Step 4e)

Textbooks with decorative alphabet strips along page edges produce
OCR artifacts (scattered colored letters at x<150 while real content
starts at x>=179). Step 4e detects a significant x-gap (>30px) between
a small cluster (<15% of total word_boxes) near the page edge and the
main content, then removes the border-strip word_boxes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Benjamin Admin
2026-03-21 17:20:45 +01:00
parent f31a7175a2
commit c0e1118870
2 changed files with 153 additions and 0 deletions

View File

@@ -1093,3 +1093,96 @@ class TestWordBoxReadingOrder:
assert [w["text"] for w in sorted_wbs] == ["tie", "sb/sth", "up"]
# Same objects, same order
assert [id(w) for w in sorted_wbs] == [id(w) for w in wbs]
# ---------------------------------------------------------------------------
# Border strip detection (Step 4e)
# ---------------------------------------------------------------------------
class TestBorderStripFilter:
"""Verify decorative page-border word_boxes are detected and removed."""
@staticmethod
def _make_wb(text, left, top, width=50, height=20, conf=95):
return {"text": text, "left": left, "top": top,
"width": width, "height": height, "conf": conf}
def test_left_border_strip_removed(self):
"""Word_boxes at x<120 with 45px gap to content at x>=179 are removed."""
# Simulate border strip (11 wbs) + real content (20 wbs)
border_wbs = [
self._make_wb("M", 49, 436, 46, 44),
self._make_wb("x", 113, 610, 21, 38),
self._make_wb("Er", 45, 998, 62, 37),
]
content_wbs = []
for i in range(20):
content_wbs.append(self._make_wb(f"word{i}", 179 + (i % 3) * 100, 100 + i * 40))
# Build zone with cells
cells = []
# Border-only cells
for i, wb in enumerate(border_wbs):
cells.append({"cell_id": f"R{i}_C0", "col_index": 0, "row_index": i,
"word_boxes": [wb], "text": wb["text"]})
# Content cells
for i, wb in enumerate(content_wbs):
ri = len(border_wbs) + i
cells.append({"cell_id": f"R{ri}_C1", "col_index": 1, "row_index": ri,
"word_boxes": [wb], "text": wb["text"]})
zone = {"zone_index": 0, "zone_type": "content", "cells": cells,
"columns": [], "rows": []}
# The filter runs inside _build_grid_core, but we can test the
# pattern detection logic: 3 border wbs + 20 content wbs,
# border right edge = 113+21=134, content left = 179, gap = 45px
# 3/23 = 13% < 15% threshold
from cv_ocr_engines import _group_words_into_lines
all_left = sorted(
[(wb["left"], wb) for cell in cells for wb in cell.get("word_boxes", [])],
key=lambda t: t[0]
)
# Find largest gap
best_gap = 0
best_idx = -1
for gi in range(len(all_left) - 1):
right_edge = all_left[gi][0] + all_left[gi][1]["width"]
gap = all_left[gi + 1][0] - right_edge
if gap > best_gap:
best_gap = gap
best_idx = gi
assert best_gap >= 30, f"Gap should be >=30, got {best_gap}"
left_count = best_idx + 1
total = len(all_left)
assert left_count / total < 0.15, f"Border ratio {left_count}/{total} should be <15%"
def test_no_removal_when_no_gap(self):
"""No gap > 30px between word_boxes → nothing removed."""
wbs = [self._make_wb(f"w{i}", 10 + i * 20, 100) for i in range(15)]
all_left = sorted([(wb["left"], wb) for wb in wbs], key=lambda t: t[0])
best_gap = 0
for gi in range(len(all_left) - 1):
right_edge = all_left[gi][0] + all_left[gi][1]["width"]
gap = all_left[gi + 1][0] - right_edge
if gap > best_gap:
best_gap = gap
assert best_gap < 30, f"No significant gap expected, got {best_gap}"
def test_equal_sides_not_removed(self):
"""Two roughly equal groups (50/50) are NOT treated as border strip."""
left_wbs = [self._make_wb(f"L{i}", 10 + i * 10, 100 + i * 30) for i in range(10)]
right_wbs = [self._make_wb(f"R{i}", 200 + i * 10, 100 + i * 30) for i in range(10)]
all_left = sorted(
[(wb["left"], wb) for wb in left_wbs + right_wbs],
key=lambda t: t[0]
)
best_gap = 0
best_idx = -1
for gi in range(len(all_left) - 1):
right_edge = all_left[gi][0] + all_left[gi][1]["width"]
gap = all_left[gi + 1][0] - right_edge
if gap > best_gap:
best_gap = gap
best_idx = gi
left_count = best_idx + 1
total = len(all_left)
# 10/20 = 50% — NOT below 15% threshold, so no removal
assert left_count / total >= 0.15, "Equal groups should NOT trigger border removal"