Google Cloud Vision

3.13.0 · active · verified Sat Mar 28

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

Install

Imports

Quickstart

This quickstart demonstrates how to perform label detection on an image from a Google Cloud Storage (GCS) URI. Ensure you have set up Application Default Credentials, typically by setting the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to the path of your service account key file, or by running `gcloud auth application-default login` for local development. For production environments on Google Cloud, authentication is usually handled automatically by the attached service account.

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')

view raw JSON →