Microsoft Azure Cognitive Services Computer Vision Client Library for Python providing state-of-the-art algorithms to process images and return information including mature content detection, face detection, color analysis, image categorization, description generation, and thumbnail creation.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Generate detailed tags for image content with confidence scores. The tagging service identifies objects, concepts, activities, and attributes present in images, providing a comprehensive set of keywords that describe the visual content.
Extract comprehensive tags that describe various aspects of image content including objects, activities, settings, and visual attributes.
def tag_image(url, language="en", model_version="latest", custom_headers=None, raw=False, **operation_config):
"""
Generate tags for image content.
Args:
url (str): Publicly reachable URL of an image
language (str, optional): Output language for tags.
Supported: "en", "es", "ja", "pt", "zh". Default: "en"
model_version (str, optional): AI model version. Default: "latest"
custom_headers (dict, optional): Custom HTTP headers
raw (bool, optional): Return raw response. Default: False
Returns:
TagResult: Generated tags with confidence scores
Raises:
ComputerVisionErrorResponseException: API error occurred
"""
def tag_image_in_stream(image, language="en", model_version="latest", custom_headers=None, raw=False, **operation_config):
"""
Generate tags from binary image stream.
Args:
image (Generator): Binary image data stream
language (str, optional): Output language for tags
model_version (str, optional): AI model version
Returns:
TagResult: Generated tags with confidence scores and metadata
"""from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from msrest.authentication import CognitiveServicesCredentials
# Initialize client
credentials = CognitiveServicesCredentials("your-api-key")
client = ComputerVisionClient("https://your-endpoint.cognitiveservices.azure.com/", credentials)
# Generate tags for image
image_url = "https://example.com/beach-scene.jpg"
tag_result = client.tag_image(image_url)
print(f"Generated {len(tag_result.tags)} tags:")
for tag in tag_result.tags:
print(f" {tag.name}: {tag.confidence:.3f}")# Filter tags by confidence threshold
image_url = "https://example.com/complex-scene.jpg"
tag_result = client.tag_image(image_url)
# Define confidence thresholds
high_confidence = 0.8
medium_confidence = 0.5
high_conf_tags = [tag for tag in tag_result.tags if tag.confidence >= high_confidence]
medium_conf_tags = [tag for tag in tag_result.tags if medium_confidence <= tag.confidence < high_confidence]
low_conf_tags = [tag for tag in tag_result.tags if tag.confidence < medium_confidence]
print("High confidence tags:")
for tag in high_conf_tags:
print(f" {tag.name} ({tag.confidence:.3f})")
print(f"\nMedium confidence tags: {len(medium_conf_tags)}")
print(f"Low confidence tags: {len(low_conf_tags)}")# Generate tags in different languages
image_url = "https://example.com/street-food.jpg"
languages = ["en", "es", "ja"]
all_tags = {}
for lang in languages:
try:
tag_result = client.tag_image(image_url, language=lang)
all_tags[lang] = [(tag.name, tag.confidence) for tag in tag_result.tags[:5]] # Top 5 tags
except Exception as e:
print(f"Failed to get tags in {lang}: {e}")
# Display results
for lang, tags in all_tags.items():
print(f"\n{lang.upper()} tags:")
for name, confidence in tags:
print(f" {name} ({confidence:.3f})")# Analyze tags by category/type
tag_result = client.tag_image(image_url)
# Categorize tags (example categorization)
categories = {
'objects': [],
'people': [],
'activities': [],
'settings': [],
'colors': [],
'other': []
}
# Simple categorization logic (you can expand this)
for tag in tag_result.tags:
tag_name = tag.name.lower()
if any(word in tag_name for word in ['person', 'man', 'woman', 'child', 'people']):
categories['people'].append(tag)
elif any(word in tag_name for word in ['red', 'blue', 'green', 'yellow', 'black', 'white']):
categories['colors'].append(tag)
elif any(word in tag_name for word in ['indoor', 'outdoor', 'room', 'kitchen', 'street']):
categories['settings'].append(tag)
elif any(word in tag_name for word in ['sitting', 'standing', 'walking', 'running', 'playing']):
categories['activities'].append(tag)
else:
categories['objects'].append(tag)
# Display categorized results
for category, tags in categories.items():
if tags:
print(f"\n{category.upper()}:")
for tag in tags:
print(f" {tag.name} ({tag.confidence:.3f})")# Generate tags from local image file
with open("local_image.jpg", "rb") as image_stream:
tag_result = client.tag_image_in_stream(image_stream)
# Get top tags above threshold
threshold = 0.7
top_tags = [tag for tag in tag_result.tags if tag.confidence >= threshold]
if top_tags:
print(f"Top tags (confidence ≥ {threshold}):")
for tag in top_tags:
print(f" {tag.name}: {tag.confidence:.3f}")
else:
print(f"No tags found above confidence threshold {threshold}")
print("All tags:")
for tag in tag_result.tags[:10]: # Show top 10
print(f" {tag.name}: {tag.confidence:.3f}")# Process multiple images and aggregate tags
image_urls = [
"https://example.com/image1.jpg",
"https://example.com/image2.jpg",
"https://example.com/image3.jpg"
]
all_tags = {} # Dictionary to aggregate tags across images
for i, url in enumerate(image_urls):
try:
tag_result = client.tag_image(url)
print(f"Processed image {i+1}/{len(image_urls)}")
# Aggregate tags
for tag in tag_result.tags:
if tag.name in all_tags:
all_tags[tag.name].append(tag.confidence)
else:
all_tags[tag.name] = [tag.confidence]
except Exception as e:
print(f"Error processing {url}: {e}")
# Calculate average confidence for each tag
tag_averages = {
tag_name: sum(confidences) / len(confidences)
for tag_name, confidences in all_tags.items()
}
# Sort by average confidence and frequency
popular_tags = sorted(
[(name, avg_conf, len(all_tags[name])) for name, avg_conf in tag_averages.items()],
key=lambda x: (x[2], x[1]), # Sort by frequency, then confidence
reverse=True
)
print("\nMost common tags across all images:")
for tag_name, avg_confidence, frequency in popular_tags[:10]:
print(f" {tag_name}: appears in {frequency} images, avg confidence {avg_confidence:.3f}")# Analyze tag hints for additional context
tag_result = client.tag_image(image_url)
print("Tags with hints:")
for tag in tag_result.tags:
if hasattr(tag, 'hint') and tag.hint:
print(f" {tag.name} ({tag.confidence:.3f}) - Hint: {tag.hint}")
else:
print(f" {tag.name} ({tag.confidence:.3f})")class TagResult:
"""
Image tagging operation result.
Attributes:
tags (list[ImageTag]): Generated tags with confidence scores
request_id (str): Request identifier
metadata (ImageMetadata): Image metadata (dimensions, format)
model_version (str): AI model version used for tagging
"""class ImageTag:
"""
Individual image tag with confidence score.
Attributes:
name (str): Tag name/label describing image content
confidence (float): Confidence score for the tag (0.0 to 1.0)
hint (str, optional): Additional context or hint about the tag
"""class ImageMetadata:
"""
Image metadata information.
Attributes:
height (int): Image height in pixels
width (int): Image width in pixels
format (str): Image format (e.g., "Jpeg", "Png")
"""The tagging service identifies various types of content:
Install with Tessl CLI
npx tessl i tessl/pypi-azure-cognitiveservices-vision-computervision