CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-azure-cognitiveservices-vision-computervision

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.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

area-of-interest.mddocs/

Area of Interest

Identify the most important rectangular area within an image for optimal cropping or focus. This feature helps determine where to crop images while preserving the most visually significant content.

Capabilities

Area of Interest Detection

Analyze images to find the rectangular region that contains the most important visual content for cropping purposes.

def get_area_of_interest(url, model_version="latest", custom_headers=None, raw=False, **operation_config):
    """
    Get area of interest in image for optimal cropping.
    
    Args:
        url (str): Publicly reachable URL of an image
        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:
        AreaOfInterestResult: Bounding rectangle of the most important image area
        
    Raises:
        ComputerVisionErrorResponseException: API error occurred
        
    Note:
        The area of interest is returned as a bounding rectangle that can be used
        for intelligent cropping while preserving important visual content.
    """

def get_area_of_interest_in_stream(image, model_version="latest", custom_headers=None, raw=False, **operation_config):
    """
    Get area of interest from binary image stream.
    
    Args:
        image (Generator): Binary image data stream
        model_version (str, optional): AI model version
        
    Returns:
        AreaOfInterestResult: Area of interest bounding rectangle
    """

Usage Examples

Basic Area of Interest Detection

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)

# Get area of interest
image_url = "https://example.com/landscape-photo.jpg"
aoi_result = client.get_area_of_interest(image_url)

# Extract bounding rectangle
area_rect = aoi_result.area_of_interest

print(f"Area of Interest:")
print(f"  Position: ({area_rect.x}, {area_rect.y})")
print(f"  Size: {area_rect.w} x {area_rect.h} pixels")

# Calculate area coverage
if aoi_result.metadata:
    image_width = aoi_result.metadata.width
    image_height = aoi_result.metadata.height
    
    total_area = image_width * image_height
    aoi_area = area_rect.w * area_rect.h
    coverage_percent = (aoi_area / total_area) * 100
    
    print(f"  Coverage: {coverage_percent:.1f}% of total image")

Smart Cropping Using Area of Interest

from PIL import Image
import requests
from io import BytesIO

# Get area of interest
image_url = "https://example.com/group-photo.jpg"
aoi_result = client.get_area_of_interest(image_url)
area_rect = aoi_result.area_of_interest

# Download and crop the original image
response = requests.get(image_url)
original_image = Image.open(BytesIO(response.content))

# Crop to area of interest
cropped_image = original_image.crop((
    area_rect.x,                    # left
    area_rect.y,                    # top  
    area_rect.x + area_rect.w,      # right
    area_rect.y + area_rect.h       # bottom
))

# Save cropped image
cropped_image.save("smart_cropped_image.jpg")

print(f"Original size: {original_image.size}")
print(f"Cropped size: {cropped_image.size}")
print(f"Area of interest: ({area_rect.x}, {area_rect.y}, {area_rect.w}, {area_rect.h})")

Multiple Aspect Ratio Cropping

# Use area of interest to generate crops for different aspect ratios
aoi_result = client.get_area_of_interest(image_url)
area_rect = aoi_result.area_of_interest

# Define target aspect ratios
target_ratios = {
    'square': 1.0,      # 1:1
    'landscape': 1.5,   # 3:2
    'portrait': 0.67,   # 2:3
    'wide': 2.0         # 2:1
}

print(f"Original area of interest: {area_rect.w}x{area_rect.h}")

for name, ratio in target_ratios.items():
    # Calculate dimensions maintaining the aspect ratio
    if area_rect.w / area_rect.h > ratio:
        # Width is too large, constrain by height
        new_height = area_rect.h
        new_width = int(new_height * ratio)
    else:
        # Height is too large, constrain by width  
        new_width = area_rect.w
        new_height = int(new_width / ratio)
    
    # Center the crop within the area of interest
    x_offset = (area_rect.w - new_width) // 2
    y_offset = (area_rect.h - new_height) // 2
    
    crop_x = area_rect.x + x_offset
    crop_y = area_rect.y + y_offset
    
    print(f"{name} crop ({ratio:.2f}): {new_width}x{new_height} at ({crop_x}, {crop_y})")

Batch Area of Interest Analysis

# Analyze multiple images for their areas of interest
image_urls = [
    "https://example.com/portrait1.jpg",
    "https://example.com/landscape1.jpg",
    "https://example.com/group-photo1.jpg"
]

aoi_data = []

for i, url in enumerate(image_urls, 1):
    try:
        print(f"Processing image {i}/{len(image_urls)}...")
        
        aoi_result = client.get_area_of_interest(url)
        area_rect = aoi_result.area_of_interest
        
        # Calculate statistics
        area_size = area_rect.w * area_rect.h
        center_x = area_rect.x + area_rect.w // 2
        center_y = area_rect.y + area_rect.h // 2
        
        aoi_info = {
            'url': url,
            'area_rect': area_rect,
            'area_size': area_size,
            'center': (center_x, center_y),
            'aspect_ratio': area_rect.w / area_rect.h if area_rect.h > 0 else 0
        }
        
        aoi_data.append(aoi_info)
        
        print(f"  Area: {area_rect.w}x{area_rect.h} at ({area_rect.x}, {area_rect.y})")
        print(f"  Center: ({center_x}, {center_y})")
        print(f"  Aspect ratio: {aoi_info['aspect_ratio']:.2f}")
        
    except Exception as e:
        print(f"  Error: {e}")

# Analyze batch results
if aoi_data:
    avg_aspect_ratio = sum(info['aspect_ratio'] for info in aoi_data) / len(aoi_data)
    total_area = sum(info['area_size'] for info in aoi_data)
    
    print(f"\nBatch Analysis Summary:")
    print(f"  Images processed: {len(aoi_data)}")
    print(f"  Average aspect ratio: {avg_aspect_ratio:.2f}")
    print(f"  Total area of interest: {total_area:,} pixels")

Local File Area of Interest

# Get area of interest from local image file
with open("family_photo.jpg", "rb") as image_stream:
    aoi_result = client.get_area_of_interest_in_stream(image_stream)
    area_rect = aoi_result.area_of_interest
    
    print(f"Local image area of interest:")
    print(f"  Position: ({area_rect.x}, {area_rect.y})")
    print(f"  Dimensions: {area_rect.w} x {area_rect.h}")
    
    # Check if the area of interest covers most of the image
    if aoi_result.metadata:
        coverage = (area_rect.w * area_rect.h) / (aoi_result.metadata.width * aoi_result.metadata.height)
        
        if coverage > 0.8:
            print("  Analysis: Most of the image is important content")
        elif coverage > 0.5:
            print("  Analysis: Moderate crop potential")
        else:
            print("  Analysis: Significant cropping opportunity")

Area of Interest for Social Media

# Generate social media crops using area of interest
image_url = "https://example.com/event-photo.jpg"
aoi_result = client.get_area_of_interest(image_url)
area_rect = aoi_result.area_of_interest

# Social media platform specifications
social_specs = {
    'instagram_square': (1080, 1080),
    'instagram_portrait': (1080, 1350),
    'facebook_cover': (1200, 630),
    'twitter_header': (1500, 500),
    'linkedin_post': (1200, 627)
}

print(f"Area of interest: {area_rect.w}x{area_rect.h}")
print(f"Social media crop recommendations:")

for platform, (target_w, target_h) in social_specs.items():
    target_ratio = target_w / target_h
    aoi_ratio = area_rect.w / area_rect.h
    
    if abs(aoi_ratio - target_ratio) < 0.1:
        fit_quality = "Perfect fit"
    elif abs(aoi_ratio - target_ratio) < 0.3:
        fit_quality = "Good fit"
    else:
        fit_quality = "Requires significant cropping"
    
    print(f"  {platform}: {fit_quality} (target: {target_ratio:.2f}, AOI: {aoi_ratio:.2f})")

Comparison with Face Detection

# Compare area of interest with face detection for portrait optimization
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes

image_url = "https://example.com/portrait.jpg"

# Get area of interest
aoi_result = client.get_area_of_interest(image_url)
area_rect = aoi_result.area_of_interest

# Get face detection results
analysis = client.analyze_image(image_url, visual_features=[VisualFeatureTypes.faces])

print(f"Area of Interest: ({area_rect.x}, {area_rect.y}) {area_rect.w}x{area_rect.h}")

if analysis.faces:
    print(f"Detected {len(analysis.faces)} face(s):")
    
    for i, face in enumerate(analysis.faces):
        face_rect = face.face_rectangle
        print(f"  Face {i+1}: ({face_rect.left}, {face_rect.top}) {face_rect.width}x{face_rect.height}")
        
        # Check if face is within area of interest
        face_center_x = face_rect.left + face_rect.width // 2
        face_center_y = face_rect.top + face_rect.height // 2
        
        in_aoi_x = area_rect.x <= face_center_x <= area_rect.x + area_rect.w
        in_aoi_y = area_rect.y <= face_center_y <= area_rect.y + area_rect.h
        
        if in_aoi_x and in_aoi_y:
            print(f"    Face {i+1} is within area of interest")
        else:
            print(f"    Face {i+1} is outside area of interest")
else:
    print("No faces detected")

Response Data Types

AreaOfInterestResult

class AreaOfInterestResult:
    """
    Area of interest detection result.
    
    Attributes:
        area_of_interest (BoundingRect): Bounding rectangle of the most important image area
        request_id (str): Request identifier
        metadata (ImageMetadata): Image metadata (dimensions, format)
        model_version (str): AI model version used for detection
    """

BoundingRect

class BoundingRect:
    """
    Rectangular bounding box coordinates.
    
    Attributes:
        x (int): Left coordinate (pixels from left edge)
        y (int): Top coordinate (pixels from top edge)
        w (int): Rectangle width in pixels
        h (int): Rectangle height in pixels
    """

ImageMetadata

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

Algorithm Details

How Area of Interest Works

The area of interest algorithm analyzes multiple visual factors:

  1. Visual Saliency: Identifies regions that naturally draw human attention
  2. Object Detection: Considers important objects and their prominence
  3. Face Detection: Prioritizes human faces in the composition
  4. Composition Analysis: Applies photographic rules like rule of thirds
  5. Edge Detection: Considers high-contrast boundaries and visual structure
  6. Color Analysis: Factors in color distribution and contrast

Use Cases

Content Management:

  • Automatic thumbnail generation for articles and galleries
  • Smart cropping for responsive web design
  • Social media content optimization

E-commerce:

  • Product image cropping for catalogs
  • Mobile-optimized product views
  • Promotional banner creation

Photography and Media:

  • Batch photo processing for different formats
  • Portrait optimization for profile pictures
  • Landscape cropping for various aspect ratios

Best Practices

  • Use area of interest as a starting point for manual fine-tuning
  • Consider combining with face detection for portrait images
  • Test results across different aspect ratios for your use case
  • Apply additional padding around the area of interest for safety margins
  • Validate results against your specific content and quality requirements

Install with Tessl CLI

npx tessl i tessl/pypi-azure-cognitiveservices-vision-computervision

docs

area-of-interest.md

domain-analysis.md

image-analysis.md

image-description.md

image-tagging.md

index.md

object-detection.md

ocr-text-recognition.md

thumbnail-generation.md

tile.json