mcpField
A
azure-ai-vision-imageanalysis-py

azure-ai-vision-imageanalysis-py

github.com/sickn33データ・データベース48.7k91 回インストール今日更新
リポジトリ

azure-ai-vision-imageanalysis-py について

Azure AI Vision 4.0の画像分析を利用するPython SDKです。画像ファイルやURLを対象に、キャプション、タグ、オブジェクト、OCRなどを取得できます。

主な機能

  1. 01画像キャプションの生成
  2. 02タグ付けとオブジェクト位置の検出
  3. 03OCRによる文字抽出
  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

キャプション、タグ、オブジェクト、OCR などに対応した、Azure AI Vision 4.0 の画像分析用クライアントライブラリです。

インストール

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画像を説明する 1 文のキャプション
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. 同じ画像を複数回分析する場合は、結果をキャッシュする

使用する場面

このスキルは、概要に記載されたワークフローまたはアクションを実行する場合に適用できます。

制限事項

  • このスキルは、タスクが上記の説明の範囲に明確に該当する場合にのみ使用してください。
  • 出力を、環境固有の検証、テスト、または専門家によるレビューの代わりとして扱わないでください。
  • 必要な入力、権限、安全上の境界、または成功基準が不足している場合は、作業を中止して確認を求めてください。

コメント