How do I call the Google Vision API with an image stored in Google Cloud Storage?
Asked Answered
S

2

5

I have a bunch of pictures on GCS and would like to figure out what they are?

Sward answered 5/2, 2016 at 1:8 Comment(0)
A
5

For GCS Integration - I would just modify the above body to point to the GCS location by replacing content attribute with gcs_image_uri

    batch_request = [{
    'image': {
        'source': {
            'gcs_image_uri': "gs://bucket_name/object_path"
        }
    },
    'features': [{
        'type': 'LANDMARK_DETECTION',
        'maxResults': max_results,
        }]
    }]
service = get_vision_service()
request = service.images().annotate(body={
    'requests': batch_request,
    })
response = request.execute()
Apostrophe answered 5/2, 2016 at 17:23 Comment(0)
S
2

The Vision API can be accessed via a REST API call. You pass in a JSON request with either image embedded or a link to the image in GCS. You can then pass in the features you want to run on the image. This is passed in as a JSON request and the response object contains the annotations. Here is a snippet of Python code calling the Vision API.

DISCOVERY_URL='https://{api}.googleapis.com/$discovery/rest?version={apiVersion}'

credentials = GoogleCredentials.get_application_default()
service = discovery.build('vision', 'v1', credentials=credentials,
                          discoveryServiceUrl=DISCOVERY_URL)

with open(photo_file, 'rb') as image:
  image_content = base64.b64encode(image.read())    
  service_request = service.images().annotate(
    body={
      'requests': [{
        'image': {
          'content': image_content
         },
        'features': [{
          'type': 'LABEL_DETECTION',  # Feature to detect
          'maxResults': 1,
         }]
       }]
    })
  response = service_request.execute()
  label = response['responses'][0]['labelAnnotations'][0]['description']

For additional information, you might wish to look at the Label Detection Tutorial

Sward answered 5/2, 2016 at 1:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.