KeyError:'image_path' while executing Google Vision API using python

KeyError:'image_path' while executing Google Vision API using python

我正在尝试使用 Google Cloud Vision API 从图像中提取文本数据。我的初始起点是 here After Enabling Vision API and Creating service account,generating json file, I created a script by referring this example.

这是我的代码

from google.cloud import vision
from google.cloud.vision_v1 import types
import os

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'generated_after_creating_sec_key.json'
image_path = 'images\image_1.png'
vision_client = vision.ImageAnnotatorClient()
print(vision_client)
image = types.Image()
image.source.image_path = image_path

response = vision_client.text_detection(image=image)
for text in response.text_annotations:
    print(text.description)

google页面中显示的示例与我的代码之间的唯一区别是示例中上传的图像是在 gcloud 上,而我的恰好在本地存储上。

这是完整的堆栈跟踪。

<google.cloud.vision_v1.ImageAnnotatorClient object at 0x000001DF861D7970>
Traceback (most recent call last):
  File "text_detection.py", line 10, in <module>
    image.source.image_path = image_path
  File "C:\Users\user\ProjectFolder\ProjName\venv\lib\site-packages\proto\message.py", line 677, in __setattr__
    pb_type = self._meta.fields[key].pb_type
KeyError: 'image_path'

此错误的根本原因是什么?请帮忙!

根据 Google Cloud Vision docs,您需要 image.source.image_uri

根据 Google Cloud Vision 文档,您应该使用 image.source.image_uri 而不是。