Build a Complete OCR Pipeline with Baidu's Unlimited-OCR for High-Resolution Documents and Multi-Page PDFs

baidu unlimited-ocrend-to-end ocrgpu inferencehigh-resolution document parsingmulti-page pdf ocrocr pipelinevision-language model

Optical character recognition (OCR) remains a cornerstone of document digitization, but traditional OCR systems often struggle with high-resolution images, complex layouts, or multi-page PDFs. Baidu's Unlimited-OCR, a 3B-parameter vision-language model, addresses these challenges by combining visual and linguistic understanding for superior text extraction. This tutorial walks through building an end-to-end OCR pipeline optimized for high-resolution images and multi-page documents, with guidance relevant to 2026 hardware and software ecosystems.


Setting Up the Environment


Start by configuring a GPU environment—NVIDIA A100 or H100 with at least 24 GB VRAM is recommended for optimal 3B-model inference. Ensure CUDA 12.1+ and PyTorch 2.2+ are installed. Install required dependencies:


pip install transformers accelerate torchvision pillow pdf2image pypdf2

For multi-page PDFs, install Poppler (system dependency) and pdf2image:

# Ubuntu/Debian
sudo apt-get install poppler-utils
# macOS
brew install poppler

Loading the Unlimited-OCR Model


Baidu’s Unlimited-OCR is available on Hugging Face. Below is an optimized loading script with automatic precision selection:


import torch
from transformers import AutoModelForVision2Seq, AutoProcessor

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
dtype = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16

model = AutoModelForVision2Seq.from_pretrained(
    "baidu/Unlimited-OCR",
    torch_dtype=dtype,
    device_map="auto"
)
processor = AutoProcessor.from_pretrained("baidu/Unlimited-OCR")

Processing High-Resolution Images


High-resolution documents—such as scanned contracts or maps—benefit from patch-based processing to stay within VRAM limits:


from PIL import Image

def process_image(image_path, max_patch_size=1024):
    image = Image.open(image_path).convert("RGB")
    # Resize for uniform patch processing while preserving aspect ratio
    w, h = image.size
    if max(w, h) > max_patch_size:
        scale = max_patch_size / max(w, h)
        image = image.resize((int(w * scale), int(h * scale)))
    
    inputs = processor(images=image, return_tensors="pt").to(device, dtype)
    with torch.no_grad():
        outputs = model.generate(**inputs, max_new_tokens=1024)
    return processor.decode(outputs[0], skip_special_tokens=True)

Parsing Multi-Page PDFs


Convert each page to an image before OCR. The pipeline preserves page order and aggregates results:


from pdf2image import convert_from_path

def parse_pdf(pdf_path, dpi=300):
    images = convert_from_path(pdf_path, dpi=dpi)  # High DPI for accuracy
    full_text = []
    for i, img in enumerate(images):
        # Convert PIL to processor format
        inputs = processor(images=img, return_tensors="pt").to(device, dtype)
        with torch.no_grad():
            outputs = model.generate(**inputs, max_new_tokens=1024)
        page_text = processor.decode(outputs[0], skip_special_tokens=True)
        full_text.append(f"--- Page {i+1} ---\n{page_text}")
    return "\n\n".join(full_text)

Optimizing for Production (2026 Best Practices)


In 2026, modern OCR pipelines leverage:

  • Mixed precision inference: Use torch.compile() for 20-30% speedup on supported GPUs.
  • Memory-efficient attention: Unlimited-OCR supports Flash Attention v2; enable via model.generationconfig.useflash_attention = True.
  • Batch processing: For document batches, pad images for GPU-efficient parallel inference.
  • Distributed inference: For massive PDFs (100+ pages), use accelerate with multiple GPUs.

Putting It All Together


Below is a complete script that accepts either an image or PDF path and outputs structured text:


import sys

def main(path):
    if path.lower().endswith(".pdf"):
        result = parse_pdf(path)
    else:
        result = process_image(path)
    print(result)

if __name__ == "__main__":
    main(sys.argv[1])

Conclusion


Baidu's Unlimited-OCR sets a new standard for document parsing by excelling where traditional OCR fails—dense layouts, handwriting overlays, and multi-page PDFs. By following this pipeline, developers can integrate high-accuracy OCR into applications ranging from document archiving to automated data entry. With 2026 hardware optimizations, inference latency is now under 2 seconds per page on premium GPUs, making real-time document digitization feasible at scale.

via MarkTechPost

Related