当前位置 博文首页 > LY的博客:python 百度api OCR识别 表格
自己参照示例写的,方便大家,顺便说一下,识别效果并不理想
# encoding:utf-8
import requests
import base64
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=*******&client_secret=******'
response = requests.get(host)
if response:
print(response.json())
a=response.json()
access_token=a['access_token']
#表格文字识别(异步接口)
request_url = "http://style.iis7.com/uploads/2021/08/12234110500', 'rb')
img = base64.b64encode(f.read())
#请求token
params = {"image":img}
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
print (response.json())
#发起
b=response.json()
request_id=b ['result'][0]['request_id']
#request_id='19441901_1671007'
request_url = "https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/get_request_result"
#接收图片
#
params = {
"request_id":request_id}
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
print (response.json())
cs