Search code examples
pythongoogle-cloud-platformgoogle-cloud-storagegoogle-cloud-vision

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


I am trying to extract text data from images using Google Cloud Vision API. My Initial starting point was here After Enabling Vision API and Creating service account,generating json file, I created a script by referring this example.

Here's my code

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)

The only difference between the example shown in google page and my code is that the image uploaded in the example is on gcloud while mine happens to be on local storage.

Here's the complete stacktrace.

<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'

What is the root cause of this error? Please help!


Solution

  • Per the Google Cloud Vision docs, you want image.source.image_uri instead.