Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 510
Les Vogel - Google DevRel Avatar asked Feb 05 '16 01:02

Les Vogel - Google DevRel


2 Answers

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()
like image 182
Ram Ramanathan Avatar answered Nov 14 '22 23:11

Ram Ramanathan


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

like image 37
Les Vogel - Google DevRel Avatar answered Nov 14 '22 23:11

Les Vogel - Google DevRel