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
Specialized image analysis using domain-specific models for celebrity and landmark recognition. These models provide detailed information about famous people and notable landmarks when detected in images.
Identify famous people in images with confidence scores and face location information.
def analyze_image_by_domain(model, url, language="en", custom_headers=None, raw=False, **operation_config):
"""
Analyze image using domain-specific model.
Args:
model (str): Domain model name. Available models:
- "celebrities": Detect and identify famous people
- "landmarks": Identify notable landmarks and locations
url (str): Publicly reachable URL of an image
language (str, optional): Output language for results. Default: "en"
custom_headers (dict, optional): Custom HTTP headers
raw (bool, optional): Return raw response. Default: False
Returns:
DomainModelResults: Domain-specific analysis results
Raises:
ComputerVisionErrorResponseException: API error occurred
"""
def analyze_image_by_domain_in_stream(model, image, language="en", custom_headers=None, raw=False, **operation_config):
"""
Domain-specific analysis from binary image stream.
Args:
model (str): Domain model name ("celebrities" or "landmarks")
image (Generator): Binary image data stream
language (str, optional): Output language
Returns:
DomainModelResults: Domain-specific analysis results
"""
def list_models(custom_headers=None, raw=False, **operation_config):
"""
List available domain models.
Returns:
ListModelsResult: Available domain models with descriptions
Note:
This endpoint returns information about all available domain-specific models
including their names and capabilities.
"""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)
# Analyze image for celebrities
image_url = "https://example.com/red-carpet-photo.jpg"
celebrity_results = client.analyze_image_by_domain("celebrities", image_url)
# Check if celebrities were detected
if celebrity_results.result and 'celebrities' in celebrity_results.result:
celebrities = celebrity_results.result['celebrities']
print(f"Detected {len(celebrities)} celebrities:")
for celebrity in celebrities:
print(f"\nCelebrity: {celebrity['name']}")
print(f"Confidence: {celebrity['confidence']:.3f}")
# Face location information
if 'faceRectangle' in celebrity:
face_rect = celebrity['faceRectangle']
print(f"Face location: ({face_rect['left']}, {face_rect['top']}) "
f"size: {face_rect['width']}x{face_rect['height']}")
else:
print("No celebrities detected in the image")# Analyze image for landmarks
image_url = "https://example.com/tourist-photo.jpg"
landmark_results = client.analyze_image_by_domain("landmarks", image_url)
# Check if landmarks were detected
if landmark_results.result and 'landmarks' in landmark_results.result:
landmarks = landmark_results.result['landmarks']
print(f"Detected {len(landmarks)} landmarks:")
for landmark in landmarks:
print(f"\nLandmark: {landmark['name']}")
print(f"Confidence: {landmark['confidence']:.3f}")
# Additional landmark information
if 'description' in landmark:
print(f"Description: {landmark['description']}")
if 'location' in landmark:
location = landmark['location']
print(f"Location: {location.get('name', 'Unknown')}")
else:
print("No landmarks detected in the image")# List all available domain models
models_result = client.list_models()
print("Available domain models:")
for model in models_result.models_property:
print(f"\nModel: {model.name}")
if hasattr(model, 'categories') and model.categories:
print(f"Categories: {', '.join(model.categories)}")
if hasattr(model, 'description') and model.description:
print(f"Description: {model.description}")# Analyze the same image with both models
image_url = "https://example.com/event-photo.jpg"
print("Analyzing image with multiple domain models...")
# Celebrity analysis
celebrity_results = client.analyze_image_by_domain("celebrities", image_url)
celebrities_found = 0
if celebrity_results.result and 'celebrities' in celebrity_results.result:
celebrities_found = len(celebrity_results.result['celebrities'])
# Landmark analysis
landmark_results = client.analyze_image_by_domain("landmarks", image_url)
landmarks_found = 0
if landmark_results.result and 'landmarks' in landmark_results.result:
landmarks_found = len(landmark_results.result['landmarks'])
print(f"\nAnalysis Summary:")
print(f"Celebrities detected: {celebrities_found}")
print(f"Landmarks detected: {landmarks_found}")
# Detailed results
if celebrities_found > 0:
print(f"\nCelebrities:")
for celebrity in celebrity_results.result['celebrities']:
print(f" - {celebrity['name']} (confidence: {celebrity['confidence']:.3f})")
if landmarks_found > 0:
print(f"\nLandmarks:")
for landmark in landmark_results.result['landmarks']:
print(f" - {landmark['name']} (confidence: {landmark['confidence']:.3f})")# Analyze local image file for celebrities
with open("party_photo.jpg", "rb") as image_stream:
celebrity_results = client.analyze_image_by_domain_in_stream(
"celebrities",
image_stream
)
if celebrity_results.result and 'celebrities' in celebrity_results.result:
for celebrity in celebrity_results.result['celebrities']:
print(f"Found: {celebrity['name']} (confidence: {celebrity['confidence']:.3f})")
else:
print("No celebrities detected in local image")# Process multiple images for celebrity detection
image_urls = [
"https://example.com/movie-premiere1.jpg",
"https://example.com/movie-premiere2.jpg",
"https://example.com/award-show.jpg"
]
all_celebrities = set()
for i, url in enumerate(image_urls, 1):
try:
print(f"Processing image {i}/{len(image_urls)}...")
celebrity_results = client.analyze_image_by_domain("celebrities", url)
if celebrity_results.result and 'celebrities' in celebrity_results.result:
image_celebrities = celebrity_results.result['celebrities']
print(f" Found {len(image_celebrities)} celebrities")
for celebrity in image_celebrities:
all_celebrities.add(celebrity['name'])
print(f" - {celebrity['name']} (confidence: {celebrity['confidence']:.3f})")
else:
print(" No celebrities detected")
except Exception as e:
print(f" Error processing image: {e}")
print(f"\nUnique celebrities across all images: {len(all_celebrities)}")
for name in sorted(all_celebrities):
print(f" - {name}")# Filter domain analysis results by confidence threshold
image_url = "https://example.com/group-photo.jpg"
confidence_threshold = 0.8
# Celebrity analysis with confidence filtering
celebrity_results = client.analyze_image_by_domain("celebrities", image_url)
high_confidence_celebrities = []
if celebrity_results.result and 'celebrities' in celebrity_results.result:
high_confidence_celebrities = [
celebrity for celebrity in celebrity_results.result['celebrities']
if celebrity['confidence'] >= confidence_threshold
]
print(f"High-confidence celebrity detections (≥{confidence_threshold}):")
if high_confidence_celebrities:
for celebrity in high_confidence_celebrities:
print(f" {celebrity['name']}: {celebrity['confidence']:.3f}")
else:
print(" None found above threshold")
# Show all detections for comparison
if celebrity_results.result and 'celebrities' in celebrity_results.result:
all_celebrities = celebrity_results.result['celebrities']
low_confidence = [c for c in all_celebrities if c['confidence'] < confidence_threshold]
if low_confidence:
print(f"\nLower confidence detections:")
for celebrity in low_confidence:
print(f" {celebrity['name']}: {celebrity['confidence']:.3f}")class DomainModelResults:
"""
Domain-specific analysis results.
Attributes:
result (dict): Domain model results containing detected entities
For celebrities: {'celebrities': [{'name': str, 'confidence': float, 'faceRectangle': dict}]}
For landmarks: {'landmarks': [{'name': str, 'confidence': float, 'description': str}]}
request_id (str): Request identifier
metadata (ImageMetadata): Image metadata (dimensions, format)
model_version (str): Domain model version used
"""class ListModelsResult:
"""
Available domain models listing.
Attributes:
models_property (list[ModelDescription]): List of available domain models
"""class ModelDescription:
"""
Description of a domain model.
Attributes:
name (str): Model name (e.g., "celebrities", "landmarks")
categories (list[str], optional): Model categories or capabilities
description (str, optional): Model description and use cases
"""Capabilities:
Celebrity Result Structure:
{
'celebrities': [
{
'name': 'Celebrity Name',
'confidence': 0.95,
'faceRectangle': {
'left': 100,
'top': 150,
'width': 80,
'height': 80
}
}
]
}Capabilities:
Landmark Result Structure:
{
'landmarks': [
{
'name': 'Landmark Name',
'confidence': 0.88,
'description': 'Brief description of the landmark',
'location': {
'name': 'City, Country'
}
}
]
}Install with Tessl CLI
npx tessl i tessl/pypi-azure-cognitiveservices-vision-computervision