刚用的时候能正常运行,过了几天后同一张图片运行报错,
res = response.json()["words_result"]
KeyError: 'words_result'
这玩意是要付费还是怎样?
import requests
import base64
def get_access_token():
url = 'https://aip.baidubce.com/oauth/2.0/token'
data = {
'grant_type': 'client_credentials', # 固定值
'client_id': 'noW7ZS8cT1GtPeQ3HzuwhxT1', # 在开放平台注册后所建应用的API Key
'client_secret': 'lvzFIxyKApelAYa1xpap7OQkdRwE7A11' # 所建应用的Secret Key
}
res = requests.post(url, data=data)
res = res.json()
access_token = res['access_token']
return access_token
#通用文字识别
def general_word():
#通用文字识别接口url
general_word_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
#获取执行路径
# path = os.getcwd()
# 二进制方式打开图片文件
f = open('mcad.png', 'rb')
img = base64.b64encode(f.read())
print(img)
params = {"image":img,
"language_type":"CHN_ENG"}
access_token = get_access_token()
request_url = general_word_url + "?access_token=" + access_token
print(request_url)
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
# print(response)
# res = response.json()
if response:
res = response.json()["words_result"]
print(res)
file_name = "菜鸟小白.txt"
with open(file_name, 'w', encoding='utf-8') as f:
for j in res:
print(j["words"])
f.write(j["words"]+"\n")
general_word()