File size: 1,517 Bytes
44b8827
 
 
6c5e7b6
44b8827
c89b433
44b8827
c89b433
 
 
44b8827
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c89b433
 
 
 
 
 
44b8827
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
from PIL import Image
import streamlit
os.system('pip install pix2tex')
from pix2tex.cli import LatexOCR
from munch import Munch

args = Munch({'config': 'settings/config.yaml',
              'checkpoint': os.path.realpath(os.path.join(os.path.dirname(__file__), 'checkpoints/weights.pth')),
              'no_resize': False})
model = LatexOCR(args)

if __name__ == '__main__':
    streamlit.set_page_config(page_title='LaTeX-OCR')
    streamlit.title('LaTeX OCR')
    streamlit.markdown(
        'Convert images of equations to corresponding LaTeX code.\n\nThis is based on the `pix2tex` module. Check it out [![github](https://img.shields.io/badge/LaTeX--OCR-visit-a?style=social&logo=github)](https://github.com/lukas-blecher/LaTeX-OCR)')

    uploaded_file = streamlit.file_uploader(
        'Upload an image an equation',
        type=['png', 'jpg'],
    )

    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        streamlit.image(image)
    else:
        streamlit.text('\n')

    if streamlit.button('Convert'):
        if uploaded_file is not None and image is not None:
            with streamlit.spinner('Computing'):
                try:
                    latex_code = model(image)
                    streamlit.code(latex_code, language='latex')
                    streamlit.markdown(f'$\\displaystyle {latex_code}$')
                except Exception as e:
                    streamlit.error(e)
        else:
            streamlit.error('Please upload an image.')