Google Cloud Vision
The `google-cloud-vision` Python client library provides access to the Google Cloud Vision API, a powerful service for image analysis. It enables developers to integrate vision detection features like image labeling, optical character recognition (OCR), face and landmark detection, object localization, and explicit content tagging into their applications. This library is actively maintained by Google, with frequent updates released as part of the broader `google-cloud-python` monorepo.
Warnings
- gotcha Authentication is critical and often a source of error. Ensure your environment has valid credentials, typically via the `GOOGLE_APPLICATION_CREDENTIALS` environment variable pointing to a service account key file. On Google Cloud infrastructure (e.g., Compute Engine, Cloud Functions), credentials are often automatically inferred from the service account attached to the resource. Make sure the service account has the 'Cloud Vision API User' role and any necessary GCS permissions if accessing images from buckets.
- breaking Older versions of the client library used `vision.Client()` for instantiation and directly accessed types like `vision.types.Image`. The recommended and current approach uses `vision.ImageAnnotatorClient()` and accesses types directly from the `vision` module (e.g., `vision.Image`, `vision.Feature`).
- gotcha The Google Cloud Vision API requires a Google Cloud project with billing enabled and the Vision API explicitly enabled. Even free tier usage counts against an enabled billing account.
- gotcha Exceeding API quotas or sending malformed image data are common causes of failures. Errors like `RESOURCE_EXHAUSTED` (429) indicate quota limits, while `INVALID_ARGUMENT` (400) often points to issues with the image format, size, or content.
- deprecated Specific features within the Vision API have deprecation timelines. 'Vision API Celebrity Recognition' and 'OCR On-Prem' features are deprecated and scheduled for shutdown by September 16, 2025.
- gotcha AutoML Vision, a distinct product for custom image models, has had its legacy version deprecated and moved to Vertex AI. This is sometimes confused with the core Cloud Vision API, which remains actively developed and supported.
Install
-
pip install google-cloud-vision
Imports
- ImageAnnotatorClient
from google.cloud import vision client = vision.ImageAnnotatorClient()
- Image
from google.cloud import vision image = vision.Image(content=b'...')
- Feature
from google.cloud import vision feature = vision.Feature(type_=vision.Feature.Type.LABEL_DETECTION)
Quickstart
import os
from google.cloud import vision
# Set up authentication if running locally (e.g., via service account key file)
# On Google Cloud (e.g., GCE, Cloud Functions), this is often handled automatically.
# os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS', '/path/to/your/keyfile.json')
def detect_labels_uri(image_uri):
"""Detects labels in the image located in Google Cloud Storage or on the Web."""
client = vision.ImageAnnotatorClient()
image = vision.Image()
image.source.image_uri = image_uri
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
print(f'{label.description}: {label.score:.2f}')
# Example usage with a publicly accessible image URI
# Make sure the image URI is publicly accessible or your service account has GCS read permissions.
detect_labels_uri('gs://cloud-samples-data/vision/label/wakeupcat.jpg')