sundeep-news-scan/_vwalls_test.py

63 lines
2.7 KiB
Python

"""TEST (read-only): identify & map the BIG vertical lines on a page — the long column
dividers that separate articles — ignoring short verticals (in-article box edges, stray
strokes). Draws a blank-canvas 'vertical wall map' per page. No smart_extractor changes,
no API. Test on 1-2 pages only."""
import os, tempfile, json
tempfile.tempdir = os.path.abspath("_tess_tmp"); os.environ["TMPDIR"] = tempfile.tempdir
os.makedirs("_tess_tmp", exist_ok=True)
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont
from _lines import detect_separator_lines
# A vertical line counts as a BIG article-separating wall when it runs at least this
# fraction of the page height. Column dividers span most of a column; box edges are short.
BIG_FRAC = 0.45
def big_vertical_walls(png_path, min_frac=BIG_FRAC):
res = detect_separator_lines(png_path)
W, H = res["size"]
walls = []
for L in res["v"]:
y1, y2 = min(L["y1"], L["y2"]), max(L["y1"], L["y2"])
length = y2 - y1
if length >= min_frac * H:
walls.append({"x": (L["x1"] + L["x2"]) // 2, "y1": y1, "y2": y2,
"len": length, "frac": round(length / H, 2)})
walls.sort(key=lambda w: w["x"])
return walls, (W, H)
def draw_walls(png_path, out_path):
walls, (W, H) = big_vertical_walls(png_path)
res = detect_separator_lines(png_path) # for the 'ignored' short ones
small = [L for L in res["v"]
if (max(L["y1"], L["y2"]) - min(L["y1"], L["y2"])) < BIG_FRAC * H]
img = Image.new("RGB", (W, H), "white")
d = ImageDraw.Draw(img)
try:
font = ImageFont.truetype("DejaVuSans-Bold.ttf", 30)
except Exception:
font = ImageFont.load_default()
# ignored short verticals = thin gray
for L in small:
d.line([ (L["x1"]+L["x2"])//2, min(L["y1"],L["y2"]),
(L["x1"]+L["x2"])//2, max(L["y1"],L["y2"]) ], fill=(200,200,200), width=3)
# big walls = thick red, labelled with height fraction
for w in walls:
d.line([w["x"], w["y1"], w["x"], w["y2"]], fill=(220, 30, 30), width=10)
d.text((w["x"] + 14, w["y1"] + 10), f'WALL x={w["x"]} {int(w["frac"]*100)}%H',
fill=(220, 30, 30), font=font)
img.save(out_path)
return walls
TESTS = [
("output/NamastheTelangana_Siddipet_20260602_20260604_214937/pages", 2, "NT"),
("output/AndhraJyothi_Siddipet District_20260602_20260603_144919/pages", 1, "AJ"),
]
for pdir, pg, tag in TESTS:
png = f"{pdir}/page_{pg:03d}.png"
out = f"{pdir}/page_{pg:03d}_vwalls_debug.png"
walls = draw_walls(png, out)
print(f"{tag} pg{pg}: {len(walls)} BIG vertical wall(s) -> {out}")
for w in walls:
print(f" x={w['x']:>4} y[{w['y1']},{w['y2']}] height={int(w['frac']*100)}% of page")