From 8998b84eac0b5e7ba151c235af79c8b32b83833e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E5=A4=A9=E7=9D=BF?= Date: Wed, 15 Jul 2020 21:10:34 +0800 Subject: [PATCH] Support: Mathpix OCR, close #8 --- README.md | 18 ++++++++++++++++ src_alfred/config.py | 8 +++++-- src_alfred/ocr.py | 51 ++++++++++++++++++++++++++++++++++++++------ 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1442888..80e3f41 100755 --- a/README.md +++ b/README.md @@ -18,6 +18,11 @@ ## 版本 +### 4.8 + +- 支持 Mathpix 公式识别(因学业原因,仅支持识别后输出 Latex 格式文本,更多支持选项待后期开发); +- 由于可选识别方式过多,CNOCR 的触发方式修改为唯一触发词 ooc (CNOCR)。 + ### 近期更新 - 紧急修复批量文件识别报错的问题; @@ -26,10 +31,13 @@ ## 能力 +### CNOCR + - 离线 OCR (CNOCR) - 通用 OCR (百度 | 腾讯优图 | Google) - 二维码识别 (百度 | ZXing) - 表格文字识别 (百度) +- 数学公式识别 (Mathpix) - 多文件识别 (百度) - 文本翻译 (彩云小译) @@ -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] #### 触发 diff --git a/src_alfred/config.py b/src_alfred/config.py index 09b94eb..452abfd 100755 --- a/src_alfred/config.py +++ b/src_alfred/config.py @@ -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 @@ -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 @@ -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=' + \ @@ -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' diff --git a/src_alfred/ocr.py b/src_alfred/ocr.py index 55c1b2e..3a04a76 100755 --- a/src_alfred/ocr.py +++ b/src_alfred/ocr.py @@ -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 @@ -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] @@ -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: @@ -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) @@ -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) '''