Vision API on Your Local Linux Machine Using Python
If you are really keen to play around with what Google built to detect faces and all those in Image Annotation this can give you a head start. If you are new to this world this can give you an idea on how to get started up. Not wasting much time, let’s go!
Step 1
Open a Linux shell and start installing python. If its already installed and you have been a Pro at this , feel free to jump ahead. But for all the others install python by following these steps.
sudo apt install python3
sudo apt install python3-pip
Step 2
Okay! Now we have python. What we need now is Google Cloud package for Python and Google Cloud SDK
pip3 install google.cloud #installs google.cloud for python3#Install the Cloud SDK
curl https://sdk.cloud.google.com | bash
exec -l $SHELL
gcloud init #Initialize the SDK
Installing the SDK is optional. Here I am using cloud storage to store images which needs to be tested. You can actually call it from anywhere. Like a web-url for example. But if you are following me step by step then the last command would start the gcloud tools for you to set it up with your google cloud account. The instructions are pretty straight forward. Configure it and lets move on to the next step
Step 3
In this setup lets enable the API from our cloud console and set up authentication for it. Go to your Cloud Console and on the left pane click on APIs and Services. You should see a button which says “Enable APIs and Services”. Click on it and on the search bar on next page type “Vision” and yous should see the API popping up in the area below. Click on the API and choose Enable on the next page.
One little thing before we start doing cool stuff. We need to setup authentication for our APIs. There are a few ways to do this. You can create an API key for POST requests or use a Service Account JSON to authenticate your API requests. In this example I will setup a service account JSON.
Go to Credentials as shown above and click on Create Credentials. On the drop down choose Service Account Key. Select the account you wanna use on the next screen and choose JSON and click on generate. This will download a JSON file which you can use to fire up API requests. Save the file. Its the single most important step in this whole exercise!
Step 4
Let’s now move on to python. Ideally we will need to store the location of this JSON file in a variable called GOOGLE_APPLICATION_CREDENTIALS. We can do all these things in Python. Use the following code snippet to set this variable to the JSON location.
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "<Location to JSON>"
#Use this at the top of your python code
Step 5
Lets Create cloud buckets to store our photos now
gcloud auth login #Login to your Google Cloud Accountgsutil mb gs://[YOUR BUCKET NAME (GLOBALLY UNIQUE)]/eg: gsutil gs://pogba_images/
I am gonna add an image of Paul Pogba I downloaded into this bucket.
gsutil cp /home/arunjith/Desktop/pogba.jpeg gs://pogba_images/
Now lets go ahead and call the Image Annotator Method on this image with the following python code
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "<Location to JSON>"from google.cloud import vision
client = vision.ImageAnnotatorClient()response = client.face_detection({'source' : {'image_uri': "gs://pogba_images/pogba.jpeg"},})
Step 5
Check the output in this step. See all the details the API spits out and since its in JSON format use a JSON interpreter to structure and lay out every detail.
Hope you will all be able to figure things out from here! Feel free to get in touch in case of questions.
Ciao!