# Copyright (c) 2026 LlamaIndex, Inc. All rights reserved. # Licensed under the OCRia-1 Model and Code License (LICENSE at the root of this repository). # This is not open-source software: see the license for permitted uses. """Transcribe one page with OCRia-1 and draw its layout boxes (experimental). pip install "transformers==5.22.2" accelerate torchvision timm scipy pypdfium2 huggingface_hub python examples/boxes.py doc.pdf --page 0 # a PDF page: exact word positions from its text layer python examples/boxes.py page.png # an image: word positions from the model's attention python examples/boxes.py page.png --attention-only # no layout detector (less accurate) Writes .boxes.png and .boxes.json next to the input. Needs a CUDA GPU for OCRia-1; the Heron layout detector (docling-project/docling-layout-heron, Apache-2.0) is downloaded from its own repository on first use. Run it from a download of this repository (huggingface-cli download llamaindex/OCRia-1 --local-dir OCRia-1); the weights come from the Hub (or a local copy: OCRIA_MODEL=/path/to/OCRia-1). """ import argparse import json import os import sys import time from pathlib import Path from PIL import Image, ImageDraw sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) # the repository root holds ocria_grounding/ from ocria_grounding import MODEL, ground_layout, load, load_detector, load_fast_layout, render_pdf_page, transcribe # noqa: E402 COLORS = {"Section-header": "#d62728", "Title": "#d62728", "Text": "#1f77b4", "List-item": "#2ca02c", "Table": "#ff7f0e", "Picture": "#9467bd", "Caption": "#8c564b", "Footnote": "#8c564b", "Page-header": "#7f7f7f", "Page-footer": "#7f7f7f"} def main() -> None: ap = argparse.ArgumentParser() ap.add_argument("path", help="a PDF or a page image") ap.add_argument("--page", type=int, default=0, help="PDF page index (0-based)") ap.add_argument("--attention-only", action="store_true", help="skip the layout detector") ap.add_argument("--layout", choices=["accurate-layout", "fast-layout"], default="accurate-layout") args = ap.parse_args() if args.path.lower().endswith(".pdf"): image, words = render_pdf_page(args.path, args.page, dpi=300) else: image, words = Image.open(args.path).convert("RGB"), None g = load(os.environ.get("OCRIA_MODEL", MODEL)) fast = load_fast_layout() if args.layout == "fast-layout" else None det = None if args.attention_only or fast is not None else load_detector() t0 = time.perf_counter() answer = transcribe(g, image) t1 = time.perf_counter() page = ground_layout(g, image, answer, detector=det, words=words if fast is None else None, method=args.layout, fast_backend=fast) t2 = time.perf_counter() out = image.copy() draw = ImageDraw.Draw(out) width = max(2, image.width // 500) pad = width + 2 # draw outside the box so the outline never covers the first or last letter for it in page["items"]: color = COLORS.get(it["type"], "#17becf") x0, y0, x1, y1 = it["box"] draw.rectangle([x0 - pad, y0 - pad, x1 + pad, y1 + pad], outline=color, width=width) tw, th = draw.textbbox((0, 0), it["type"])[2:] # the type label sits above the box, on a white tab ty = max(0, y0 - pad - th - 2) draw.rectangle([x0 - pad, ty, x0 - pad + tw + 4, ty + th + 2], fill="white") draw.text((x0 - pad + 2, ty), it["type"], fill=color) stem = Path(args.path).with_suffix("") if args.path.lower().endswith(".pdf"): stem = Path(f"{stem}.p{args.page}") out.save(f"{stem}.boxes.png") Path(f"{stem}.boxes.json").write_text(json.dumps({"answer": answer, **page}, indent=1)) print(f"{len(page['items'])} items -> {stem}.boxes.png (transcription {t1 - t0:.1f} s, boxes {t2 - t1:.1f} s)") if __name__ == "__main__": main()