OnnxTR (Onnx Text Recognition)

repository·main·Indexed 41 days ago

https://github.com/felixdittrich92/onnxtr

A lightweight, high-performance OCR library and docTR ONNX-wrapper designed for document text recognition. It removes heavy dependencies like PyTorch or TensorFlow in favor of ONNX Runtime, offering faster inference, quantization support, and hardware acceleration via CUDA, OpenVINO, and CoreML. Features include layout analysis, table structure recognition, and a CLI for exporting results to Markdown, JSON, HTML, and XML.

Tokens
6.3K
Snippets
14
Records
21
Agent score
51%

What's inside OnnxTR

  1. Overview of OnnxTR

    main

    OnnxTR is an Optical Character Recognition (OCR) library that provides an efficient way to parse textual information (localizing and identifying words) from documents.

    It acts as a wrapper around the doctr library, specifically providing an ONNX-based pipeline. This approach offers several advantages over the base library:

    • No PyTorch or TensorFlow dependencies: It uses ONNX for inference.
    • Lightweight: Requires fewer resources.
    • Faster Inference: Optimized for lower latency.
    • Quantization Support: Includes 8-bit quantized models for faster inference on CPUs.
  2. Perform layout analysis and table recognition

    main

    When detect_layout=True and detect_tables=True are set in ocr_predictor, the resulting Document object contains structured information about the page layout and tables.

    • Layout Regions: Accessible via page.layout. Each region contains a type (e.g., Text, Title, Table, Picture), confidence, and geometry.
    • Tables: Accessible via page.tables. Words inside tables are regrouped into cells. Each table provides num_rows, num_cols, and a list of cells containing row_start, col_start, and value.

    Note: Enabling detect_tables automatically enables layout detection.

    from onnxtr.io import DocumentFile
    from onnxtr.models import ocr_predictor
    
    doc = DocumentFile.from_pdf("path/to/your/doc.pdf")
    
    model = ocr_predictor(detect_layout=True, detect_tables=True)
    result = model(doc)
    
    page = result.pages[0]
    
    # Layout regions
    for region in page.layout:
        print(region.type, region.confidence, region.geometry)
    
    # Tables
    for table in page.tables:
        print(f"{table.num_rows}x{table.num_cols}")
        for cell in table.cells:
            print(cell.row_start, cell.col_start, cell.value)
  3. Run the OnnxTR OCR demo locally

    main

    To run the OnnxTR OCR demonstration on your local machine, navigate to the demo directory, install the necessary dependencies using pip, and execute the Gradio application with app.py.

    cd demo
    pip install -r requirements.txt
    python3 app.py
  4. Install OnnxTR with various execution providers

    main

    Install OnnxTR using pip with specific extras depending on your hardware and requirements.

    Hardware Recommendations:

    • NVIDIA GPU: Use gpu variants.
    • Intel CPU/GPU: Use openvino variants.
    • Apple Silicon Mac: Use cpu variants (CoreML is auto-detected).
    • Other: Use cpu variants.

    Note: For GPU support, CUDA and cuDNN must be installed on your system before installing the package. For OpenVINO, the default device is CPU with FP32 precision; configuration can be adjusted via ONNX Runtime settings.

    # standard cpu support
    pip install "onnxtr[cpu]"
    pip install "onnxtr[cpu-headless]"
    
    # with gpu support
    pip install "onnxtr[gpu]"
    pip install "onnxtr[gpu-headless]"
    
    # OpenVINO cpu | gpu support for Intel CPUs | GPUs
    pip install "onnxtr[openvino]"
    pip install "onnxtr[openvino-headless]"
    
    # with HTML support
    pip install "onnxtr[html]"
    
    # with support for visualization
    pip install "onnxtr[viz]"
    
    # with support for all dependencies
    pip install "onnxtr[html, gpu, viz]"
  5. Use the OnnxTR CLI for OCR

    main

    The OnnxTR CLI allows you to perform OCR on images, PDFs, or web pages (rendered as PDFs) and export the results in various formats. You can specify architectures for detection, recognition, and layout, as well as control hardware acceleration via execution providers.

    Basic Usage:

    # Perform OCR on an image and save as JSON
    onnxtr --input_path image.png --output results.json
    
    # Perform OCR on a PDF with specific layout detection and table extraction
    onnxtr --input_path document.pdf --detect_layout --detect_tables --output results.md
    
    # Use GPU acceleration
    onnxtr --input_path image.png --device cuda --output results.json
    onnxtr --input_path image.png --output results.json
  6. Load models from the HuggingFace Hub

    main

    OnnxTR supports seamless integration with the HuggingFace Hub. You can use from_hub to download and load models directly.

    • To use a recognition model from the hub: pass it to the reco_arch parameter of ocr_predictor.
    • To use a detection model from the hub: pass it to the det_arch parameter of ocr_predictor.
    from onnxtr.io import DocumentFile
    from onnxtr.models import ocr_predictor, from_hub
    
    img = DocumentFile.from_images(["<image_path>"])
    # Load your model from the hub
    model = from_hub("onnxtr/my-model")
    
    # Pass it to the predictor
    # If your model is a recognition model:
    predictor = ocr_predictor(det_arch="db_mobilenet_v3_large", reco_arch=model)
    
    # If your model is a detection model:
    predictor = ocr_predictor(det_arch=model, reco_arch="crnn_mobilenet_v3_small")
    
    # Get your predictions
    res = predictor(img)
  7. Configure advanced engine settings with EngineConfig

    main

    You can pass custom ONNX Runtime SessionOptions and providers to the model engines using EngineConfig. This allows for fine-grained control over hardware acceleration (e.g., specifying CUDA device IDs) and memory management.

    To dynamically manage memory, you can provide a run_options_provider callback to EngineConfig that modifies RunOptions during inference.

    from onnxruntime import SessionOptions
    
    from onnxtr.models import ocr_predictor, EngineConfig
    
    general_options = (
        SessionOptions()
    )  # For configuartion options see: https://onnxruntime.ai/docs/api/python/api_summary.html#sessionoptions
    general_options.enable_cpu_mem_arena = False
    
    # NOTE: The following would force to run only on the GPU if no GPU is available it will raise an error
    # List of strings e.g. ["CUDAExecutionProvider", "CPUExecutionProvider"] or a list of tuples with the provider and its options e.g.
    # [("CUDAExecutionProvider", {"device_id": 0}), ("CUDAExecutionProvider", {"arena_extend_strategy": "kSameAsRequested"})]
    providers = [
        ("CUDAExecutionProvider", {"device_id": 0, "cudnn_conv_algo_search": "DEFAULT"})
    ]  # For available providers see: https://onnxruntime.ai/docs/execution-providers/
    
    engine_config = EngineConfig(session_options=general_options, providers=providers)
    # We use the default predictor with the custom engine configuration
    # NOTE: You can define different engine configurations for detection, recognition and classification depending on your needs
    predictor = ocr_predictor(det_engine_cfg=engine_config, reco_engine_cfg=engine_config, clf_engine_cfg=engine_config)
  8. Export OCR results to various formats

    main

    The Document object returned by the model can be exported into several formats using the export_as() method or specialized methods.

    Supported Formats via export_as(format, ...):

    • markdown (or md): Can specify direction (e.g., "auto", "ltr", "rtl", "ttb-rtl", "ttb-ltr").
    • asciidoc (or adoc)
    • html
    • xml (hOCR format)
    • text (or txt)
    • json (or dict)

    Other Export Methods:

    • result.export(): Returns a nested dictionary.
    • result.render(): Returns human-readable text.
    • result.export_as_xml(): Returns XML (hOCR format) as a list of (bytes, ElementTree) tuples per page.
    result.export_as("markdown")  # or "md"
    result.export_as("asciidoc")  # or "adoc"
    result.export_as("html")
    result.export_as("xml")  # hOCR, one (bytes, ElementTree) tuple per page
    result.export_as("text")  # or "text"
    result.export_as("json")  # or "dict"
    
    # Reading order can be resolved on export
    result.export_as("markdown", direction="auto")  # auto | ltr | rtl | ttb-rtl | ttb-ltr
  9. Push models to the HuggingFace Hub

    main

    You can upload your own trained OnnxTR models to the HuggingFace Hub using push_to_hf_hub. You must first authenticate using login_to_hub(). When pushing, specify the task (e.g., detection, recognition, or classification) and the arch (the model architecture name).

    from onnxtr.models import parseq, push_to_hf_hub, login_to_hub
    from onnxtr.utils.vocabs import VOCABS
    
    # Login to the hub
    login_to_hub()
    
    # Recognition model
    model = parseq("~/onnxtr-parseq-multilingual-v1.onnx", vocab=VOCABS["multilingual"])
    push_to_hf_hub(
        model,
        model_name="onnxtr-parseq-multilingual-v1",
        task="recognition",  # The task for which the model is intended [detection, recognition, classification]
        arch="parseq",  # The name of the model architecture
        override=False,  # Set to `True` if you want to override an existing model / repository
    )
    
    # Detection model
    model = linknet_resnet18("~/onnxtr-linknet-resnet18.onnx")
    push_to_hf_hub(model, model_name="onnxtr-linknet-resnet18", task="detection", arch="linknet_resnet18", override=True)
  10. Load custom exported docTR models

    main

    You can load custom ONNX models exported from docTR into OnnxTR. Use the specific model functions from onnxtr.models to initialize the architecture, then compose them using ocr_predictor.

    from onnxtr.models import ocr_predictor, linknet_resnet18, parseq
    
    reco_model = parseq("path_to_custom_model.onnx", vocab="ABC")
    det_model = linknet_resnet18("path_to_custom_model.onnx")
    model = ocr_predictor(det_arch=det_model, reco_arch=reco_model)
  11. Read documents using DocumentFile

    main

    Use the DocumentFile class from onnxtr.io to load different file types into the OCR pipeline.

    • from_pdf(path): Load a PDF file.
    • from_images(path_or_list): Load a single image or a list of images (for multi-page processing).
    • from_url(url): Load a webpage (requires weasyprint to be installed).
    from onnxtr.io import DocumentFile
    
    # PDF
    pdf_doc = DocumentFile.from_pdf("path/to/your/doc.pdf")
    # Image
    single_img_doc = DocumentFile.from_images("path/to/your/img.jpg")
    # Webpage (requires `weasyprint` to be installed)
    webpage_doc = DocumentFile.from_url("https://www.yoursite.com")
    # Multiple page images
    multi_img_doc = DocumentFile.from_images(["path/to/page1.jpg", "path/to/page2.jpg"])