Goolge Cloud Vision API Example in Python
....will run on your Google Cloud Account
STEP 1: log into (create if you dont have one) Google Cloud Account
STEP 2: create a new project inside Cloud Console
The result is a new project --- take note of your project ID you will use it later
STEP3: Enable the Vision API in your project
Navigate to the API Manager
|
STEP 4: Setup some basic code
you have a lot of options here -you can use any IDE you want and then learn how to deploy the code to Google Cloud. and hook up your code to a GitHub repository. PyCharm is a good idea for Python from itelliJ ...
NOTE you can develop in any language you want not just Python. Read about Google's support for other languages like Java. See here for example code in Java.
another option --not as good for long-term coding or more elaborate coding but, for a quick demo we will use the Google Cloud console's built-in
Using Google Cloud Shell to clone an existing code sample given by Google
Cloud Shell is a built-in command line tool for the console. We're going to use Cloud Shell to run our app.
STEP 1: Open Google Cloud Shell
Open Cloud Shell by clicking the cloud shell icon in the navigation bar at the top.
STEP 2: Clone the sample code
Use Cloud Shell to clone and navigate to the "Hello World" code. The sample code is cloned from your project repository to the Cloud Shell.
In Cloud Shell enter:
Clone a sample repository:
step a: create an evironment variable we will use to create a sample directory in your project
TUTORIALDIR=src/YOURProjectID/python_vision_quickstartFOR ME THIS WOULD MEAN TYPING IN
TUTORIALDIR=src/isight-2018/python_vision_quickstart
Step b: grab one of the Existing (search on Google Cloud Vision) Git Repository samples you want to try
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git $TUTORIALDIR
HERE is an example of me doing the above
NOW change directory into the newly copied code (note the directory will be dependent on the code you cloned)
cd $TUTORIALDIR/vision/cloud-client/quickstart
Python code that reads in a image file stored in resources/wakeupcat.jpg and calls the Google Vision label detection on it.
.... note it first does importing of google cloud vision library and istantiates a vision ImageAnnotator client.************quickstart.py file************
#!/usr/bin/env python
# Copyright 2016 Google Inc. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
def run_quickstart():
# [START vision_quickstart]
import io
import os
# Imports the Google Cloud client library
# [START migration_import]
from google.cloud import vision
from google.cloud.vision import types
# [END migration_import]
# Instantiates a client
# [START migration_client]
client = vision.ImageAnnotatorClient()
# [END migration_client]
# The name of the image file to annotate
file_name = os.path.join(
os.path.dirname(__file__),
'resources/wakeupcat.jpg')
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
print(label.description)
# [END vision_quickstart]
if __name__ == '__main__':
run_quickstart()
STEP 5: Create Credentials
-
need credentials to use API so your application can authenticate its identity to service
create service account, a service account key and set the key as our default credentials.type in the Google Cloud Console once you are logged in >>>>>to get to console hit the icon for it in navigation bar <gcloud iam service-accounts create vision-quickstart; \ Here is example of me running this and the name of the service account is vision-quickstart for my project id isight-2018 |
alternative --- do it through the main console
|
STEP 6: Run your code (then modify and rerun)
Image we will process |
run from the Google Cloud Console
python quickstart.py