This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from 838239178/develop
(feat): 新增 azure ocr
- Loading branch information
Showing
5 changed files
with
60 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# 微软Azure计算机视觉OCR | ||
|
||
## 依赖需求 | ||
|
||
由于Azure支持的图片大小不在范围内,因此需要Pillow缩放图片大小,需要在你的`requirements.txt`中加入`Pillow` | ||
|
||
## 步骤 | ||
|
||
1. 需要注册微软账号 | ||
2. 登陆 https://azure.microsoft.com/ | ||
3. 在免费服务中开通**计算机视觉**服务 | ||
4. 填写表单,注意选择的区域,建议选择EastAsia | ||
5. 创建完成后进入该服务>资源管理>密钥和终结点,复制其中一个密钥和终结点,终结点去掉`https://`前缀 | ||
6. 填写config.json, ocr.ak 为终结点, ocr.sk 为其中一个密钥, 例: | ||
```json | ||
"ocr": { | ||
"sk": "hksight.cognitiveservices.azure.com", | ||
"ak": "182391820319283019", | ||
"type": "azure" | ||
}, | ||
``` |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import base64 | ||
from io import BytesIO | ||
import requests | ||
from PIL import Image | ||
|
||
_ENDPOINT = '' | ||
_SECRET_KEY = '' | ||
|
||
|
||
def set_keys(api_key, secret_key): | ||
global _ENDPOINT, _SECRET_KEY | ||
_ENDPOINT = api_key | ||
_SECRET_KEY = secret_key | ||
|
||
|
||
def is_need_keys() -> bool: | ||
return True | ||
|
||
|
||
def get_result(img: bytes) -> str: | ||
img = base64.standard_b64decode(img) | ||
buf = BytesIO() | ||
Image.open(BytesIO(img)) \ | ||
.resize((157,52)) \ | ||
.save(buf, format="JPEG") | ||
url = f"https://{_ENDPOINT}/vision/v3.2/ocr?language=en&detectOrientation=false" | ||
resp = requests.post(url, headers={ | ||
"Ocp-Apim-Subscription-Key": _SECRET_KEY, | ||
"Content-type": "application/octet-stream" | ||
}, data=buf.getvalue(), timeout=10) | ||
body = resp.json() | ||
if resp.status_code != 200: | ||
raise Exception(f"识别失败: {body['error']['message']}") | ||
return body["regions"][0]["lines"][0]["words"][0]["text"] |