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日

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?