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())
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章