notebooks/LaTeX_OCR_test.ipynb
In this colab you can convert an image of an equation into LaTeX code.
Execute the cell titled "Setup". The first time an error will show up. Simply execute the cell again. Everything should be fine now.
Next, execute the cell below and upload the image(s).
Note: You can probably also run this project locally and with a GUI. Follow the steps on GitHub
#@title Setup
%reload_ext autoreload
%autoreload
import PIL
!pip install Pillow -U -qq
if int(PIL.__version__[0]) < 9:
print('Mandatory restart: Execute this cell again!')
import os
os.kill(os.getpid(), 9)
!pip install pix2tex -qq
!pip install opencv-python-headless==4.1.2.30 -U -qq
def upload_files():
from google.colab import files
from io import BytesIO
uploaded = files.upload()
return [(name, BytesIO(b)) for name, b in uploaded.items()]
from pix2tex import cli as pix2tex
from PIL import Image
model = pix2tex.LatexOCR()
from IPython.display import HTML, Math
display(HTML("<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/"
"latest.js?config=default'></script>"))
table = r'\begin{array} {l|l} %s \end{array}'
imgs = upload_files()
predictions = []
for name, f in imgs:
img = Image.open(f)
math = model(img)
print(math)
predictions.append('\\mathrm{%s} & \\displaystyle{%s}'%(name, math))
Math(table%'\\\\'.join(predictions))