python基于百度API的ORC文字识别

python基于百度API的ORC文字识别

python基于百度API的ORC文字识别

# encoding:utf-8

import requests
import base64

# 文字识别模块    高精度:https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic 普通:general_basic
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
# 二进制方式打开图片
f = open('C:\\Users\\bill_love_sea\\Desktop\\python\\20200617140056.jpg', 'rb')
img = base64.b64encode(f.read())
# 获取API相关的参数:access_token,headers,data 等
params = {
    
    "image": img}
access_token = '24.253c31adc2d26d728940cf62c65013c0.2592000.1597372330.282335-21365498'
request_url = request_url + '?access_token=' + access_token
headers = {
    
    'content_type': 'application/x-www-form-urlencoded'}
# 以POST方式进行请求
response = requests.post(request_url, data=params, headers=headers)
# 输出响应结果
if response:
    a = dict(response.json())
    for words in a['words_result']:
        print(words['words'])

调取API的基本过程百度云的相关文档都有,可以自行参考,其中‘access_token’参数的获取如下:

# encoding:utf-8
import requests
# client_id 为官网获取的AK, client_secret 为官网获取的SK
AK = 'ysLHhFMBBGOq6wTZeoS6W6aX'
SK = '9wioD22eKhFoyfv8rcdO01lzBunUq4S6'
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+AK+'&client_secret='+SK+''
response = requests.get(host)
if response:
    print(response.json()['access_token'])

猜你喜欢

转载自blog.csdn.net/bill_love_c/article/details/107462036