Margin detection update

This commit is contained in:
sebastien committed 2026-10-30 12:34:37 +01:00
1 parent 2c9dc65640
commit b295f871a7
6 files changed
+292 -68

No files matched your search

+68 -43
View File
@@ -10,7 +10,8 @@ from copienator.ink_detection import detect_bounds
ANALYSIS_DPI = 100
WIDTH_TOLERANCE = .08
# Copie01's name/class block reaches about 27% of the full page height.
# Start below it with clearance, including on later pages with repeated names.
# Fallback when name localization is unavailable. A localized first-page
# name/class bottom replaces this cutoff on all pages, including repeated names.
BODY_TOP = .30
BODY_BOTTOM = .82
LABEL_BOTTOM = .92
@@ -20,7 +21,10 @@ LABEL_MARGIN_GAP_MM = 10
DISCARD_LEFT_FRACTION = .10
STANDARD_MARGIN_MM = 40
MARGIN_CLEARANCE_MM = 20
MIN_MARGIN_MM = MARGIN_CLEARANCE_MM
# Minimum usable margin is independent of the strip clearance.
MIN_MARGIN_MM = 33
STANDARD_LINE_HEIGHT_MM = 8
MAX_EXCEPTION_LINES = 4
def trimmed_left_edge(starts: list[float]) -> float | None:
@@ -51,38 +55,60 @@ def handwriting_mask(rgb: np.ndarray) -> np.ndarray:
return keep[labels] & diagnostics['masks']['foreground']
def occupied_band_starts(mask: np.ndarray, bottom_fraction: float) -> list[float]:
def occupied_bands(mask: np.ndarray, bottom_fraction: float, top_fraction: float = BODY_TOP) -> list[dict]:
"""Ignore empty bands and writing starting beyond the page midpoint."""
height, width = mask.shape
band_height = max(1, round(4 * ANALYSIS_DPI / 25.4))
bottom = round(bottom_fraction * height)
starts = []
for y in range(round(BODY_TOP * height), bottom, band_height):
for y in range(round(top_fraction * height), bottom, band_height):
band = mask[y:min(y + band_height, bottom)]
xs = np.flatnonzero(band.sum(axis=0) >= 2)
if band.sum() >= 40 and len(xs) >= 12 and xs[0] <= width / 2:
starts.append(float(xs[0]) * 25.4 / ANALYSIS_DPI)
starts.append({'start_mm': float(xs[0]) * 25.4 / ANALYSIS_DPI,
'top_mm': y * 25.4 / ANALYSIS_DPI,
'bottom_mm': min(y + band_height, bottom) * 25.4 / ANALYSIS_DPI})
return starts
def body_edge(mask: np.ndarray) -> dict:
"""Keep the body decile; collect a wider 30–92% label-search window."""
starts = occupied_band_starts(mask, BODY_BOTTOM)
def occupied_band_starts(mask: np.ndarray, bottom_fraction: float, top_fraction: float = BODY_TOP) -> list[float]:
return [band['start_mm'] for band in occupied_bands(mask, bottom_fraction, top_fraction)]
def short_margin_exception(estimate: dict, margin: float) -> bool:
"""One block of at most four 8 mm lines may depart from an established margin."""
bands = estimate.get('body_bands', [])
low = [band for band in bands if band['start_mm'] < MIN_MARGIN_MM]
if not low or low[-1]['bottom_mm'] - low[0]['top_mm'] > MAX_EXCEPTION_LINES * STANDARD_LINE_HEIGHT_MM:
return False
# Require a majority of occupied bands outside that entire block, and a
# consistent decile there. Coordinates retain blank gaps between bands.
rest = [band['start_mm'] for band in bands
if band['bottom_mm'] <= low[0]['top_mm'] or band['top_mm'] >= low[-1]['bottom_mm']]
return (len(rest) >= 6 and len(rest) > len(bands) / 2
and abs(trimmed_left_edge(rest) - margin) <= COHERENT_MARGIN_TOLERANCE_MM)
def body_edge(mask: np.ndarray, top_fraction: float = BODY_TOP) -> dict:
"""Collect body and label bands below the name or the fallback header cutoff."""
bands = occupied_bands(mask, BODY_BOTTOM, top_fraction)
starts = [band['start_mm'] for band in bands]
return {
'edge_mm': trimmed_left_edge(starts) if len(starts) >= 6 else None,
'bands': len(starts),
'body_bands': bands,
'band_starts_mm': starts,
'label_band_starts_mm': occupied_band_starts(mask, LABEL_BOTTOM),
'label_band_starts_mm': occupied_band_starts(mask, LABEL_BOTTOM, top_fraction),
}
def estimate_page(image: Image.Image, render_dpi: int) -> dict:
def estimate_page(image: Image.Image, render_dpi: int, *, top_fraction: float = BODY_TOP) -> dict:
scale = ANALYSIS_DPI / render_dpi
small = image.convert('RGB').resize(
(max(1, round(image.width * scale)), max(1, round(image.height * scale))),
Image.Resampling.LANCZOS,
)
return body_edge(handwriting_mask(np.asarray(small)))
return body_edge(handwriting_mask(np.asarray(small)), top_fraction)
def format_groups(images: list[Image.Image], reference_width: float) -> list[list[int]]:
@@ -100,36 +126,34 @@ def format_groups(images: list[Image.Image], reference_width: float) -> list[lis
return groups
def analyse_margins(images: list[Image.Image], render_dpi: int, reference_width: float) -> dict:
def analyse_margins(images: list[Image.Image], render_dpi: int, reference_width: float,
*, name_bottom_height_fraction: float | None = None) -> dict:
"""Estimate body margins, then retain left labels supported across coherent pages."""
name_localized = (type(name_bottom_height_fraction) in (int, float)
and 0 < name_bottom_height_fraction <= 1)
analysis_top = float(name_bottom_height_fraction) if name_localized else BODY_TOP
starts = [0] * len(images)
groups = []
page_estimates = {}
def page_estimate(index):
if index not in page_estimates:
page_estimates[index] = estimate_page(images[index], render_dpi)
page_estimates[index] = (estimate_page(images[index], render_dpi, top_fraction=analysis_top)
if name_localized else estimate_page(images[index], render_dpi))
return page_estimates[index]
for indices in format_groups(images, reference_width):
# Spread samples through the copy rather than relying on the header page.
sample = sorted({indices[0], indices[min(1, len(indices) - 1)],
indices[len(indices) // 2], indices[-1]})
estimates = {index: page_estimate(index) for index in sample}
estimates = {index: page_estimate(index) for index in indices}
values = [item['edge_mm'] for item in estimates.values() if item['edge_mm'] is not None]
# Trim across the copy's format group, not separately on each page:
# one marginal comment must not veto the other pages' shared margin.
band_starts = [start for item in estimates.values() for start in item['band_starts_mm']]
status = 'insufficient-ink'
edge = trimmed_left_edge(band_starts)
edge = round(float(np.median(values)), 1) if values else None
left_mm = 0.0
if len(values) >= min(2, len(indices)):
status = 'near-edge' if edge < MIN_MARGIN_MM else 'margin'
left_mm = (0.0 if edge < MIN_MARGIN_MM else
max(edge / 2, edge - MARGIN_CLEARANCE_MM))
# Use ALL coherent pages, not just the four body-margin samples.
# A sparse/low/outlying page cannot supply evidence for this format's
# established body margin. Its normal fallback behaviour remains below.
# Only independently coherent pages supply evidence for labels in the margin.
coherent = [index for index in indices
if edge is not None and page_estimate(index)['edge_mm'] is not None
and page_estimate(index)['edge_mm'] >= MIN_MARGIN_MM
@@ -151,8 +175,18 @@ def analyse_margins(images: list[Image.Image], render_dpi: int, reference_width:
status = 'narrow-page'
left_mm = 0.0
start = round(left_mm * render_dpi / 25.4)
previous_coherent = 0
for index in indices:
starts[index] = start
estimate = page_estimate(index)
low = estimate['edge_mm'] is not None and estimate['edge_mm'] < MIN_MARGIN_MM
tolerated = (status == 'margin' and low and previous_coherent >= 2
and short_margin_exception(estimate, edge))
starts[index] = 0 if low and not tolerated else start
estimate['short_margin_exception'] = tolerated
estimate['decision'] = ('short-exception' if tolerated else
'page-near-edge' if low else status)
if index in coherent:
previous_coherent += 1
groups.append({
'pages': [index + 1 for index in indices],
'status': status,
@@ -164,22 +198,9 @@ def analyse_margins(images: list[Image.Image], render_dpi: int, reference_width:
'label_edge_mm': label_edge,
'label_bands': len(label_starts),
'bands': len(band_starts),
'discarded_bands': int(len(band_starts) * DISCARD_LEFT_FRACTION),
'aggregation': 'median-of-page-deciles',
'samples': [{'page': index + 1, **item} for index, item in estimates.items()],
})
# Inspect actual neighbours, including pages outside the format samples.
# Blank/uncertain pages do not establish or extend a no-margin run.
no_margin_run = 0
no_margin_from_page = None
for index in range(len(images)):
edge = page_estimate(index)['edge_mm']
no_margin_run = no_margin_run + 1 if edge is not None and edge < MIN_MARGIN_MM else 0
if no_margin_run >= 2:
run_start = index - 1
no_margin_from_page = run_start + 1
starts[run_start:] = [0] * (len(images) - run_start)
break
pages = []
for index in range(len(images)):
estimate = page_estimate(index)
@@ -188,14 +209,18 @@ def analyse_margins(images: list[Image.Image], render_dpi: int, reference_width:
'status': 'unknown' if edge is None else
('low' if edge < MIN_MARGIN_MM else 'normal')})
return {'version': 9, 'pages': pages, 'starts_px': starts, 'groups': groups,
'no_margin_from_page': no_margin_from_page,
return {'version': 13, 'pages': pages, 'starts_px': starts, 'groups': groups,
'no_margin_from_page': None,
'discard_left_fraction': DISCARD_LEFT_FRACTION, 'margin_clearance_mm': MARGIN_CLEARANCE_MM,
'cut_fraction_of_margin': .5, 'cut_rule': 'maximum',
'standard_margin_mm': STANDARD_MARGIN_MM, 'minimum_margin_mm': MIN_MARGIN_MM,
'body_height_fraction': [BODY_TOP, BODY_BOTTOM],
'label_height_fraction': [BODY_TOP, LABEL_BOTTOM],
'name_bottom_height_fraction': name_bottom_height_fraction if name_localized else None,
'top_cutoff_source': 'localized-name' if name_localized else 'fallback',
'body_height_fraction': [analysis_top, BODY_BOTTOM],
'label_height_fraction': [analysis_top, LABEL_BOTTOM],
'label_left_fraction': LABEL_LEFT_FRACTION,
'coherent_margin_tolerance_mm': COHERENT_MARGIN_TOLERANCE_MM,
'label_margin_gap_mm': LABEL_MARGIN_GAP_MM,
'maximum_band_start_width_fraction': .5}
'maximum_band_start_width_fraction': .5,
'standard_line_height_mm': STANDARD_LINE_HEIGHT_MM,
'max_exception_lines': MAX_EXCEPTION_LINES}