Rapid LaTeX OCR

repository·main·Indexed 19 days ago

https://github.com/rapidai/rapidlatexocr

A high-performance tool for converting formula images into LaTeX format. It is a deployment-optimized version of LaTeX-OCR that utilizes ONNX and OpenVINO for faster inference. The library provides a Python API via the LaTeXOCR class and a command-line interface for image-to-LaTeX conversion.

Tokens
1.4K
Snippets
5
Records
8
Agent score
14%

What's inside rapidlatexocr

  1. Understand the Rapid LaTeX OCR framework

    main

    Rapid LaTeX OCR is a deployment-optimized version of LaTeX-OCR. It uses ONNXRuntime or OpenVINO for inference with models converted to ONNX format.

    Key distinctions:

    • Inference only: This repository contains simplified reasoning code for faster and easier deployment. It does not contain training code.
    • Model format: All models are provided in ONNX format.
    • Training: If you need to train a custom model, you must use the original LaTeX-OCR repository.
  2. Install rapid_latex_ocr

    main

    Install the package using pip. Note that the model files will be automatically downloaded and placed in a models directory within the installation path. If you encounter slow download speeds, you can download the models manually via Google Drive or Baidu NetDisk.

    pip install rapid_latex_ocr
  3. Use LaTeXOCR in a Python script

    main

    To use the OCR functionality within your Python code, import the LaTeXOCR class. You can pass raw image bytes to the model instance. The model returns a tuple containing the predicted LaTeX string and the inference elapsed time.

    from rapid_latex_ocr import LaTeXOCR
    
    model = LaTeXOCR()
    
    img_path = "tests/test_files/6.png"
    with open(img_path, "rb") as f:
        data = f.read()
    
    res, elapse = model(data)
    
    print(res)
    print(elapse)
  4. Use rapid_latex_ocr via Command Line Interface

    main

    You can run the OCR tool directly from the terminal by passing the path to an image file as an argument. The tool will output the predicted LaTeX string followed by the inference time.

    $ rapid_latex_ocr tests/test_files/6.png
    
    # Output example:
    # {\frac{x^{2}}{a^{2}}}-{\frac{y^{2}}{b^{2}}}=1
    # 0.47902780000000034
  5. Initialize the LaTeXOCR engine

    main

    To use the LaTeXOCR engine, instantiate the LaTeXOCR class. If you do not provide paths to the model files, the class will attempt to download the required components (image_resizer.onnx, encoder.onnx, decoder.onnx, and tokenizer.json) to a local models directory automatically.

    Initialization Parameters

    • config_path (Union[str, Path]): Path to a YAML configuration file. Defaults to config.yaml in the current directory.
    • image_resizer_path (Union[str, Path]): Path to the image resizer ONNX model.
    • encoder_path (Union[str, Path]): Path to the encoder ONNX model.
    • decoder_path (Union[str, Path]): Path to the decoder ONNX model.
    • tokenizer_json (Union[str, Path]): Path to the tokenizer JSON file.
    ```python
    from rapid_latex_ocr.main import LaTeXOCR
    
    # Automatic download mode
    engine = LaTeXOCR()
    
    # Manual path mode
    engine = LaTeXOCR(
        image_resizer_path="/path/to/resizer.onnx",
        encoder_path="/path/to/encoder.onnx",
        decoder_path="/path/to/decoder.onnx",
        tokenizer_json="/path/to/tokenizer.json"
    )
    ```埋
  6. Convert an image to LaTeX using LaTeXOCR

    main

    The LaTeXOCR class is callable. You can pass an image path or an image object directly to the instance to perform OCR.

    Call Signature

    __call__(self, img: InputType) -> Tuple[str, float]

    • img: An InputType (typically a file path string or a compatible image object).
    • Returns: A tuple containing:
      • pred (str): The predicted LaTeX string.
      • elapse (float): The time taken for the inference in seconds.

    Example

    from rapid_latex_ocr.main import LaTeXOCR
    
    engine = LaTeXOCR()
    latex_string, duration = engine("path/to/formula_image.png")
    print(f"LaTeX: {latex_string}")
    print(f"Time: {duration:.4f}s")
  7. Use LaTeXOCR via Command Line Interface

    main

    You can run the OCR engine directly from the terminal by providing an image path. You can also optionally specify paths to specific model files.

    Usage

    python -m rapid_latex_ocr.main <img_path> [options]

    Arguments

    • img_path (positional): Path to the image of the formula.
    • -img_resizer, --image_resizer_path: Path to the image resizer model.
    • -encdoer, --encoder_path: Path to the encoder model.
    • -decoder, --decoder_path: Path to the decoder model.
    • -tokenizer, --tokenizer_json: Path to the tokenizer JSON file.

    Example

    python -m rapid_latex_ocr.main my_formula.png
  8. Configure LaTeXOCR via LaTeXOCRInput

    main

    The LaTeXOCRInput dataclass defines the hyperparameters and constraints used by the engine. These values are typically loaded from a config.yaml file during LaTeXOCR initialization.

    Fields

    FieldTypeDefaultDescription
    max_widthint672Maximum width for processing
    max_heightint192Maximum height for processing
    min_heightint32Minimum height for processing
    min_widthint32Minimum width for processing
    bos_tokenint1Beginning of sequence token ID
    max_seq_lenint512Maximum sequence length
    eos_tokenint2End of sequence token ID
    temperaturefloat0.00001Sampling temperature