Skip to content

Commit

Permalink
Support: Mathpix OCR, close #8
Browse files Browse the repository at this point in the history
  • Loading branch information
陆天睿 committed Jul 15, 2020
1 parent 26818e0 commit 8998b84
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

## 版本

### 4.8

- 支持 Mathpix 公式识别(因学业原因,仅支持识别后输出 Latex 格式文本,更多支持选项待后期开发);
- 由于可选识别方式过多,CNOCR 的触发方式修改为唯一触发词 ooc (CNOCR)。

### 近期更新

- 紧急修复批量文件识别报错的问题;
Expand All @@ -26,10 +31,13 @@

## 能力

### CNOCR

- 离线 OCR (CNOCR)
- 通用 OCR (百度 | 腾讯优图 | Google)
- 二维码识别 (百度 | ZXing)
- 表格文字识别 (百度)
- 数学公式识别 (Mathpix)
- 多文件识别 (百度)
- 文本翻译 (彩云小译)

Expand Down Expand Up @@ -159,6 +167,16 @@ pip install zxing

- Google OCR 为收费业务,需绑定信用卡,故本项目不带测试 Token,需要自行申请。

### [Mathpix](https://mathpix.com)

#### 触发

- 公式识别:截图至剪贴板后使用关键词 `oom (mathpix)` 触发。

#### 说明

- Mathpix 为收费业务,需绑定信用卡,故本项目不带测试 Token,需要自行[申请](https://accounts.mathpix.com/ocr-api)

### [ZXing][10]

#### 触发
Expand Down
8 changes: 6 additions & 2 deletions src_alfred/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
@Description: Capture than OCR - Variable
@Author: Chandler Lu
@Date: 2020-03-09 20:32:15
@LastEditTime: 2020-04-21 19:42:11
@LastEditTime: 2020-07-15 20:51:39
'''
# -*- coding: UTF-8 -*-

import os

# Control
Expand All @@ -26,6 +25,8 @@
GOOGLE_POST_REFERER = os.environ['google_post_referer']
GOOGLE_HTTP_PROXY = os.environ['google_http_proxy']
CAIYUN_TRANSLATE_TOKEN = os.environ['caiyun_token']
MATHPIX_APP_ID = os.environ['mathpix_app_id']
MATHPIX_APP_KEY = os.environ['mathpix_app_key']

# Key - Quicker

Expand All @@ -37,6 +38,8 @@
# GOOGLE_POST_REFERER = ''
# GOOGLE_HTTP_PROXY = ''
# CAIYUN_TRANSLATE_TOKEN = '3975l6lr5pcbvidl6jl2'
# MATHPIX_APP_ID = ''
# MATHPIX_APP_KEY = ''

# API
BAIDU_GET_TOKEN_URL = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + \
Expand All @@ -47,3 +50,4 @@
TENCENT_YOUTU_OCR_API = 'https://api.ai.qq.com/fcgi-bin/ocr/ocr_generalocr'
GOOGLE_OCR_API = 'https://vision.googleapis.com/v1/images:annotate'
CAIYUN_TRANSLATE_API = 'http://api.interpreter.caiyunai.com/v1/translator'
MATHPIX_API = 'https://api.mathpix.com/v3/text'
51 changes: 45 additions & 6 deletions src_alfred/ocr.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'''
@Description: Capture than OCR - macOS - Online OCR
@version: 4.7
@Description: Capture than OCR - Online OCR
@version: 4.8
@Author: Chandler Lu
@Date: 2019-11-26 23:52:36
@LastEditTime: 2020-04-24 17:25:21
@LastEditTime: 2020-07-15 20:52:29
'''
# -*- coding: UTF-8 -*-
import sys
Expand Down Expand Up @@ -282,6 +282,42 @@ def google_ocr(pic_path):
declare_network_error()


'''
Mathpix OCR
'''


def mathpix_ocr(pic_path):
''' https://docs.mathpix.com/?python#request-parameters
file_path = 'limit.jpg'
image_uri = "data:image/jpg;base64," + b64encode(open(pic_path, "rb").read()).decode()
r = requests.post("https://api.mathpix.com/v3/text",
data=json.dumps({'src': image_uri}),
headers={"app_id": "YOUR_APP_ID", "app_key": "YOUR_APP_KEY",
"Content-type": "application/json"})
print(json.dumps(json.loads(r.text), indent=4, sort_keys=True))
'''
try:
response = requests.post(
url=c.MATHPIX_API,
headers={
'app_id': c.MATHPIX_APP_ID,
'app_key': c.MATHPIX_APP_KEY,
'Content-type': 'application/json',
},
data=json.dumps({
'src': 'data:image/jpg;base64,' + convert_image_base64(pic_path)
})
)
if (response.status_code == 200):
response_json = response.json()['latex_styled']
print(response_json, end='')
else:
print('Request failed!', end='')
except requests.exceptions.ConnectionError:
declare_network_error()


def multi_file_ocr():
names = [name for name in os.listdir(temp_path)
if re.search(r'.png|.jpg|.jpeg|.bmp', name, re.IGNORECASE) and os.path.getsize(temp_path + '/' + name) <= 4194304]
Expand Down Expand Up @@ -459,7 +495,7 @@ def remove_pic(pic_path):


if __name__ == "__main__":
if (0 <= ocr_select <= 6):
if (ocr_select != 99):
try:
os.path.getsize(pic_path)
except FileNotFoundError:
Expand All @@ -472,7 +508,8 @@ def remove_pic(pic_path):
4: tencent
5: google
6: zxing
7: file
7: mathpix
99: file
'''
if (ocr_select == 0):
cnocr_ocr(pic_path)
Expand All @@ -489,8 +526,10 @@ def remove_pic(pic_path):
elif (ocr_select == 6):
barcode_decode(pic_path)
elif (ocr_select == 7):
mathpix_ocr(pic_path)
elif (ocr_select == 99):
multi_file_ocr()
if (ocr_select != 7):
if (ocr_select != 99):
remove_pic(pic_path)

'''
Expand Down

0 comments on commit 8998b84

Please sign in to comment.