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 = "https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/request"
# 二进制方式打开图片文件
f = open('捕获.PNG', '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())
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章