Setup Google Cloud Vision API in 10 Steps
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:
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”
Step 2: Select “ENABLE APIS AND SERVICES”
Step 3: Search for Cloud Vision API then click on it
Step 4: Enable it
Step 5: Navigate to Credentials then select “CREATE CREDENTIALS” and click on “Service Account”
Step 6: Provide your service account details (you can leave out the optional parts) and click done
Step 7: Click into the service account you’ve created
Step 8: Navigate to KEYS, select “ADD KEY” then “Create new key”
Step 9: Create a JSON key type
Recommended by LinkedIn
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.)
!pip install google-cloud-vision
import os
from google.cloud import vision_v1
from google.cloud.vision_v1 import types
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'unique-sentinel-320212-91cb79ab2383.json' # Your JSON file name
client = vision_v1.ImageAnnotatorClient()
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
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.))
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)))
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: