Setting up API and Vision Intro - Google Cloud Tutorial




Welcome everyone to part 2 of the Google Cloud tutorial series. In this tutorial, we're going to be covering the vision API, but also covering the initial set up for just about any of the APIs. Some of the setup that we do here will only need to be done once in this series.

To begin, we're going to do some commands and installations:

First, we'll make a workspace:

cd ~

mkdir gcloudstuff

cd gcloudstuff

sudo apt-get install python-pip

Now we can install the Google Cloud API stuff:

sudo pip install google-cloud

This will install all of the APIs.You can also do specific APIs, like sudo pip install google-cloud-vision for example for vision.

To use any of the APIs, you need to enable them. To do this, I find the easiest thing to do is search for it. Log into the console, use the search, and begin to type the name. In this case, you can just type "vision," and then click on the Google Cloud Vision API. Next, click on "enable." If you forget to do this, you will see:

google.cloud.exceptions.Forbidden: 403
Google Cloud Natural Language API has
not been used in project tutorials-161020
before or it is disabled. Enable it by
visiting
https://console.developers.google.com/apis/api/language.googleapis.com/overview?project=tutorials-161020
then retry. If you enabled this API recently,
wait a few minutes for the action to propagate
to our systems and retry.
(POST https://language.googleapis.com/v1/documents:analyzeSentiment)

This should alert you that you've forgotten.

Next, we need to to setup the API credentials. To do this, open the side bar by clicking on the hamburger icon, and then choose the API manager, then go to Credentials on the side.

Now choose to create credentials > choose "service account key" > choose new service account > Make a name > select a role. I personally chose project > owner, so we had full access, but you can make specific keys for specific people if you would like. Now hit create, and this will return to you a json file of your key information. Open up this .json file, and copy the contents. Now head back to the server shell, and do nano apikey.json to open up a new file in the shell, and paste in the json contents. Different shells will act differently, some will support control+c and control+v, others will copy and paste with a right click, others can be right click copied and pasted. To leave nano, control+x to leave, y to save it, and you're all set.

Once you've saved this, we need to set the path to it, so the machine knows where to find our credentials (which we need when using the Google Cloud API). We can do this once by doing:

export GOOGLE_APPLICATION_CREDENTIALS=~/gcloudstuff/apikey.json

This is fine until you reboot. If you'd like this to be permanent, then you can instead do nano ~/.profile, and paste export GOOGLE_APPLICATION_CREDENTIALS=~/gcloudstuff/apikey.json at the bottom. Exit and save, then do: source ~/.bashrc.

Okay, so we've setup all the base API things, and now we're going to go ahead and work specifically with the Vision API:

mkdir visiontut

cd visiontut

Now we need an image. You can use WHATEVER image you like for this, but I will be using one of Guido van Rossum:

wget https://upload.wikimedia.org/wikipedia/commons/thumb/9/94/Guido_van_Rossum_OSCON_2006_cropped.png/375px-Guido_van_Rossum_OSCON_2006_cropped.png

Now we have this image locally, so we see what the Google Vision API sees in this image.

import io
from google.cloud import vision

vision_client = vision.Client()
file_name = '375px-Guido_van_Rossum_OSCON_2006_cropped.png'

with io.open(file_name, 'rb') as image_file:
    content = image_file.read()
    image = vision_client.image(
        content=content, )

labels = image.detect_labels()
for label in labels:
    print(label.description)

The output in this case:

hair
face
person
facial hair
nose
chin
hairstyle
beard
head
senior citizen

This is just a quick example of using the Vision API, which we'll jump more into in the next tutorial.

The next tutorial:





  • Intro and creating a virtual machine - Google Cloud Tutorial
  • Setting up API and Vision Intro - Google Cloud Tutorial
  • Vision API continued - Google Cloud Tutorial
  • Natural Language API - Google Cloud Tutorial
  • Translation API - Google Cloud Tutorial