使用KaTex格式来书写数学公式是个痛苦的事情,有时候看到某个数学公式想记录下来,为了省事不得不截图,但这很不专业,今天发现一个可以智能识别公式的程序,它使用机器学习模型来识别图像,并转化成KaTex代码,准确率很高。
LaTex-OCR仓库:https://github.com/lukas-blecher/LaTeX-OCR
有三种使用方式:
1、python代码
from PIL import Image
from pix2tex.cli import LatexOCR
'''
pip install pix2tex
pip install "pix2tex[gui]"
pip install -U typing_extensions
'''
img = Image.open('../imgs/360截图20231128085054222.jpg')
model = LatexOCR()
print(model(img))
# 打印信息
# \Gamma(z)=\int_{0}^{\infty}t^{z-1}e^{-t}d t\,.
测试图片为
识别的结果准确无误。
2、命令行
pix2tex
输入 h 查看帮助文档
Predict LaTeX code for image ("h" for help). h
pix2tex help:
Usage:
On Windows and macOS you can copy the image into memory and just press ENTER to get a prediction.
Alternatively you can paste the image file path here and submit.
You might get a different prediction every time you submit the same image. If the result you got was close you
can just predict the same image by pressing ENTER again. If that still does not work you can change the temperature
or you have to take another picture with another resolution (e.g. zoom out and take a screenshot with lower resolution).
Press "x" to close the program.
You can interrupt the model if it takes too long by pressing Ctrl+C.
Visualization:
You can either render the code into a png using XeLaTeX (see README) to get an image file back.
This is slow and requires a working installation of XeLaTeX. To activate type 'show' or set the flag --show
Alternatively you can render the expression in the browser using katex.org. Type 'katex' or set --katex
Settings:
to toggle one of these settings: 'show', 'katex', 'no_resize' just type it into the console
Change the temperature (default=0.333) type: "t=0.XX" to set a new temperature.
将粘贴板中的图片内容直接ctl+v
粘贴到命令行,按回车即可。输出如下
\Gamma(z)=\int_{0}^{\infty}t^{z-1}e^{-t}d t\,.
3、GUI模式
在命令行输入命令latexocr
,启动GUI交互界面,点击Snip
,然后截图公式即可。