本文是文本OCR的后续,因为用到了公式识别,所以阅读了 Mathpix API 文档,编写了一份比较适合自己使用的公式识别的Python程序,具体代码如下,注意使用之前应当去 Mathpix 官网 申请开发者ID
和 KEY
其对应的是代码中的APP_ID
和APP_KEY
后的XXX
,值得高兴的是每个月的有1000次免费的使用权限(虽然超出了也不贵,但是如果怕超,可以在填写信用卡的安全码时写一个错误安全码)。相较软件上的50次,我想已经很好了。下面是代码实现,可以直接拷贝使用:
import os
import sys
import json
import time
import base64
import signal
import win32con
import winsound
import requests
from PIL import ImageGrab
import win32clipboard as wc
def set_clipboard(txt):
wc.OpenClipboard()
wc.EmptyClipboard()
wc.SetClipboardData(win32con.CF_UNICODETEXT, txt)
wc.CloseClipboard()
env = os.environ
default_headers = {
'app_id': env.get('APP_ID', 'XXX'),
'app_key': env.get('APP_KEY', 'XXX'),
'Content-type': 'application/json'
}
service = 'https://api.mathpix.com/v3/latex'
format_set = ["text",
"latex_simplified",
"latex_styled",
"mathml",
"asciimath",
"latex_list"]
format_set_output = ["text",
"latex_simplified",
"latex_styled"]
def image_uri(filename):
image_data = open(filename, "rb").read()
return "data:image/jpg;base64," + base64.b64encode(image_data).decode()
def latex(args, headers=default_headers, timeout=30):
r = requests.post(service,
data=json.dumps(args), headers=headers, timeout=timeout)
return json.loads(r.text)
def sig_handler(signum, frame):
sys.exit(0)
""" 截图后,调用Mathpix 公式识别"""
def LatexOcrScreenshots(path="./",ifauto=False):
if not os.path.exists(path):
os.makedirs(path)
image = ImageGrab.grabclipboard()
if image != None:
print("\rThe image has been obtained. Please wait a moment!",end=" ")
filename = str(time.time_ns())
image.save(path+filename+".png")
txt = latex({
'src': image_uri(path+filename+".png"),
"ocr": ["math", "text"],
"skip_recrop": True,
"formats":format_set
})
os.remove(path+filename+".png")
# for format_text in txt:
# print(format_text,":\n",txt[format_text])
for format_text in format_set_output:
if format_text in txt:
set_clipboard(txt[format_text])
break;
winsound.PlaySound('SystemAsterisk',winsound.SND_ASYNC)
return txt
else :
if not ifauto:
print("Please get the screenshots by Shift+Win+S! ",end="")
return ""
else:
print("\rPlease get the screenshots by Shift+Win+S ! ",end="")
def AutoOcrScreenshotsLatex():
signal.signal(signal.SIGINT, sig_handler)
signal.signal(signal.SIGTERM, sig_handler)
print("Waiting For Ctrl+C to exit ater removing all picture files and txt files!")
print("Please get the screenshots by Shift+Win+S !",end="")
while(1):
try:
LatexOcrScreenshots(ifauto=True)
time.sleep(0.1)
except SystemExit:
break
else :
pass
finally:
pass
可以看出其与百度API不同的地方是,直接使用网站POST便可以实现OCR内容的获取,具体获取的内容是由format_set
决定的,而输出的内容的优先级是由format_set_output
决定的。