Search code examples
pythongoogle-cloud-platformartificial-intelligence

Google AI Vision / Image labeling?


i try to label images with the Google Vision API - and this code works fine generally:

from google.cloud import vision
import os
import sys

if __name__ == '__main__':
  path = os.path.abspath(os.path.dirname(sys.argv[0])) 
  credsFN = os.path.join(path, "creds.json")
  os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credsFN

  imageURL = "https://www.24mantra.com/wp-content/uploads/2019/10/banner-3.jpg"

  client = vision.ImageAnnotatorClient()
  image = vision.Image()
  image.source.image_uri = imageURL
  features = [vision.Feature.Type.LABEL_DETECTION]
  features = [vision.Feature(type_=feature_type) for feature_type in features]
  requ = vision.AnnotateImageRequest(image=image, features=features)
  resp = client.annotate_image(request=requ)
  for label in resp.label_annotations:
    print(f"{label.score:4.0%}: {label.description}")

Now i tried it with some other url like:

https://www.juicer.io/api/posts/459631754/images.jpg?external_id=CiE264cqZcw&s=199f57b090efba5ef332c3ff09d7b14554f84bfc

(when you click the link you will see that the image opens without problems in the browser)

But when i run the above program with this link it is not working anymore and i get an empty response? How can i get also results from Google Vison API with this links?


Solution

  • The link with an error is having an error code: 7 with an error message: "We're not allowed to access the URL on your behalf. Please download the content and pass it in."

    The possible workaround for this is to store your image in Google Cloud Storage(GCS) and use the gs://uri instead. See here for your reference.