0
# Area of Interest
1
2
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.
3
4
## Capabilities
5
6
### Area of Interest Detection
7
8
Analyze images to find the rectangular region that contains the most important visual content for cropping purposes.
9
10
```python { .api }
11
def get_area_of_interest(url, model_version="latest", custom_headers=None, raw=False, **operation_config):
12
"""
13
Get area of interest in image for optimal cropping.
14
15
Args:
16
url (str): Publicly reachable URL of an image
17
model_version (str, optional): AI model version. Default: "latest"
18
custom_headers (dict, optional): Custom HTTP headers
19
raw (bool, optional): Return raw response. Default: False
20
21
Returns:
22
AreaOfInterestResult: Bounding rectangle of the most important image area
23
24
Raises:
25
ComputerVisionErrorResponseException: API error occurred
26
27
Note:
28
The area of interest is returned as a bounding rectangle that can be used
29
for intelligent cropping while preserving important visual content.
30
"""
31
32
def get_area_of_interest_in_stream(image, model_version="latest", custom_headers=None, raw=False, **operation_config):
33
"""
34
Get area of interest from binary image stream.
35
36
Args:
37
image (Generator): Binary image data stream
38
model_version (str, optional): AI model version
39
40
Returns:
41
AreaOfInterestResult: Area of interest bounding rectangle
42
"""
43
```
44
45
## Usage Examples
46
47
### Basic Area of Interest Detection
48
49
```python
50
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
51
from msrest.authentication import CognitiveServicesCredentials
52
53
# Initialize client
54
credentials = CognitiveServicesCredentials("your-api-key")
55
client = ComputerVisionClient("https://your-endpoint.cognitiveservices.azure.com/", credentials)
56
57
# Get area of interest
58
image_url = "https://example.com/landscape-photo.jpg"
59
aoi_result = client.get_area_of_interest(image_url)
60
61
# Extract bounding rectangle
62
area_rect = aoi_result.area_of_interest
63
64
print(f"Area of Interest:")
65
print(f" Position: ({area_rect.x}, {area_rect.y})")
66
print(f" Size: {area_rect.w} x {area_rect.h} pixels")
67
68
# Calculate area coverage
69
if aoi_result.metadata:
70
image_width = aoi_result.metadata.width
71
image_height = aoi_result.metadata.height
72
73
total_area = image_width * image_height
74
aoi_area = area_rect.w * area_rect.h
75
coverage_percent = (aoi_area / total_area) * 100
76
77
print(f" Coverage: {coverage_percent:.1f}% of total image")
78
```
79
80
### Smart Cropping Using Area of Interest
81
82
```python
83
from PIL import Image
84
import requests
85
from io import BytesIO
86
87
# Get area of interest
88
image_url = "https://example.com/group-photo.jpg"
89
aoi_result = client.get_area_of_interest(image_url)
90
area_rect = aoi_result.area_of_interest
91
92
# Download and crop the original image
93
response = requests.get(image_url)
94
original_image = Image.open(BytesIO(response.content))
95
96
# Crop to area of interest
97
cropped_image = original_image.crop((
98
area_rect.x, # left
99
area_rect.y, # top
100
area_rect.x + area_rect.w, # right
101
area_rect.y + area_rect.h # bottom
102
))
103
104
# Save cropped image
105
cropped_image.save("smart_cropped_image.jpg")
106
107
print(f"Original size: {original_image.size}")
108
print(f"Cropped size: {cropped_image.size}")
109
print(f"Area of interest: ({area_rect.x}, {area_rect.y}, {area_rect.w}, {area_rect.h})")
110
```
111
112
### Multiple Aspect Ratio Cropping
113
114
```python
115
# Use area of interest to generate crops for different aspect ratios
116
aoi_result = client.get_area_of_interest(image_url)
117
area_rect = aoi_result.area_of_interest
118
119
# Define target aspect ratios
120
target_ratios = {
121
'square': 1.0, # 1:1
122
'landscape': 1.5, # 3:2
123
'portrait': 0.67, # 2:3
124
'wide': 2.0 # 2:1
125
}
126
127
print(f"Original area of interest: {area_rect.w}x{area_rect.h}")
128
129
for name, ratio in target_ratios.items():
130
# Calculate dimensions maintaining the aspect ratio
131
if area_rect.w / area_rect.h > ratio:
132
# Width is too large, constrain by height
133
new_height = area_rect.h
134
new_width = int(new_height * ratio)
135
else:
136
# Height is too large, constrain by width
137
new_width = area_rect.w
138
new_height = int(new_width / ratio)
139
140
# Center the crop within the area of interest
141
x_offset = (area_rect.w - new_width) // 2
142
y_offset = (area_rect.h - new_height) // 2
143
144
crop_x = area_rect.x + x_offset
145
crop_y = area_rect.y + y_offset
146
147
print(f"{name} crop ({ratio:.2f}): {new_width}x{new_height} at ({crop_x}, {crop_y})")
148
```
149
150
### Batch Area of Interest Analysis
151
152
```python
153
# Analyze multiple images for their areas of interest
154
image_urls = [
155
"https://example.com/portrait1.jpg",
156
"https://example.com/landscape1.jpg",
157
"https://example.com/group-photo1.jpg"
158
]
159
160
aoi_data = []
161
162
for i, url in enumerate(image_urls, 1):
163
try:
164
print(f"Processing image {i}/{len(image_urls)}...")
165
166
aoi_result = client.get_area_of_interest(url)
167
area_rect = aoi_result.area_of_interest
168
169
# Calculate statistics
170
area_size = area_rect.w * area_rect.h
171
center_x = area_rect.x + area_rect.w // 2
172
center_y = area_rect.y + area_rect.h // 2
173
174
aoi_info = {
175
'url': url,
176
'area_rect': area_rect,
177
'area_size': area_size,
178
'center': (center_x, center_y),
179
'aspect_ratio': area_rect.w / area_rect.h if area_rect.h > 0 else 0
180
}
181
182
aoi_data.append(aoi_info)
183
184
print(f" Area: {area_rect.w}x{area_rect.h} at ({area_rect.x}, {area_rect.y})")
185
print(f" Center: ({center_x}, {center_y})")
186
print(f" Aspect ratio: {aoi_info['aspect_ratio']:.2f}")
187
188
except Exception as e:
189
print(f" Error: {e}")
190
191
# Analyze batch results
192
if aoi_data:
193
avg_aspect_ratio = sum(info['aspect_ratio'] for info in aoi_data) / len(aoi_data)
194
total_area = sum(info['area_size'] for info in aoi_data)
195
196
print(f"\nBatch Analysis Summary:")
197
print(f" Images processed: {len(aoi_data)}")
198
print(f" Average aspect ratio: {avg_aspect_ratio:.2f}")
199
print(f" Total area of interest: {total_area:,} pixels")
200
```
201
202
### Local File Area of Interest
203
204
```python
205
# Get area of interest from local image file
206
with open("family_photo.jpg", "rb") as image_stream:
207
aoi_result = client.get_area_of_interest_in_stream(image_stream)
208
area_rect = aoi_result.area_of_interest
209
210
print(f"Local image area of interest:")
211
print(f" Position: ({area_rect.x}, {area_rect.y})")
212
print(f" Dimensions: {area_rect.w} x {area_rect.h}")
213
214
# Check if the area of interest covers most of the image
215
if aoi_result.metadata:
216
coverage = (area_rect.w * area_rect.h) / (aoi_result.metadata.width * aoi_result.metadata.height)
217
218
if coverage > 0.8:
219
print(" Analysis: Most of the image is important content")
220
elif coverage > 0.5:
221
print(" Analysis: Moderate crop potential")
222
else:
223
print(" Analysis: Significant cropping opportunity")
224
```
225
226
### Area of Interest for Social Media
227
228
```python
229
# Generate social media crops using area of interest
230
image_url = "https://example.com/event-photo.jpg"
231
aoi_result = client.get_area_of_interest(image_url)
232
area_rect = aoi_result.area_of_interest
233
234
# Social media platform specifications
235
social_specs = {
236
'instagram_square': (1080, 1080),
237
'instagram_portrait': (1080, 1350),
238
'facebook_cover': (1200, 630),
239
'twitter_header': (1500, 500),
240
'linkedin_post': (1200, 627)
241
}
242
243
print(f"Area of interest: {area_rect.w}x{area_rect.h}")
244
print(f"Social media crop recommendations:")
245
246
for platform, (target_w, target_h) in social_specs.items():
247
target_ratio = target_w / target_h
248
aoi_ratio = area_rect.w / area_rect.h
249
250
if abs(aoi_ratio - target_ratio) < 0.1:
251
fit_quality = "Perfect fit"
252
elif abs(aoi_ratio - target_ratio) < 0.3:
253
fit_quality = "Good fit"
254
else:
255
fit_quality = "Requires significant cropping"
256
257
print(f" {platform}: {fit_quality} (target: {target_ratio:.2f}, AOI: {aoi_ratio:.2f})")
258
```
259
260
### Comparison with Face Detection
261
262
```python
263
# Compare area of interest with face detection for portrait optimization
264
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
265
266
image_url = "https://example.com/portrait.jpg"
267
268
# Get area of interest
269
aoi_result = client.get_area_of_interest(image_url)
270
area_rect = aoi_result.area_of_interest
271
272
# Get face detection results
273
analysis = client.analyze_image(image_url, visual_features=[VisualFeatureTypes.faces])
274
275
print(f"Area of Interest: ({area_rect.x}, {area_rect.y}) {area_rect.w}x{area_rect.h}")
276
277
if analysis.faces:
278
print(f"Detected {len(analysis.faces)} face(s):")
279
280
for i, face in enumerate(analysis.faces):
281
face_rect = face.face_rectangle
282
print(f" Face {i+1}: ({face_rect.left}, {face_rect.top}) {face_rect.width}x{face_rect.height}")
283
284
# Check if face is within area of interest
285
face_center_x = face_rect.left + face_rect.width // 2
286
face_center_y = face_rect.top + face_rect.height // 2
287
288
in_aoi_x = area_rect.x <= face_center_x <= area_rect.x + area_rect.w
289
in_aoi_y = area_rect.y <= face_center_y <= area_rect.y + area_rect.h
290
291
if in_aoi_x and in_aoi_y:
292
print(f" Face {i+1} is within area of interest")
293
else:
294
print(f" Face {i+1} is outside area of interest")
295
else:
296
print("No faces detected")
297
```
298
299
## Response Data Types
300
301
### AreaOfInterestResult
302
303
```python { .api }
304
class AreaOfInterestResult:
305
"""
306
Area of interest detection result.
307
308
Attributes:
309
area_of_interest (BoundingRect): Bounding rectangle of the most important image area
310
request_id (str): Request identifier
311
metadata (ImageMetadata): Image metadata (dimensions, format)
312
model_version (str): AI model version used for detection
313
"""
314
```
315
316
### BoundingRect
317
318
```python { .api }
319
class BoundingRect:
320
"""
321
Rectangular bounding box coordinates.
322
323
Attributes:
324
x (int): Left coordinate (pixels from left edge)
325
y (int): Top coordinate (pixels from top edge)
326
w (int): Rectangle width in pixels
327
h (int): Rectangle height in pixels
328
"""
329
```
330
331
### ImageMetadata
332
333
```python { .api }
334
class ImageMetadata:
335
"""
336
Image metadata information.
337
338
Attributes:
339
height (int): Image height in pixels
340
width (int): Image width in pixels
341
format (str): Image format (e.g., "Jpeg", "Png")
342
"""
343
```
344
345
## Algorithm Details
346
347
### How Area of Interest Works
348
349
The area of interest algorithm analyzes multiple visual factors:
350
351
1. **Visual Saliency**: Identifies regions that naturally draw human attention
352
2. **Object Detection**: Considers important objects and their prominence
353
3. **Face Detection**: Prioritizes human faces in the composition
354
4. **Composition Analysis**: Applies photographic rules like rule of thirds
355
5. **Edge Detection**: Considers high-contrast boundaries and visual structure
356
6. **Color Analysis**: Factors in color distribution and contrast
357
358
### Use Cases
359
360
**Content Management**:
361
- Automatic thumbnail generation for articles and galleries
362
- Smart cropping for responsive web design
363
- Social media content optimization
364
365
**E-commerce**:
366
- Product image cropping for catalogs
367
- Mobile-optimized product views
368
- Promotional banner creation
369
370
**Photography and Media**:
371
- Batch photo processing for different formats
372
- Portrait optimization for profile pictures
373
- Landscape cropping for various aspect ratios
374
375
### Best Practices
376
377
- Use area of interest as a starting point for manual fine-tuning
378
- Consider combining with face detection for portrait images
379
- Test results across different aspect ratios for your use case
380
- Apply additional padding around the area of interest for safety margins
381
- Validate results against your specific content and quality requirements