Powered By GitBook
Label images with the Cloud Vision API
Learn the basics of image labeling with a simple Cloud Vision API application.
Take the interactive version of this tutorial, which runs in the Cloud Console:
​​
​
​
</walkthrough-alt>

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.

Project setup

Google Cloud 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.
</walkthrough-project-billing-setup>
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:
</walkthrough-enable-apis>

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 Cloud Console.
Open Cloud Shell by clicking the </walkthrough-cloud-shell-icon>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.
If the directory already exists, remove the previous files before cloning:
1
rm -rf python-docs-samples
Copied!
Clone the sample repository:
1
git clone https://github.com/googleapis/python-vision.git
Copied!
Change directory to the tutorial directory:
1
cd python-vision/samples/snippets/quickstart
Copied!
You are now in the main directory for the sample code.

Explore the application

Enter the following command to view the application code:
1
cat quickstart.py
Copied!
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.
1
gcloud iam service-accounts create vision-quickstart --project {{project_id}}
Copied!
{{project_id}} is your Google Cloud project ID.

Create credentials

Next, create a service account key and set it as your default credentials.
1
gcloud iam service-accounts keys create key.json --iam-account \
2
[email protected]{{project_id}}.iam.gserviceaccount.com
Copied!
1
export GOOGLE_APPLICATION_CREDENTIALS=key.json
Copied!
</walkthrough-test-code-output>

Test your app

To test your app with a sample image, enter the following command:
1
python quickstart.py
Copied!
The image resource, resources/wakeupcat.jpg, is specified in the source. (View image)

Conclusion

</walkthrough-conclusion-trophy>
Congratulations! You are ready to use the Cloud Vision API.
Here's what you can do next:
Last modified 7mo ago