LaTeX-OCR

授权协议 MIT License
开发语言
所属分类 企业应用、 LaTeX排版系统
软件类型 开源软件
地区 不详
投 递 者 戈博易
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

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

Requirements

Model

  • PyTorch (tested on v1.7.1)
  • Python 3.7+ & dependencies (requirements.txt)
    pip install -r requirements.txt
    

Dataset

In order to render the math in many different fonts we use XeLaTeX, generate a PDF and finally convert it to a PNG. For the last step we need to use some third party tools:

Using the model

  1. Download/Clone this repository
  2. For now you need to install the Python dependencies specified in requirements.txt (look above)
  3. Download the weights.pth (and optionally image_resizer.pth) file from my Google Drive and place it in the checkpoints directory

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 python gui.py. From here you can take a screenshot and the predicted latex code is rendered using MathJax and copied to your clipboard.

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).

Alternatively you can use pix2tex.py with similar functionality as gui.py, only as command line tool. In this case you don't need to install PyQt5. Using this script you can also parse already existing images from the disk.

Note: As of right now it works best with images of smaller resolution. Don't zoom in all the way before taking a picture. Double check the result carefully. You can try to redo the prediction with an other resolution if the answer was wrong.

Update: I have trained an image classifier on randomly scaled images of the training data to predict the original size.This model will automatically resize the custom image to best resemble the training data and thus increase performance of images found in the wild. To use this preprocessing step, all you have to do is download the second weights file mentioned above. You should be able to take bigger (or smaller) images of the formula and still get a satisfying result

Training the model

  1. First we need to combine the images with their ground truth labels. I wrote a dataset class (which needs further improving) that saves the relative paths to the images with the LaTeX code they were rendered with. To generate the dataset pickle file run
python dataset/dataset.py --equations path_to_textfile --images path_to_images --tokenizer path_to_tokenizer --out dataset.pkl

You can find my generated training data on the Google Drive as well (formulae.zip - images, math.txt - labels). Repeat the step for the validation and test data. All use the same label text file.

  1. Edit the data entry in the config file to the newly generated .pkl file. Change other hyperparameters if you want to. See settings/default.yaml for a template.
  2. Now for the actual training run
python train.py --config path_to_config_file

Model

The model consist of a ViT [1] encoder with a ResNet backbone and a Transformer [2] decoder.

Performance

BLEU score normed edit distance
0.88 0.10

Data

We need paired data for the network to learn. Luckily there is a lot of LaTeX code on the internet, e.g. wikipedia, arXiv. We also use the formulae from the im2latex-100k dataset.All of it can be found here

Fonts

Latin Modern Math, GFSNeohellenicMath.otf, Asana Math, XITS Math, Cambria Math

TODO

  • add more evaluation metrics
  • create a GUI
  • add beam search
  • support handwritten formulae
  • reduce model size (distillation)
  • find optimal hyperparameters
  • tweak model structure
  • fix data scraping and scrape more data
  • trace the model

Contribution

Contributions of any kind are welcome.

Acknowledgment

Code taken and modified from lucidrains, rwightman, im2markup, arxiv_leaks, pkra: Mathjax, harupy: snipping tool

References

[1] An Image is Worth 16x16 Words

[2] Attention Is All You Need

  • 作者在写文章时,大量的公式输入比较频繁,因此需要一款公式自动识别根据 市面上使用最多的是mathpix,但是收费昂贵,对于普通用户一个月仅有100次机会,实在捉襟见肘。有网友提供一种基于注册的无限使用方法,参考数学公式截图识别神器Mathpix无限使用教程。 1.simpletex 网页版使用体验还可以 2.LaTeX-OCR github开源的一款识别工具,使用过程如下(其实按照作者的步骤一步一

  • Mac安装Tesseract遇见的问题 环境:Macos Big Sur 1.打开terminal终端工具 /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)" 2.brew安装成功后, 注意brew安装过程中时间较长,请耐心等待 brew install tesseract

 相关资料
  • 部署 LaTeX 安装 texlive-core 新建 a.tex 文件,内容如下: \documentclass[11pt,a4paper]{article} %加入了一些针对XeTeX的改进并且加入了 \XeTeX 命令来输入漂亮的XeTeX logo \usepackage{xltxtra} %启用一些LaTeX中的功能 \usepackage{xunicode} %%%% fontspe

  • LaTeX(LATEX,音译“拉泰赫”)是一种基于TeX的排版系统。 LaTeX通过CTAN服务器发布,或作为TeX用户组(TUG)或第三方提供的许多易于安装和可用的TeX发布版本的一部分。如果您遇到问题,请访问帮助部分。 LaTeX本身并不是一个独立的排版程序,而是运行在Donald E. Knuth的TeX排版系统之上的文档准备软件。TeX发行版通常会将工作中的TeX系统所需的所有部件捆绑在一

  • Vim + LaTeX snippets setup How I'm able to take notes in mathematics lectures using LaTeX and Vim Vim configuration Copy tex.snippets to ~/.vim/UltiSnips/ and assuming you're using Vim Plug, add the f

  • latex-action GitHub Action to compile LaTeX documents. This actions runs on docker using a maximal TeXLive environment installed. Inputs root_file The root LaTeX file to be compiled. This input is req

  • PDF 下载: 放映版 阅读版 Copyright (C) 2018–2021 by Xiangdong Zeng.

  • Warning — looking for a maintainer This extension is no longer maintained. I'm sorry, it's not you, it's me. I still love this functionality, I've just moved to a different editor to get it.I don't wa