Edit on GitHub
Report issue
Page history

Label images with the Cloud Vision API

Author(s): @jscud ,   Published: 2019-07-31


Label images with the Cloud Vision API

Take the interactive version of this tutorial, which runs in the Google Cloud Platform (GCP) Console:

Open in GCP Console

Introduction

This tutorial walks you through a basic application that uses the Vision API. In this application, a LABEL_DETECTION request annotates an image with a label (tag) that is selected based on the image content. For example, a picture of a barn may produce a label of barn, farm, or some other similar annotation.

"Python" and the Python logos are trademarks or registered trademarks of the Python Software Foundation.

Project setup

GCP organizes resources into projects. This allows you to collect all of the related resources for a single application in one place.

Begin by creating a new project or selecting an existing project for this tutorial.

For details, see Creating a project.

Enable the Vision API

Before your project can use a service like the Vision API, you must enable the API for the project.

Use the following to enable the API:

https://console.cloud.google.com/flows/enableapi?apiid=vision.googleapis.com

Open Cloud Shell

In this tutorial, you do much of your work in Cloud Shell, which is a built-in command-line tool for the GCP Console.

Open Cloud Shell by clicking the Activate Cloud Shell button in the navigation bar in the upper-right corner of the console.

Clone the sample code

Use Cloud Shell to clone and navigate to the sample code. The sample code is cloned from your project repository to the Cloud Shell.

rm -rf python-docs-samples

Clone the sample repository:

git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git

Change directory to the tutorial directory:

cd python-docs-samples/vision/cloud-client/quickstart

You are now in the main directory for the sample code.

Explore the application

Enter the following command to view the application code:

cat quickstart.py

The quickstart.py file formats your request information, like the request type and content. Expand each section below to learn about the details.

Requests to the Vision API are provided as JSON objects. See the Vision API reference for complete information on the specific structure of such a request. Your JSON request is only sent when you call execute. This pattern allows you to pass around such requests and call execute as needed.

Set up up a service account and credentials

To use a Cloud API, you need to set up the proper credentials for your application. This enables your application to authenticate its identity to the service and obtain authorization to perform tasks.

Create a service account

You need to create a service account to authenticate your API requests. If you already have the service account created, it will be reused.

gcloud iam service-accounts create vision-quickstart --project 

is your GCP project ID.

Create credentials

Next, create a service account key and set it as your default credentials.

gcloud iam service-accounts keys create key.json --iam-account \
  vision-quickstart@.iam.gserviceaccount.com
export GOOGLE_APPLICATION_CREDENTIALS=key.json

Test your app

To test your app with a sample image, enter the following command:

python quickstart.py

The image resource, resources/wakeupcat.jpg, is specified in the source. (View image)

Conclusion

Congratulations! You are ready to use the Cloud Vision API.

Here's what you can do next:

Submit a Tutorial

Share step-by-step guides

SUBMIT A TUTORIAL

Request a Tutorial

Ask for community help

SUBMIT A REQUEST

GCP Tutorials

Tutorials published by GCP

VIEW TUTORIALS

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.