Setup Google Cloud Vision API in 10 Steps
Photo by cottonbro from Pexels.

Setup Google Cloud Vision API in 10 Steps

Google Cloud Vision API Results

Have you ever wondered and dreamt about the cool applications of Computer Vision?

Well, with Google Cloud Vision API, you can leverage on Google’s pre-trained model and apply them to your problems easily.

In this test drive, I will walkthrough the 10 steps to set this up and running.


Before I begin, all this is made possible through the amazing documentation that Google has provided. Key references can be found at:

  1. https://cloud.google.com/vision
  2. https://cloud.google.com/vision/docs/libraries#installing_the_client_library
  3. https://googleapis.dev/python/vision/latest/index.html
  4. My GitHub for this mini project: https://github.com/ray-xuanruilee/GoogleCloudVisionAPI_Test

Assumptions: You have signed up for and created your Google Cloud Project. (If you haven’t done so, please visit this link and new users get $300 credit too.)

This guide is accurate as at 29 July 2021, Thursday.


Step 1: Login to your Google Cloud Project , within Home, click on “Go to APIs overview”

No alt text provided for this image

Step 2: Select “ENABLE APIS AND SERVICES”

No alt text provided for this image

Step 3: Search for Cloud Vision API then click on it

No alt text provided for this image

Step 4: Enable it

No alt text provided for this image

Step 5: Navigate to Credentials then select “CREATE CREDENTIALS” and click on “Service Account”

No alt text provided for this image

Step 6: Provide your service account details (you can leave out the optional parts) and click done

No alt text provided for this image

Step 7: Click into the service account you’ve created

No alt text provided for this image

Step 8: Navigate to KEYS, select “ADD KEY” then “Create new key”

No alt text provided for this image

Step 9: Create a JSON key type

  • A JSON file will be downloaded
  • Put this JSON file into the working directory of your Python script

No alt text provided for this image


Now that we have setup the Google Cloud Vision API, we can get it started with a few lines of code. (My GitHub project for your reference can be found here if there are issues with formatting below.)

Step 10: Let’s get it started with Python! (I will be using Jupyter Notebook here.)

  • Install the Google Cloud Vision library that will allow us to make requests to the Google Cloud Vision API

!pip install google-cloud-vision        

  • Import the libraries

import os
from google.cloud import vision_v1        

from google.cloud.vision_v1 import types        

  • Insert your Google Cloud Vision API key (this was the JSON file downloaded earlier that should be stored in your working directory)

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'unique-sentinel-320212-91cb79ab2383.json' # Your JSON file name        

  • Instantiate the client

client = vision_v1.ImageAnnotatorClient()        

  • Set image to be analyzed

image = types.Image()        

image.source.image_uri = 'https://www.indiewire.com/wp-content/uploads/2016/08/20140216-131646.jpg' # You can put in another URL here that you would like analyzed        

  • Utilize label detection

response_labels = client.label_detection(image=image)
print('Labels (and confidence score):');print('='*30)
for label in response_labels.label_annotations:        

    print(label.description, '(%.0f%%)' % (label.score*100.))        

  • Utilize face detection

response_faces = client.face_detection(image=image)
# Names of likelihood from google.cloud.vision.enums
likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
                       'LIKELY', 'VERY_LIKELY')
    
print('Faces (and confidence score)');print('='*30)

for face in response_faces.face_annotations:
    print('anger: {}'.format(likelihood_name[face.anger_likelihood]))
    print('joy: {}'.format(likelihood_name[face.joy_likelihood]))
    print('surprise: {}'.format(likelihood_name[face.surprise_likelihood]))
    vertices = (['({},{})'.format(vertex.x, vertex.y)
                for vertex in face.bounding_poly.vertices])        


   print('face bounds: {}'.format(','.join(vertices)))        

  • There are a few other methods like detecting text, handwriting, etc. that you can learn more here
  • Well, when you’re done, you might want to disable your API

No alt text provided for this image


Well, there you go. I hope you guys found this helpful and let me know what you liked in the comments section below!

May you wander in wisdom and invoke data to evoke insights. 

If you guys like what I wrote, follow me here on:

  1. LinkedIn
  2. Medium
  3. Twitter
  4. GitHub

To view or add a comment, sign in

Others also viewed

Explore content categories