lukas-blecher /LaTeX-OCR

pix2tex: Using a ViT to convert images of equations into LaTeX code.

★ 16,621⑂ 1,301👁 85⑃ 79● 299▤ 7
PythonMITCreated 2020-12-11lukas-blecher.github.io/LaTeX-OCR/
下载:0.0.310.0.31 ↓0.0.312023-04-13
+0
日增长
16,621
stars
1,301
forks
85
watchers
79
prs
299
issues
7
releases

README

pix2tex - LaTeX OCR

       

The goal of this project is to create a learning based system that takes an image of a math formula and returns corresponding LaTeX code. 

!header

 Using the model
To run the model you need Python 3.7+

If you don't have PyTorch installed. Follow their instructions here.

Install the package pix2tex: 


pip install "pix2tex[gui]"


Model checkpoints will be downloaded automatically.

There are three ways to get a prediction from an image. 
1. You can use the command line tool by calling pix2tex. Here you can parse already existing images from the disk and images in your clipboard.

2. Thanks to @katie-lim, you can use a nice user interface as a quick way to get the model prediction. Just call the GUI with latexocr. From here you can take a screenshot and the predicted latex code is rendered using MathJax and copied to your clipboard.

    Under linux, it is possible to use the GUI with gnome-screenshot (which comes with multiple monitor support). For other Wayland compositers, grim and slurp will be used for wlroots-based Wayland compositers and spectacle for KDE Plasma. Note that gnome-screenshot is not compatible with wlroots or Qt based compositers. Since gnome-screenshot will be preferred when available, you may have to set the environment variable SCREENSHOT_TOOL to grim or spectacle in these cases (other available values are gnome-screenshot and pil).

    !demo

    If the model is unsure about the what's in the image it might output a different prediction every time you click "Retry". With the temperature parameter you can control this behavior (low temperature will produce the same result).

3. You can use an API. This has additional dependencies. Install via pip install -U "pix2tex[api]" and run
    bash
    python -m pix2tex.api.run
    
    to start a Streamlit demo that connects to the API at port 8502. There is also a docker image  available for the API: https://hub.docker.com/r/lukasblecher/pix2tex 

    
    docker pull lukasblecher/pix2tex:api
    docker run --rm -p 8502:8502 lukasblecher/pix2tex:api
    
    To also run the streamlit demo run
    
    docker run --rm -it -p 8501:8501 --entrypoint python lukasblecher/pix2tex:api pix2tex/api/run.py
    
    and navigate to http://localhost:8501/

4. Use from within Python
    python
    from PIL import Image
    from pix2tex.cli import LatexOCR
    
    img = Image.open('path/to/image.png')
    model = LatexOCR()
    print(model(img))
    

The model works best with images of smaller resolution. That's why I added a preprocessing step where another neural network predicts the optimal resolution of the input image. This model will automatically resize the custom image to best resemble the training data and thus increase performance of images found in the wild. Still it's not perfect and might not be able to handle huge images optimally, so don't zoom in all the way before taking a picture. 

Always double check the result carefully. You can try to re

Top issues

Discussions

Latest release

0.0.31 0.0.31 Pre-release 2023-04-13
What's Changed

Big change (maybe breaking in your old environment) is moving away from PyQt5 to PyQt6. This will help with installation issues for M1 and M2 based systems.

 Update GUI to PyQt6 (Fix for M1 and M2 OS X) by @JoepdeJong in https://github.com/lukas-blecher/LaTeX-OCR/pull/236
 feat: add utf-8 support for reading README file by @llxlr in https://github.com/lukas-blecher/LaTeX-OCR/pull/201

 New Contributors
 @llxlr made their first contribution in https://github.com/lukas-blecher/LaTeX-OCR/pull/201
 @JoepdeJong made their first contribution in https://github.com/lukas-blec

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON