| 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_CN') | |
| streamlit.title('LaTeX OCR_CN') | |
| streamlit.markdown( | |
| '将方程式或者数学公式的图像转换为相应的 LaTeX 代码.\n\n这是基于pix2tex项目.查看[](https://github.com/lukas-blecher/LaTeX-OCR)') | |
| uploaded_file = streamlit.file_uploader( | |
| '上传公式图片,点击BrowserFiles或者拖拽到此上传,支持200MB以内的JPG或者PNG图片', | |
| type=['png', 'jpg'], | |
| ) | |
| if uploaded_file is not None: | |
| image = Image.open(uploaded_file) | |
| streamlit.image(image) | |
| else: | |
| streamlit.text('\n') | |
| if streamlit.button('转换'): | |
| if uploaded_file is not None and image is not None: | |
| with streamlit.spinner('计算中'): | |
| 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('请上传图片.') | |