Google Cloud Vision

raw JSON →
3.13.0 verified Tue May 12 auth: no python install: verified quickstart: stale

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.

pip install google-cloud-vision
error ModuleNotFoundError: No module named 'google.cloud.vision'
cause This error occurs when the `google-cloud-vision` library is not installed in the Python environment being used, or the Python interpreter cannot find the installed package.
fix
Ensure the library is correctly installed using pip: pip install google-cloud-vision or python3 -m pip install google-cloud-vision. If using a virtual environment, ensure it is activated before installation and execution.
error google.api_core.exceptions.PermissionDenied: 403 The caller does not have permission
cause This typically indicates that the authenticated service account or user lacks the necessary IAM permissions to access the Google Cloud Vision API, the specific project, or the Google Cloud Storage bucket containing the image.
fix
Verify that the Google Cloud Vision API is enabled for your project. Ensure the service account or user has the 'Vision API User' role (roles/vision.user) and, if accessing images from Cloud Storage, the 'Storage Object Viewer' role (roles/storage.objectViewer) on the relevant bucket. Also, confirm that the GOOGLE_APPLICATION_CREDENTIALS environment variable correctly points to a valid service account key file.
error google.api_core.exceptions.InvalidArgument: 400 Request contains an invalid argument.
cause This error signifies that the request sent to the Vision API is malformed, contains incorrect data, or specifies an unsupported image format or URI.
fix
Check the image data for corruption, ensure it's a supported format (JPEG, PNG, GIF, BMP, WEBP, RAW, ICO, PDF, TIFF), and verify that base64 encoded strings are valid. If using a GCS URI, confirm it follows the gs://bucket_name/object_name format and that the object exists and is accessible.
error AttributeError: module 'google.cloud.vision' has no attribute 'Image'
cause This error indicates that the `Image` class is not directly accessible under the `google.cloud.vision` module in the current version of the library. The client library structure changed how image objects are instantiated.
fix
Instead of vision.Image(), use vision.types.Image() or vision_v1.types.Image() if you've imported a specific version, or directly instantiate with content: image = vision.Image(content=content).
error AttributeError: 'ImageAnnotatorClient' object has no attribute 'text_detection'
cause This error occurs when attempting to call `text_detection` directly on an `ImageAnnotatorClient` instance. The correct method for text detection is `text_detection()` as part of a `client.annotate_image()` or `client.batch_annotate_images()` request, or the simplified `label_detection`, `face_detection`, etc. methods which take an `Image` object. The `text_detection` method is not a direct attribute of the client object itself.
fix
Use the text_detection feature within an annotate_image call. For example: response = client.text_detection(image=image) (if using the older simplified client) or more commonly: response = client.annotate_image({'image': image, 'features': [{'type': vision.Feature.Type.TEXT_DETECTION}]}).
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.
fix Set `GOOGLE_APPLICATION_CREDENTIALS` environment variable. Grant the 'Cloud Vision API User' IAM role to your service account. For GCS images, ensure the service account also has 'Storage Object Viewer' or similar access.
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`).
fix Update your code to use `client = vision.ImageAnnotatorClient()` and refer to types as `vision.Image`, `vision.Feature`, etc. Refer to the migration guide for older versions (though the PyPI version is 3.x, older client libraries could have been 0.x).
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.
fix Visit the Google Cloud Console, select your project, ensure billing is enabled, and navigate to 'APIs & Services > Library' to enable the 'Cloud Vision API'.
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.
fix Monitor your API usage in the Google Cloud Console. Validate image inputs (format, size) before sending them. Implement retry logic with exponential backoff for transient errors like `RESOURCE_EXHAUSTED`.
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.
fix Review the official Vision API deprecations page for details. For OCR On-Prem, migrate to the standard Vision API OCR. Evaluate alternatives or plan for shutdown for other deprecated features.
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.
fix Understand the distinction between the pre-trained Cloud Vision API and AutoML Vision. If you are using AutoML Vision, migrate to Vertex AI as recommended.
python os / libc status wheel install import disk
3.10 alpine (musl) - - 1.94s 72.6M
3.10 slim (glibc) - - 1.08s 70M
3.11 alpine (musl) - - 2.69s 78.0M
3.11 slim (glibc) - - 1.68s 76M
3.12 alpine (musl) - - 2.71s 69.3M
3.12 slim (glibc) - - 2.12s 67M
3.13 alpine (musl) - - 2.63s 68.8M
3.13 slim (glibc) - - 2.55s 67M
3.9 alpine (musl) - - 1.80s 72.8M
3.9 slim (glibc) - - 1.28s 71M

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