次の方法で共有


イメージの説明

Important

現在、画像説明は中国で利用できません。

Image Description API を使用して、イメージのさまざまな種類のテキスト説明を生成できます。

API の詳細については、AI イメージング機能の API リファレンスを参照してください。

コンテンツ モデレーションの詳細については、「生成 AI API を使用したコンテンツの安全性」を参照してください。

Important

パッケージ マニフェストの要件: Windows AI イメージング API を使用するには、systemAIModelsで宣言されたPackage.appxmanifest機能を備えた MSIX パッケージとしてアプリをパッケージ化する必要があります。 さらに、マニフェストの MaxVersionTested 属性が最新の Windows バージョン ( 10.0.26226.0 以降など) に設定されていることを確認して、Windows AI 機能を適切にサポートします。 古い値を使用すると、モデルの読み込み時に "アプリによって宣言されていません" エラーが発生する可能性があります。

<Dependencies>
  <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.17763.0" MaxVersionTested="10.0.26226.0" />
  <TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.17763.0" MaxVersionTested="10.0.26226.0" />
</Dependencies>

説明の種類

次の種類のテキスト説明がサポートされています。

  • 簡単 - グラフや図に適した説明を提供します。
  • 詳細 - 長い説明を提供します。
  • - 画像のキャプションに適した簡単な説明を提供します。 指定がない場合は、既定値が使用されます。
  • アクセシビリティ - ユーザー補助のニーズを持つユーザー向けの詳細について、長い説明を提供します。

制限事項

これらの API は Machine Learning (ML) モデルを使用するため、テキストが画像を正しく説明していない場合、エラーが発生することがあります。 そのため、以下のシナリオにおける画像には、これらの API の使用はお勧めできません。

  • 画像にセンシティブな内容 (国旗、地図、地球儀、文化的シンボル、宗教的シンボルなど) が含まれる場合。不正確な説明により、論争を招く可能性があります。
  • 医療に関するアドバイスや診断、法的コンテンツ、財務文書など、正確な説明が不可欠な場合。

画像の説明の例

次の例は、指定した説明の種類 (省略可能) とコンテンツ モデレーションのレベル (省略可能) に基づいて画像のテキストの説明を取得する方法を示しています。

SoftwareBitmap は現在サポートされていないため、イメージは ImageBuffer オブジェクトである必要があります (この例では、SoftwareBitmapImageBuffer に変換する方法を示します)。

  1. GetReadyState メソッドを呼び出し、EnsureReadyAsync メソッドが正常に返されるのを待って、イメージ記述モデルを使用できることを確認します。

  2. イメージ記述モデルが使用可能になったら、 ImageDescriptionGenerator オブジェクトを作成して参照します。

  3. (省略可能) ContentFilterOptions オブジェクトを作成し、任意の値を指定します。 既定値を使用する場合は、null オブジェクトを渡すことができます。

  4. 元のイメージ、ImageDescriptionKind (優先する説明の種類の省略可能な値)、および ContentFilterOptions オブジェクト (省略可能) を指定する DescribeAsync メソッドを呼び出して、イメージの説明 (LanguageModelResponse.Response) を取得します。

using Microsoft.Graphics.Imaging;
using Microsoft.Windows.Management.Deployment;  
using Microsoft.Windows.AI;
using Microsoft.Windows.AI.ContentModeration;
using Windows.Storage.StorageFile;  
using Windows.Storage.Streams;  
using Windows.Graphics.Imaging;

if (ImageDescriptionGenerator.GetReadyState() == AIFeatureReadyState.NotReady) 
{
    var result = await ImageDescriptionGenerator.EnsureReadyAsync();
    if (result.Status != AIFeatureReadyResultState.Success)
    {
        throw result.ExtendedError;
    }
}

ImageDescriptionGenerator imageDescriptionGenerator = await ImageDescriptionGenerator.CreateAsync();

// Convert already available softwareBitmap to ImageBuffer.
ImageBuffer inputImage = ImageBuffer.CreateCopyFromBitmap(softwareBitmap);  

// Create content moderation thresholds object.
ContentFilterOptions filterOptions = new ContentFilterOptions();
filterOptions.PromptMinSeverityLevelToBlock.ViolentContentSeverity = SeverityLevel.Medium;
filterOptions.ResponseMinSeverityLevelToBlock.ViolentContentSeverity = SeverityLevel.Medium;

// Get text description.
LanguageModelResponse languageModelResponse = await imageDescriptionGenerator.DescribeAsync(inputImage, ImageDescriptionScenario.Caption, filterOptions);
string response = languageModelResponse.Response;

#include <winrt/Microsoft.Graphics.Imaging.h>
#include <winrt/Microsoft.Windows.AI.Imaging.h>
#include <winrt/Microsoft.Windows.AI.ContentSafety.h>
#include <winrt/Microsoft.Windows.AI.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Graphics.Imaging.h> 
#include <winrt/Windows.Storage.Streams.h>
#include <winrt/Windows.Storage.StorageFile.h>

using namespace winrt::Microsoft::Graphics::Imaging; 
using namespace winrt::Microsoft::Windows::AI;
using namespace winrt::Microsoft::Windows::AI::ContentSafety; 
using namespace winrt::Microsoft::Windows::AI::Imaging; 
using namespace winrt::Windows::Foundation; 
using namespace winrt::Windows::Graphics::Imaging;
using namespace winrt::Windows::Storage::Streams;
using namespace winrt::Windows::Storage::StorageFile;    

if (ImageDescriptionGenerator::GetReadyState() == AIFeatureReadyState::NotReady)
{
    auto loadResult = ImageDescriptionGenerator::EnsureReadyAsync().get();

    if (loadResult.Status() != AIFeatureReadyResultState::Success)
    {
        throw winrt::hresult_error(loadResult.ExtendedError());
    }
}

ImageDescriptionGenerator imageDescriptionGenerator = 
    ImageDescriptionGenerator::CreateAsync().get();

// Convert already available softwareBitmap to ImageBuffer.
auto inputBuffer = Microsoft::Graphics::Imaging::ImageBuffer::CreateForSoftwareBitmap(softwareBitmap);

// Create content moderation thresholds object.

ContentFilterOptions contentFilter{};
contentFilter.PromptMaxAllowedSeverityLevel().Violent(SeverityLevel::Medium);
contentFilter.ResponseMaxAllowedSeverityLevel().Violent(SeverityLevel::Medium);

// Get text description.
auto response = imageDescriptionGenerator.DescribeAsync(inputBuffer, ImageDescriptionKind::BriefDescription, contentFilter).get();
string text = response.Description();

こちらも参照ください