As of January 1, 2020 this library no longer supports Python 2 on the latest released version. Library versions released prior to that date will continue to be available. For more information please visit Python 2 support on Google Cloud.

Python Client for Google Cloud Vision

GA pypi versions

The Google Cloud Vision API enables developers to understand the content of an image by encapsulating powerful machine learning models in an easy to use REST API. It quickly classifies images into thousands of categories (e.g., “sailboat”, “lion”, “Eiffel Tower”), detects individual objects and faces within images, and finds and reads printed words contained within images. You can build metadata on your image catalog, moderate offensive content, or enable new marketing scenarios through image sentiment analysis. Analyze images uploaded in the request or integrate with your image storage on Google Cloud Storage.

Quick Start

In order to use this library, you first need to go through the following steps:

  1. Select or create a Cloud Platform project.

  2. Enable billing for your project.

  3. Enable the Google Cloud Vision API.

  4. Setup Authentication.

Installation

Install this library in a virtualenv using pip. virtualenv is a tool to create isolated Python environments. The basic problem it addresses is one of dependencies and versions, and indirectly permissions.

With virtualenv, it’s possible to install this library without needing system install permissions, and without clashing with the installed system dependencies.

Supported Python Versions

Python >= 3.6

Deprecated Python Versions

Python == 2.7.

The last version of this library compatible with Python 2.7 is google-cloud-vision==1.0.0.

RaspberryPi ARM devices

Note: Raspberry Pi ARMv6 is not supported because there is an internal binary google that does not comply with armv6 processors.

Mac/Linux

pip install virtualenv
virtualenv <your-env>
source <your-env>/bin/activate
<your-env>/bin/pip install google-cloud-vision

Windows

pip install virtualenv
virtualenv <your-env>
<your-env>\Scripts\activate
<your-env>\Scripts\pip.exe install google-cloud-vision

Example Usage

from google.cloud import vision

client = vision.ImageAnnotatorClient()
response = client.annotate_image({
  'image': {'source': {'image_uri': 'gs://my-test-bucket/image.jpg'}},
  'features': [{'type_': vision.Feature.Type.FACE_DETECTION}]
})

Next Steps

Note

Because this client uses grpcio library, it is safe to share instances across threads. In multiprocessing scenarios, the best practice is to create client instances after the invocation of os.fork() by multiprocessing.Pool or multiprocessing.Process.

Using the Library

Annotate an Image

You can call the annotate_image() method directly:

>>> from google.cloud import vision
>>> client = vision.ImageAnnotatorClient()
>>> response = client.annotate_image({
...   'image': {'source': {'image_uri': 'gs://my-test-bucket/image.jpg'}},
...   'features': [{'type': vision.enums.Feature.Type.FACE_DETECTION}],
... })
>>> len(response.annotations)
2
>>> for face in response.annotations[0].faces:
...     print(face.joy)
Likelihood.VERY_LIKELY
Likelihood.VERY_LIKELY
Likelihood.VERY_LIKELY
>>> for logo in response.annotations[0].logos:
...     print(logo.description)
'google'
'github'

Single-feature Shortcuts

If you are only requesting a single feature, you may find it easier to ask for it using our direct methods:

>>> from google.cloud import vision
>>> client = vision.ImageAnnotatorClient()
>>> response = client.face_detection({
...   'source': {'image_uri': 'gs://my-test-bucket/image.jpg'},
... })
>>> len(response.annotations)
1
>>> for face in response.annotations[0].faces:
...     print(face.joy)
Likelihood.VERY_LIKELY
Likelihood.VERY_LIKELY
Likelihood.VERY_LIKELY

No results found

If no results for the detection performed can be extracted from the image, then an empty list is returned. This behavior is similar with all detection types.

Example with logo_detection():

>>> from google.cloud import vision
>>> client = vision.ImageAnnotatorClient()
>>> with open('./image.jpg', 'rb') as image_file:
...     content = image_file.read()
>>> response = client.logo_detection({
...     'content': content,
... })
>>> len(response.annotations)
0

API Reference

This package includes clients for multiple versions of the Vision API. By default, you will get v1, the latest stable version.

A new beta release with additional features over the current stable version, spelled v1p4beta1, is provided to allow you to use these new features. These are expected to move into the stable release soon; until then, the usual beta admonishment (changes are possible, etc.) applies.

Previous beta releases v1p3beta1 and v1p2beta1 are provided as well.

An API and type reference is provided for v1p3beta1:

An API and type reference is provided for v1p2beta1:

Migration Guide

See the guide below for instructions on migrating to the 2.x release of this library.