一.前言
这个项目是我在求职时遇到的一个实操题,使用百度API的OCR识别的发票识别接口进行识别,仅供学习使用。
二.获取access_token
大家可以用这个链接访问接口,可以申请免费次数,具体如何申请官方网站会有指导,不在赘述。
申请成功如下图。
可以看到我们有API Key 和 Secret Key,用这两个参数去获取AccessToken,这个AccessToken可以保持30天。
下面代码获取AccessToken:
client_id 就是API Key,client_secret就是Secret Key
import requests
import json
def main():
url = "https://aip.baidubce.com/oauth/2.0/token?client_id=*********&client_secret=*********&grant_type=client_credentials"
payload = json.dumps("")
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
if __name__ == '__main__':
main()
接下来会返回json数据,我们只要里面的access_token值就行。
三.用access_token调用发票识别接口
直接上代码
pdf文件和image文件都可以传入:
def main():