利用Mathpic OCR和Quicker实现数学公式的快速识别

利用Mathpic OCR和Quicker实现数学公式的快速识别

写在前面

相信很多小伙伴已经习惯了使用Mathpic来识别手写的或者论文中的数学公式,但是现在Mathpic每个月只能免费查询50次,如果想继续查询需要5美刀包月,这价格优点扛不住啊!不过, Mathpix官网还算是优点良心,给开发者留了条后门,使用API查询每月可免费查询1,000条,相信对于一般的小伙伴来说已经是绰绰有余了~
具体收费标准如下:

  • First 1000:requests free
  • 1-100K requests:$0.004/request
  • 100-300K requests:$0.002/request
  • 300K+ requests:$0.001/request

账号的申请过程,如下面网址所示

Mathpix收费了?快使用API吧,一个月免费识别1000次!

不过申请过程中,需要???绑定信用卡???,有忌讳的小伙伴就只能另外想别的方法了~

关于信用卡,这里讲一下,我用的不是visa信用卡,额度也是非常低,不过也还是能通过验证

当然,配合上Quicker软件可以大大提高效率。就像下面这样
????????????????
01

准备过程

所需环境

Python3.X 需要安装第三方requests模块以及pillow模块
Win10 Win7应该也是可以滴
Quicker 有免费版,当然也有收费版,一个月也就一瓶饮料钱

配置过程

下面是我自己的配置过程,大家也可以使用自己所需的方式进行配置

PC端
  • 将如下的simple.py置于C:\CustomStartUp\formrec文件夹内(或其他的文件夹下,路径中不要带空格及中文!!!),py文件中在APP_IDAPP_KEY中填写自己申请账号过程中官网给出使用API的app id和app key
  • 创建一个latexform_start.txt文件,置于C:\CustomStartUp文件夹下,内部填写simple.py文件的绝对路径地址(包含simple.py),比如我的地址是C:\CustomStartUp\formrec\simple.py
# simple.py
import os
import base64
import requests
import json
from PIL import ImageGrab

# APP_ID=my-id APP_KEY=my-key python3 simple.py 
# Fill in your app id.
APP_ID   = 'your APP_ID'
# Fill in your app key.
APP_KEY  = 'your APP_KEY'

#
# Common module for calling Mathpix OCR service from Python.
#
# N.B.: Set your credentials in environment variables APP_ID and APP_KEY,
# either once via setenv or on the command line as in
#

env = os.environ

default_headers = {
    'app_id': env.get('APP_ID', APP_ID),
    'app_key': env.get('APP_KEY', APP_KEY),
    'Content-type': 'application/json'
}

service = 'https://api.mathpix.com/v3/latex'

#
# Return the base64 encoding of an image with the given filename.
#
def image_uri(filename):
    image_data = open(filename, "rb").read()
    return "data:image/jpg;base64," + base64.b64encode(image_data).decode()

#
# Call the Mathpix service with the given arguments, headers, and timeout.
#
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 mathpix_clipboard(): # Identify the clipboard formula
    im = ImageGrab.grabclipboard()
    fp = os.path.abspath(__file__)
    fp = os.path.dirname(fp) # Get the file dir. 
    fn = os.path.join(fp, 'equa.png') # Get the filename where the picture is stored.
    im.save(fn,'PNG')
    r = latex({
        'src': image_uri(fn),
        'formats': ['latex_simplified']
    })
    print(r['latex_simplified'])

if __name__ == '__main__':
    mathpix_clipboard()
Quicker配置
  • 在需要运行的软件上添加组合动作(我平常用Typora和为知云上做笔记,所以在这两个上面添加了)
    02
  • 1、读取文件

03

  • 2、运行脚本

04

  • 3、组合成文本

05

  • 4、发送文本到窗口

06

编辑完成以后如下
在这里插入图片描述

使用过程
  1. 使用Snipaste或者其他截图软件将需要转换的图片截取到剪切板中
  2. 打开相应的Markdown或者Latex软件(已经在Quicker上设置过的软件)
  3. 点击公式识别按钮,即可在相应软件上生成对应的Latex代码

Now, everything is ready, have fun~

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值