SDVAERVA 2021-10-08 18:48 采纳率: 80%
浏览 62
已结题

百度云智能文字识别报错

刚用的时候能正常运行,过了几天后同一张图片运行报错,

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()
  • 写回答

1条回答 默认 最新

  • ak01_10 2021-10-08 20:50
    关注

    如果你可以识别其他图片,偏偏识别不了这一个图片,那应该是识别超时,或者识别失败。
    百度的识别也没有那么强大

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月21日
  • 已采纳回答 10月13日
  • 创建了问题 10月8日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改