百度 图片文字识别 Python版本

百度图片文字识别文档:https://cloud.baidu.com/doc/OCR/s/Sk3h7xyad

1. 创建应用

登录百度智能云,在产品服务/文字识别-概览下创建应用
image.png

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"]]))
eg:image.jpg
标题result:

image.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值