python请求文字识别ocr api
支持免费在线体验,API文档清晰,提供多重语言案例

以下是用python请求文字识别ocr api的代码:
python
# API文档网址:https://market.shiliuai.com/doc/advanced-general-ocr
# -*- coding: utf-8 -*-
import requests
import base64
import json
# 请求接口
URL = "https://ocr-api.shiliuai.com/api/general_ocr/v1"
# 图片转base64
def get_base64(file_path):
with open(file_path, 'rb') as f:
data = f.read()
b64 = base64.b64encode(data).decode('utf8')
return b64
def demo(appcode, file_path):
# 请求头
headers = {
'Authorization': 'APPCODE %s' % appcode,
'Content-Type': 'application/json'
}
# 请求体
b64 = get_base64(file_path)
data = {"image_base64": b64}
# 请求
response = requests.post(url=URL, headers=headers, json=data)
content = json.loads(response.content)
print(content)
if __name__ == "__main__":
appcode = "你的APPCODE"
file_path = "本地图片路径"
demo(appcode, file_path)
其中appcode可以到market.shiliuai.com申请得到。