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.
Common errors
-
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.fixEnsure 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. -
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.fixVerify 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. -
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.fixCheck 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. -
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.fixInstead 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)`. -
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.fixUse 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}]})`.
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.vision import Client
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')