azure-ai-vision-imageanalysis-java について
Azure AI Visionで画像を分析するJavaアプリケーション向けのスキルです。同期・非同期クライアントと複数の画像分析機能を扱います。
主な機能
- 01画像キャプションと詳細キャプション
- 02OCRによるテキスト抽出
- 03物体・人物・タグの検出
- 04スマートクロップ領域の取得
- 05同期・非同期クライアント
活用シーン
- 01ファイルやURLの画像へのキャプション生成
- 02ドキュメント画像からのテキスト抽出
- 03画像内の物体や人物の位置特定
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ sickn33/ agentic-awesome-skills --skill azure-ai-vision-imageanalysis-javaインストール後、Claude Code で/azure-ai-vision-imageanalysis-javaを入力して読み込まれたか確認します。
詳細
Java 用 Azure AI Vision Image Analysis SDK
Java 用 Azure AI Vision Image Analysis SDK を使用して、画像分析アプリケーションを構築します。
インストール
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-vision-imageanalysis</artifactId>
<version>1.1.0-beta.1</version>
</dependency>
クライアントの作成
API キーを使用する場合
import com.azure.ai.vision.imageanalysis.ImageAnalysisClient;
import com.azure.ai.vision.imageanalysis.ImageAnalysisClientBuilder;
import com.azure.core.credential.KeyCredential;
String endpoint = System.getenv("VISION_ENDPOINT");
String key = System.getenv("VISION_KEY");
ImageAnalysisClient client = new ImageAnalysisClientBuilder()
.endpoint(endpoint)
.credential(new KeyCredential(key))
.buildClient();
非同期クライアント
import com.azure.ai.vision.imageanalysis.ImageAnalysisAsyncClient;
ImageAnalysisAsyncClient asyncClient = new ImageAnalysisClientBuilder()
.endpoint(endpoint)
.credential(new KeyCredential(key))
.buildAsyncClient();
DefaultAzureCredential を使用する場合
import com.azure.identity.DefaultAzureCredentialBuilder;
ImageAnalysisClient client = new ImageAnalysisClientBuilder()
.endpoint(endpoint)
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
視覚機能
| 機能 | 説明 |
|---|---|
CAPTION | 人が読める画像の説明を生成します |
DENSE_CAPTIONS | 最大 10 個の領域のキャプションを生成します |
READ | OCR - 画像からテキストを抽出します |
TAGS | オブジェクト、シーン、動作のコンテンツタグを生成します |
OBJECTS | バウンディングボックス付きでオブジェクトを検出します |
SMART_CROPS | スマートなサムネイル領域を生成します |
PEOPLE | 人物とその位置を検出します |
基本パターン
キャプションの生成
import com.azure.ai.vision.imageanalysis.models.*;
import com.azure.core.util.BinaryData;
import java.io.File;
import java.util.Arrays;
// From file
BinaryData imageData = BinaryData.fromFile(new File("image.jpg").toPath());
ImageAnalysisResult result = client.analyze(
imageData,
Arrays.asList(VisualFeatures.CAPTION),
new ImageAnalysisOptions().setGenderNeutralCaption(true));
System.out.printf("Caption: \"%s\" (confidence: %.4f)%n",
result.getCaption().getText(),
result.getCaption().getConfidence());
URL からキャプションを生成
ImageAnalysisResult result = client.analyzeFromUrl(
"https://example.com/image.jpg",
Arrays.asList(VisualFeatures.CAPTION),
new ImageAnalysisOptions().setGenderNeutralCaption(true));
System.out.printf("Caption: \"%s\"%n", result.getCaption().getText());
テキストの抽出(OCR)
ImageAnalysisResult result = client.analyze(
BinaryData.fromFile(new File("document.jpg").toPath()),
Arrays.asList(VisualFeatures.READ),
null);
for (DetectedTextBlock block : result.getRead().getBlocks()) {
for (DetectedTextLine line : block.getLines()) {
System.out.printf("Line: '%s'%n", line.getText());
System.out.printf(" Bounding polygon: %s%n", line.getBoundingPolygon());
for (DetectedTextWord word : line.getWords()) {
System.out.printf(" Word: '%s' (confidence: %.4f)%n",
word.getText(),
word.getConfidence());
}
}
}
オブジェクトの検出
ImageAnalysisResult result = client.analyzeFromUrl(
imageUrl,
Arrays.asList(VisualFeatures.OBJECTS),
null);
for (DetectedObject obj : result.getObjects().getValues()) {
System.out.printf("Object: %s (confidence: %.4f)%n",
obj.getTags().get(0).getName(),
obj.getTags().get(0).getConfidence());
ImageBoundingBox box = obj.getBoundingBox();
System.out.printf(" Location: x=%d, y=%d, w=%d, h=%d%n",
box.getX(), box.getY(), box.getWidth(), box.getHeight());
}
タグの取得
ImageAnalysisResult result = client.analyzeFromUrl(
imageUrl,
Arrays.asList(VisualFeatures.TAGS),
null);
for (DetectedTag tag : result.getTags().getValues()) {
System.out.printf("Tag: %s (confidence: %.4f)%n",
tag.getName(),
tag.getConfidence());
}
人物の検出
ImageAnalysisResult result = client.analyzeFromUrl(
imageUrl,
Arrays.asList(VisualFeatures.PEOPLE),
null);
for (DetectedPerson person : result.getPeople().getValues()) {
ImageBoundingBox box = person.getBoundingBox();
System.out.printf("Person at x=%d, y=%d (confidence: %.4f)%n",
box.getX(), box.getY(), person.getConfidence());
}
スマートクロッピング
ImageAnalysisResult result = client.analyzeFromUrl(
imageUrl,
Arrays.asList(VisualFeatures.SMART_CROPS),
new ImageAnalysisOptions().setSmartCropsAspectRatios(Arrays.asList(1.0, 1.5)));
for (CropRegion crop : result.getSmartCrops().getValues()) {
System.out.printf("Crop region: aspect=%.2f, x=%d, y=%d, w=%d, h=%d%n",
crop.getAspectRatio(),
crop.getBoundingBox().getX(),
crop.getBoundingBox().getY(),
crop.getBoundingBox().getWidth(),
crop.getBoundingBox().getHeight());
}
詳細なキャプション
ImageAnalysisResult result = client.analyzeFromUrl(
imageUrl,
Arrays.asList(VisualFeatures.DENSE_CAPTIONS),
new ImageAnalysisOptions().setGenderNeutralCaption(true));
for (DenseCaption caption : result.getDenseCaptions().getValues()) {
System.out.printf("Caption: \"%s\" (confidence: %.4f)%n",
caption.getText(),
caption.getConfidence());
System.out.printf(" Region: x=%d, y=%d, w=%d, h=%d%n",
caption.getBoundingBox().getX(),
caption.getBoundingBox().getY(),
caption.getBoundingBox().getWidth(),
caption.getBoundingBox().getHeight());
}
複数の機能
ImageAnalysisResult result = client.analyzeFromUrl(
imageUrl,
Arrays.asList(
VisualFeatures.CAPTION,
VisualFeatures.TAGS,
VisualFeatures.OBJECTS,
VisualFeatures.READ),
new ImageAnalysisOptions()
.setGenderNeutralCaption(true)
.setLanguage("en"));
// Access all results
System.out.println("Caption: " + result.getCaption().getText());
System.out.println("Tags: " + result.getTags().getValues().size());
System.out.println("Objects: " + result.getObjects().getValues().size());
System.out.println("Text blocks: " + result.getRead().getBlocks().size());
非同期分析
asyncClient.analyzeFromUrl(
imageUrl,
Arrays.asList(VisualFeatures.CAPTION),
null)
.subscribe(
result -> System.out.println("Caption: " + result.getCaption().getText()),
error -> System.err.println("Error: " + error.getMessage()),
() -> System.out.println("Complete")
);
エラー処理
import com.azure.core.exception.HttpResponseException;
try {
client.analyzeFromUrl(imageUrl, Arrays.asList(VisualFeatures.CAPTION), null);
} catch (HttpResponseException e) {
System.out.println("Status: " + e.getResponse().getStatusCode());
System.out.println("Error: " + e.getMessage());
}
環境変数
VISION_ENDPOINT=https://<resource>.cognitiveservices.azure.com/
VISION_KEY=<your-api-key>
画像の要件
- 形式:JPEG、PNG、GIF、BMP、WEBP、ICO、TIFF、MPO
- サイズ:< 20 MB
- 寸法:50x50~16000x16000 ピクセル
リージョンの利用可否
Caption と Dense Captions には GPU 対応リージョンが必要です。デプロイ前に対応リージョンを確認してください。
トリガーフレーズ
- 「Java 画像分析」
- 「Azure Vision SDK」
- 「画像のキャプション生成」
- 「OCR による画像テキスト抽出」
- 「画像のオブジェクト検出」
- 「スマートクロップのサムネイル」
- 「画像内の人物を検出」
使用する場合
このスキルは、概要で説明されているワークフローまたはアクションを実行する際に適用できます。
制限事項
- タスクが上記の範囲に明確に合致する場合にのみ、このスキルを使用してください。
- 出力を、環境固有の検証、テスト、または専門家によるレビューの代わりとして扱わないでください。
- 必要な入力、権限、安全上の境界、または成功基準が不足している場合は、停止して確認を求めてください。
要求されていない場合、機能結果のラッパーが存在しないことがあります。要求した各結果を null チェックし、get(0) でインデックスを指定する前に、検出されたオブジェクトにタグがあることを確認してください。固定された SDK でモデル/機能の利用可否とシグネチャを確認してください。スコア、キャプション、OCR は不確実なモデル出力であり、本人確認や事実の証明にはなりません。非公開画像をクラウドに転送することを承認し、出力テキストを保護してください。ImageAnalysisResult APIを参照してください。
コメント
Markdown 対応