sundeep-news-scan/_replay_hstrip.py

25 lines
1.3 KiB
Python

import os,tempfile,contextlib,io,json,shutil
tempfile.tempdir=os.path.abspath("_tess_tmp"); os.environ["TMPDIR"]=tempfile.tempdir
os.makedirs("_tess_tmp",exist_ok=True)
from pathlib import Path
import smart_extractor as se
from _lines import separator_barriers
RUN=Path("output/AndhraJyothi_Siddipet District_20260602_20260603_144919"); D=RUN/"pages"
OUT=RUN/"all_article_crops_hstrip"; PAPER="andhra_jyothi"
if OUT.exists(): shutil.rmtree(OUT)
for pg in (1,2,3,4):
png=str(D/f"page_{pg:03d}.png")
regs=json.loads((D/f"page_{pg:03d}.regions.json").read_text())["regions"]
buf=io.StringIO()
with contextlib.redirect_stdout(buf):
regs=se.dedup_overlapping_regions(regs); regs=se.merge_stacked_title_lines(regs,png)
regs=se.drop_masthead_regions(regs,PAPER,pg,png); regs=se.promote_paragraph_titles(regs,png,PAPER)
bars=separator_barriers(png,regs)
ds=se.find_article_starts_by_dateline(regs,png,PAPER)
blocks=se.cluster_all_article_blocks(regs,ds,sep_lines=bars)
n=se.crop_all_article_blocks(png,regs,str(OUT),pg,dateline_starts=ds,sep_lines=bars)
ext=[l for l in buf.getvalue().splitlines() if "Headline-strip" in l]
print(f"page {pg}: {n} crops | {len(ext)} headline-strip extension(s)")
for l in ext: print(" ",l.strip())
print("DONE")