Azure Cognitive Services Computer Vision Client Library (Deprecated)
This is a deprecated Python client library for Microsoft Azure Cognitive Services Computer Vision, offering algorithms for image analysis, OCR, and other vision tasks. The current version is 0.9.1. This package is no longer actively maintained and has been superseded by `azure-ai-vision-imageanalysis`. Users are strongly encouraged to migrate to the new library for continued feature updates and non-security bug fixes.
Common errors
-
azure.cognitiveservices.vision.computervision.models._models_py3.ComputerVisionErrorResponseException: (PermissionDenied) Access denied due to invalid subscription key or wrong API endpoint.
cause The provided subscription key is invalid, expired, or does not match the regional endpoint being used.fixVerify that your `VISION_KEY` and `VISION_ENDPOINT` environment variables (or hardcoded values, though not recommended) are correct and correspond to an active Azure Computer Vision resource. Ensure the endpoint URL exactly matches the region where the resource was deployed. -
azure.cognitiveservices.vision.computervision.models._models_py3.ComputerVisionErrorResponseException: (ResourceNotFound) Resource not found.
cause The endpoint URL is incorrect, the resource does not exist, or the API path/version in the URL is unsupported for your resource.fixDouble-check the `VISION_ENDPOINT` URL for typos. Confirm the Azure Computer Vision resource is active in the portal. Ensure the API version and path being called are valid for your resource and match the endpoint. -
azure.cognitiveservices.vision.computervision.models._models_py3.ComputerVisionErrorResponseException: (InvalidImageSize) Image must be at least 50 pixels in width and height.
cause The input image dimensions (width or height) are smaller than the minimum required size, or the image file size exceeds the allowed limit (e.g., 4MB for v3.2, 20MB for v4.0).fixEnsure that images are at least 50x50 pixels. Check the file size against the API limits. For local images, consider resizing or compressing before sending. -
azure.cognitiveservices.vision.computervision.models._models_py3.ComputerVisionErrorResponseException: (InvalidImageFormat) Input data is not a valid image.
cause The provided image data is corrupted, in an unsupported format, or the URL provided for a remote image does not link directly to an image file (e.g., it's a redirect or a webpage).fixVerify the image file integrity and format (e.g., JPG, PNG, BMP). If using a URL, ensure it's a direct link to the image file, not a redirect or a webpage containing the image. For local files, ensure the file stream is correctly passed.
Warnings
- breaking This library (`azure-cognitiveservices-vision-computervision`) has been deprecated. It stopped receiving new features and non-security bug fixes after November 1, 2024, and will only receive security fixes until that date. All users should migrate to the replacement package, `azure-ai-vision-imageanalysis` (Track 2 SDK).
- breaking Older Computer Vision API versions (v1.0, v2.0, v2.1, v3.0, and v3.1) will be retired on September 13, 2026. This SDK uses these older API versions. After this date, API calls to these versions will fail.
- gotcha Client authentication requires using the correct regional endpoint URL that matches your Azure Computer Vision resource, not a global endpoint. Mismatched keys/endpoints are a common source of 401 errors.
- gotcha Earlier versions (e.g., 0.2.0) introduced breaking changes by switching to keyword-only arguments for model signatures. Later changes also affected how enum types behave (using `str` mixin).
Install
-
pip install azure-cognitiveservices-vision-computervision pillow
Imports
- ComputerVisionClient
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
- CognitiveServicesCredentials
from msrest.authentication import CognitiveServicesCredentials
- VisualFeatureTypes
from azure.cognitiveservices.vision.computervision import VisualFeatureTypes
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
Quickstart
import os
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from msrest.authentication import CognitiveServicesCredentials
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
# Set up your Computer Vision subscription key and endpoint as environment variables
VISION_KEY = os.environ.get('VISION_KEY', 'YOUR_VISION_SUBSCRIPTION_KEY')
VISION_ENDPOINT = os.environ.get('VISION_ENDPOINT', 'YOUR_VISION_ENDPOINT')
if not VISION_KEY or not VISION_ENDPOINT:
print("Please set the VISION_KEY and VISION_ENDPOINT environment variables.")
exit()
# Authenticate the client
credentials = CognitiveServicesCredentials(VISION_KEY)
computervision_client = ComputerVisionClient(VISION_ENDPOINT, credentials)
# Analyze a remote image
remote_image_url = "https://learn.microsoft.com/azure/ai-services/computer-vision/media/quickstarts/presentation.png"
print(f"\nAnalyzing image from URL: {remote_image_url}")
# Select the visual features to analyze
image_features = [VisualFeatureTypes.categories, VisualFeatureTypes.description, VisualFeatureTypes.tags]
analyzed_results = computervision_client.analyze_image(remote_image_url, image_features)
# Print results
print("Description:")
if analyzed_results.description.captions:
for caption in analyzed_results.description.captions:
print(f" '{caption.text}' with confidence {caption.confidence:.2f}")
print("Tags:")
if analyzed_results.tags:
for tag in analyzed_results.tags:
print(f" '{tag.name}' with confidence {tag.confidence:.2f}")
print("Categories:")
if analyzed_results.categories:
for category in analyzed_results.categories:
print(f" '{category.name}' with confidence {category.score:.2f}")