mcpField
A
azure-ai-vision-imageanalysis-py

azure-ai-vision-imageanalysis-py

github.com/sickn33数据与数据库48.7k91 次安装今天更新
代码仓库

关于 azure-ai-vision-imageanalysis-py

供 Python 开发者使用的 Azure AI Vision 4.0 图像分析 SDK,可从图像文件或 URL 获取描述、标签、对象和 OCR 等结果。

主要功能

  1. 01生成图像描述
  2. 02识别标签与对象位置
  3. 03提取图像文字
  4. 04检测人物区域
  5. 05建议智能裁剪区域
  6. 06支持同步与异步客户端

适用场景

  1. 01为图像生成说明文字
  2. 02提取图片中的文字
  3. 03检测对象并获取边界框
  4. 04为缩略图选择裁剪区域

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/sickn33/agentic-awesome-skills --skill azure-ai-vision-imageanalysis-py

安装后在 Claude Code 中输入/azure-ai-vision-imageanalysis-py确认已加载。

详细介绍

Azure AI Vision Image Analysis SDK for Python

用于 Azure AI Vision 4.0 图像分析的客户端库,支持图像描述、标签、对象、OCR 等功能。

安装

pip install azure-ai-vision-imageanalysis

环境变量

VISION_ENDPOINT=https://<resource>.cognitiveservices.azure.com
VISION_KEY=<your-api-key>  # If using API key

身份验证

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 发送给 Azure。请确认相关权利和隐私范围、区域、服务可用性及当前支持的功能。OCR 和图像描述可能有误;人员检测不会识别具体人员,也无法确定年龄、身份、是否同意或敏感特征。用于可能产生重大影响的场景前,请先审核输出,并避免记录私密的 OCR 文本。

从 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}")
        print(f"  Confidence: {obj.tags[0].confidence:.2f}")
        box = obj.bounding_box
        print(f"  Bounding box: x={box.x}, y={box.y}, w={box.width}, h={box.height}")

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}")
            print(f"  Bounding polygon: {line.bounding_polygon}")
            
            # Word-level details
            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 detected:")
        print(f"  Confidence: {person.confidence:.2f}")
        box = person.bounding_box
        print(f"  Bounding box: x={box.x}, y={box.y}, w={box.width}, h={box.height}")

智能裁剪

result = client.analyze_from_url(
    image_url=image_url,
    visual_features=[VisualFeatures.SMART_CROPS],
    smart_crops_aspect_ratios=[0.9, 1.33, 1.78]  # Portrait, 4:3, 16:9
)

if result.smart_crops:
    for crop in result.smart_crops.list:
        print(f"Aspect ratio: {crop.aspect_ratio}")
        box = crop.bounding_box
        print(f"  Crop region: x={box.x}, y={box.y}, w={box.width}, h={box.height}")

异步客户端

from azure.ai.vision.imageanalysis.aio import ImageAnalysisClient
from azure.identity.aio import DefaultAzureCredential

async def analyze_image():
    async with ImageAnalysisClient(
        endpoint=endpoint,
        credential=DefaultAzureCredential()
    ) as client:
        result = await client.analyze_from_url(
            image_url=image_url,
            visual_features=[VisualFeatures.CAPTION]
        )
        if result.caption:
            print(result.caption.text)

视觉功能

功能说明
CAPTION用一句话描述图像
DENSE_CAPTIONS为多个区域生成描述
TAGS内容标签(对象、场景、动作)
OBJECTS使用边界框进行对象检测
READOCR 文本提取
PEOPLE使用边界框进行人员检测
SMART_CROPS为缩略图建议裁剪区域

错误处理

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"Reason: {e.reason}")
    print(f"Message: {e.error.message}")

图像要求

  • 格式:JPEG、PNG、GIF、BMP、WEBP、ICO、TIFF、MPO
  • 最大尺寸:20 MB
  • 尺寸:50x50 至 16000x16000 像素

最佳实践

  1. 仅选择所需功能,以优化延迟和成本
  2. 在高吞吐量场景中使用异步客户端
  3. 针对无效图像或身份验证问题处理 HttpResponseError
  4. 启用 gender_neutral_caption,生成包容性描述
  5. 指定语言,以生成本地化描述
  6. 设置 smart_crops_aspect_ratios,使其符合缩略图要求
  7. 缓存结果,避免多次分析同一图像

适用场景

当任务涉及执行概述中描述的工作流或操作时,可使用此技能。

限制

  • 仅当任务明确符合上述范围时,才使用此技能。
  • 不要将输出视为针对特定环境进行验证、测试或专家审查的替代品。
  • 如果缺少必要输入、权限、安全边界或成功标准,请暂停并请求澄清。

评论

支持 Markdown