百度图片文字识别文档:https://cloud.baidu.com/doc/OCR/s/Sk3h7xyad
1. 创建应用
登录百度智能云,在产品服务/文字识别-概览
下创建应用
2. 查看应用列表,找到自己的APPID/API Key/Secrest Key
3.示例代码(两种方式获取图片文字)
导入相应的包,定义常量
# encoding:utf-8
import requests
import base64
from aip import AipOcr
"""填入自己创建的应用信息"""
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
3.1 通用文字识别
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
image = get_file_content(r"[文件路径]")
""" 调用通用文字识别, 图片参数为本地图片 """
text = client.basicGeneral(image)
"""图片URl"""
#text = client.basicGeneralUrl(url)
print("|".join([word["words"] for word in text["words_result"]]))
3.2 网络图片文字识别
# POST请求获取 网络图片识别
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'.format(API_KEY, SECRET_KEY)
response = requests.get(host)
at = response.json()
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/webimage"
# 二进制方式打开图片文件
f = open(r'[图片路径]', 'rb')
img = base64.b64encode(f.read())
#如果要使用url识别,params中body中用url替代image即可。二选一
params = {
"image":img,
"detect_language": "true"
}
request_url = request_url + "?access_token=" + at['access_token']
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
info = response.json()
print("|".join([word["words"] for word in info["words_result"]]))
网友评论