
关于
Azure AI 视觉图像分析 SDK,支持图像描述、标签、物体检测、OCR、人物检测和智能裁剪。用于计算机视觉和图像理解任务。
name: azure-ai-vision-imageanalysis-py description: Azure AI Vision 图像分析 SDK,支持图像描述、标签、对象检测、OCR、人物检测和智能裁剪。适用于计算机视觉和图像理解任务。 risk: unknown source: community date_added: '2026-02-27'
Azure AI Vision 图像分析 Python SDK
用于 Azure AI Vision 4.0 图像分析的客户端库,包括图像描述、标签、对象检测、OCR 等功能。
安装
pip install azure-ai-vision-imageanalysis
环境变量
VISION_ENDPOINT=https://<resource>.cognitiveservices.azure.com
VISION_KEY=<your-api-key> # 如果使用 API 密钥
身份验证
API 密钥
import os
from azure.ai.vision.imageanalysis import ImageAnalysisClient
from azure.core.credentials import AzureKeyCredential
endpoint = os.environ["VISION_ENDPOINT"]
key = os.environ["VISION_KEY"]
client = ImageAnalysisClient(
endpoint=endpoint,
credential=AzureKeyCredential(key)
)
Entra ID(推荐)
from azure.ai.vision.imageanalysis import ImageAnalysisClient
from azure.identity import DefaultAzureCredential
client = ImageAnalysisClient(
endpoint=os.environ["VISION_ENDPOINT"],
credential=DefaultAzureCredential()
)
从 URL 分析图像
from azure.ai.vision.imageanalysis.models import VisualFeatures
image_url = "https://example.com/image.jpg"
result = client.analyze_from_url(
image_url=image_url,
visual_features=[
VisualFeatures.CAPTION,
VisualFeatures.TAGS,
VisualFeatures.OBJECTS,
VisualFeatures.READ,
VisualFeatures.PEOPLE,
VisualFeatures.SMART_CROPS,
VisualFeatures.DENSE_CAPTIONS
],
gender_neutral_caption=True,
language="en"
)
从文件分析图像
with open("image.jpg", "rb") as f:
image_data = f.read()
result = client.analyze(
image_data=image_data,
visual_features=[VisualFeatures.CAPTION, VisualFeatures.TAGS]
)
图像描述
result = client.analyze_from_url(
image_url=image_url,
visual_features=[VisualFeatures.CAPTION],
gender_neutral_caption=True
)
if result.caption:
print(f"Caption: {result.caption.text}")
print(f"Confidence: {result.caption.confidence:.2f}")
密集描述(多区域)
result = client.analyze_from_url(
image_url=image_url,
visual_features=[VisualFeatures.DENSE_CAPTIONS]
)
if result.dense_captions:
for caption in result.dense_captions.list:
print(f"Caption: {caption.text}")
print(f" Confidence: {caption.confidence:.2f}")
print(f" Bounding box: {caption.bounding_box}")
标签
result = client.analyze_from_url(
image_url=image_url,
visual_features=[VisualFeatures.TAGS]
)
if result.tags:
for tag in result.tags.list:
print(f"Tag: {tag.name} (confidence: {tag.confidence:.2f})")
对象检测
result = client.analyze_from_url(
image_url=image_url,
visual_features=[VisualFeatures.OBJECTS]
)
if result.objects:
for obj in result.objects.list:
print(f"Object: {obj.tags[0].name} (confidence: {obj.tags[0].confidence:.2f})")
print(f" Bounding box: {obj.bounding_box}")
OCR / 文本读取
result = client.analyze_from_url(
image_url=image_url,
visual_features=[VisualFeatures.READ]
)
if result.read:
for block in result.read.blocks:
for line in block.lines:
print(f"Line: {line.text}")
for word in line.words:
print(f" Word: {word.text} (confidence: {word.confidence:.2f})")
人物检测
result = client.analyze_from_url(
image_url=image_url,
visual_features=[VisualFeatures.PEOPLE]
)
if result.people:
for person in result.people.list:
print(f"Person: confidence={person.confidence:.2f}")
print(f" Bounding box: {person.bounding_box}")
智能裁剪
result = client.analyze_from_url(
image_url=image_url,
visual_features=[VisualFeatures.SMART_CROPS],
smart_crops_aspect_ratios=[0.9, 1.33]
)
if result.smart_crops:
for crop in result.smart_crops.list:
print(f"Aspect ratio: {crop.aspect_ratio}")
print(f" Bounding box: {crop.bounding_box}")
错误处理
from azure.core.exceptions import HttpResponseError
try:
result = client.analyze_from_url(
image_url=image_url,
visual_features=[VisualFeatures.CAPTION]
)
except HttpResponseError as e:
print(f"Status code: {e.status_code}")
print(f"Message: {e.message}")
最佳实践
- 使用 Entra ID 认证而非 API 密钥以提高安全性
- 仅请求所需的视觉特征以减少延迟
- 对于批量处理使用异步客户端
- 图像大小限制:20MB,最小 50x50 像素
- 支持格式:JPEG、PNG、GIF、BMP、TIFF、ICO、WEBP
兼容工具
Claude CodeCursor
标签
后端开发
