"""Find text lines on a page picture → boxes (x, y, w, h), top to bottom. 1. adaptive threshold → ink pixels (works on old yellow paper, shadows, light-on-dark) 2. letters = connected ink blobs; a high percentile of their heights ≈ letter height h (vowel marks, dots and noise are small, so a plain median would be too low) 3. smear ink sideways and a little up/down (vowels and subscripts sit above / below the letters) 4. each smeared blob = a piece of a line; pieces on the same row with a gap < 3 h are joined (Khmer leaves wide spaces between phrases), so only real columns stay apart 5. tiny pieces (noise, a photo's texture) are dropped; you can fix boxes by hand in the app """ from __future__ import annotations import cv2 import numpy as np def _ink(g): blur = cv2.GaussianBlur(g, (3, 3), 0) block = max(15, (min(g.shape) // 20) | 1) ink = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, block, 15) if ink.mean() / 255 >= 0.5: # light text on a dark background return 255 - ink # real ink is DARK; show-through from the back of the page and stains are light grey → not ink. # keep only pixels darker than halfway between the paper and the darkest ink dark, paper = np.percentile(blur, 1), np.median(blur) if paper - dark > 60: ink[blur > dark + 0.55 * (paper - dark)] = 0 return ink def find_lines(rgb: np.ndarray) -> list[tuple[int, int, int, int]]: g = cv2.cvtColor(rgb, cv2.COLOR_RGB2GRAY) H, W = g.shape scale = min(1.0, 2000 / max(H, W)) if scale < 1: g = cv2.resize(g, (int(W * scale), int(H * scale)), interpolation=cv2.INTER_AREA) ink = _ink(g) n, lab, st, _ = cv2.connectedComponentsWithStats(ink, 8) hs, ws, areas = st[1:, 3], st[1:, 2], st[1:, 4] real = areas >= 12 if real.sum() < 3: return [(0, 0, W, H)] h = float(np.percentile(hs[real], 70)) bad = np.zeros(n, bool) bad[1:] = (areas < 4) | (hs > 4 * h) | (ws > 0.8 * g.shape[1]) | (hs > 0.5 * g.shape[0]) clean = ink.copy() clean[bad[lab]] = 0 kx, ky = max(3, int(0.9 * h)), max(1, int(0.3 * h)) smear = cv2.dilate(clean, cv2.getStructuringElement(cv2.MORPH_RECT, (kx, ky))) _, lab2, st2, _ = cv2.connectedComponentsWithStats(smear, 8) pieces = [] for i, (x, y, w_, h_, a) in enumerate(st2[1:], 1): x0, x1 = x + kx // 2, x + w_ - kx // 2 y0, y1 = y + ky // 2, y + h_ - ky // 2 if y1 - y0 < 0.5 * h or x1 - x0 < 0.6 * h: continue inkpx = int((clean[y0:y1, x0:x1] > 0).sum()) if inkpx < 0.7 * h * h or y1 - y0 < 0.7 * h: # specks, show-through dots, dust: not text continue pieces.append([x0, y0, x1, y1]) # join pieces of the same row pieces.sort(key=lambda p: p[0]) rows = [] for p in pieces: for r in rows: ov = min(r[3], p[3]) - max(r[1], p[1]) if ov > 0.5 * min(r[3] - r[1], p[3] - p[1]) and p[0] - r[2] < 3 * h: r[0], r[1], r[2], r[3] = min(r[0], p[0]), min(r[1], p[1]), max(r[2], p[2]), max(r[3], p[3]) break else: rows.append(list(p)) # drop lonely small pieces (noise): narrower than 2.5 letters and not tall enough to be a line rows = [r for r in rows if r[2] - r[0] >= 2.5 * h or r[3] - r[1] >= 0.9 * h] def split(r): """A box taller than ~3.6 letters is usually two lines touching (subscripts reach the next line): cut it at the emptiest pixel row in its middle part.""" x0, y0, x1, y1 = r if y1 - y0 <= 3.6 * h: return [r] prof = (clean[y0:y1, x0:x1] > 0).sum(1).astype(float) lo, hi = int(1.6 * h), len(prof) - int(1.6 * h) if hi <= lo: return [r] cut = lo + int(np.argmin(prof[lo:hi])) return split([x0, y0, x1, y0 + cut]) + split([x0, y0 + cut, x1, y1]) rows = [p for r in rows for p in split(r)] rows = [r for r in rows if r[3] - r[1] >= 0.5 * h] pad = max(2, int(0.25 * h)) s = 1 / scale out = [] for x0, y0, x1, y1 in rows: x0, y0 = max(0, x0 - pad), max(0, y0 - pad) x1, y1 = min(g.shape[1], x1 + pad), min(g.shape[0], y1 + pad) out.append((int(x0 * s), int(y0 * s), int((x1 - x0) * s), int((y1 - y0) * s))) out.sort(key=lambda b: (b[1] + b[3] / 2) // max(1, int(0.8 * h / scale)) * 100000 + b[0]) return out