簡體   English   中英

KeyError: 'textAnnotations' while doing OCR using Google Cloud Vision API

[英]KeyError: 'textAnnotations' while doing OCR using Google Cloud Vision API

正在關注 OCR 教程。 我所做的一切都與教程中提到的相同,但我在最后一部分生成請求時收到此錯誤KeyError: 'textAnnotations' 我能知道我該如何解決這個問題嗎? 我的輸入圖像只是一個字符a

這是我創建請求的方式:

from googleapiclient.discovery import build
import base64
IMAGE="gs://<project-name>/<folder>/<file-name>.jpg"
vservice = build('vision', 'v1', developerKey=APIKEY)
request = vservice.images().annotate(body={
    'requests': [{
       'image': {
          'source': {
             'gcs_image_uri': IMAGE
          }
      },
      'features': [{
          'type': 'TEXT_DETECTION',
          'maxResults': 3,
        }]
     }],
  })
responses = request.execute(num_retries=3)
print(responses['responses'][0]['textAnnotations'][0]['description'])

出現錯誤時由 API 返回的響應結構與responses['responses'][0]['textAnnotations'][0]['description']不同。 我的觀點是存在錯誤,因此您的響應沒有您認為的形狀。 按照 Caleb 的建議,我會打印出完整的回復,然后先看看里面有什么

當您的圖像不包含文本時會發生此錯誤。 因此,在這種情況下,將不會有任何 textAnnotations 數組。 響應將類似於 Array ( [responses] => Array ([0] => Array ( ))) 您可以簡單地添加一個檢查,如果 textAnnotations 存在於響應數組中,然后發送其他您想要的任何其他響應的描述。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM