Windows AI API でサポートされる AI イメージング機能により、次の機能が有効になります。
- イメージのスーパー解像度: イメージのスケーリングとシャープニング。
- 画像の説明: 画像を説明するテキストを生成します。
- 画像のセグメント化: 画像内のオブジェクトを識別します。
- 画像の前景抽出: 入力イメージの前景を抽出する
- オブジェクトの消去: イメージからオブジェクトを削除します。
API の詳細については、AI イメージング機能の API リファレンスを参照してください。
コンテンツ モデレーションの詳細については、「生成 AI API を使用したコンテンツの安全性」を参照してください。
重要
パッケージ マニフェストの要件: 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>
画像の超解像でできること
Image Super Resolution API を使用すると、イメージのシャープ化とスケーリングが可能になります。
スケールファクターが高いほどアーティファクトが発生し、画像の精度が損なわれる可能性があるため、スケーリングは最大 8 倍に制限されます。 最終的な幅または高さが元の値の 8 倍を超える場合は、例外がスローされます。
画像の超解像度の例
次の例は、既存のソフトウェア ビットマップ イメージ (targetWidth) のスケール (targetHeight、softwareBitmap) を変更し、ImageScaler オブジェクトを使用してイメージのシャープネスを向上させる方法を示しています (イメージを拡大縮小せずにシャープネスを向上させるには、既存のイメージの幅と高さを指定するだけです)。
GetReadyState メソッドを呼び出し、EnsureReadyAsync メソッドが正常に返されるのを待って、イメージスーパー解像度モデルを使用できることを確認します。
イメージスーパー解像度モデルが使用可能になったら、 ImageScaler オブジェクトを作成して参照します。
ScaleSoftwareBitmap メソッドを使用して、既存のイメージと目的の幅と高さをモデルに渡すことで、既存のイメージのシャープでスケーリングされたバージョンを取得します。
using Microsoft.Graphics.Imaging;
using Microsoft.Windows.Management.Deployment;
using Microsoft.Windows.AI;
using Windows.Graphics.Imaging;
if (ImageScaler.GetReadyState() == AIFeatureReadyState.NotReady)
{
var result = await ImageScaler.EnsureReadyAsync();
if (result.Status != AIFeatureReadyResultState.Success)
{
throw result.ExtendedError;
}
}
ImageScaler imageScaler = await ImageScaler.CreateAsync();
SoftwareBitmap finalImage = imageScaler.ScaleSoftwareBitmap(softwareBitmap, targetWidth, targetHeight);
#include <winrt/Microsoft.Graphics.Imaging.h>
#include <winrt/Microsoft.Windows.AI.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Graphics.Imaging.h>
using namespace winrt::Microsoft::Graphics::Imaging;
using namespace winrt::Microsoft::Windows::AI;
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Graphics::Imaging;
if (ImageScaler::GetReadyState() == AIFeatureReadyState::NotReady)
{
auto loadResult = ImageScaler::EnsureReadyAsync().get();
if (loadResult.Status() != AIFeatureReadyResultState::Success)
{
throw winrt::hresult_error(loadResult.ExtendedError());
}
}
int targetWidth = 100;
int targetHeight = 100;
ImageScaler imageScaler = ImageScaler::CreateAsync().get();
Windows::Graphics::Imaging::SoftwareBitmap finalImage =
imageScaler.ScaleSoftwareBitmap(softwareBitmap, targetWidth, targetHeight);
画像の説明文生成でできること
重要
現在、画像説明は中国で利用できません。
Image Description API は、画像のさまざまな種類のテキスト説明を生成する機能を提供します。
次の種類のテキスト説明がサポートされています。
- 簡単 - グラフや図に適した説明を提供します。
- 詳細 - 長い説明を提供します。
- 図 - 画像のキャプションに適した簡単な説明を提供します。 指定がない場合は、既定値が使用されます。
- アクセシビリティ - ユーザー補助のニーズを持つユーザー向けの詳細について、長い説明を提供します。
これらの API は Machine Learning (ML) モデルを使用するため、テキストが画像を正しく説明していない場合、エラーが発生することがあります。 そのため、以下のシナリオにおける画像には、これらの API の使用はお勧めできません。
- 画像にセンシティブな内容 (国旗、地図、地球儀、文化的シンボル、宗教的シンボルなど) が含まれる場合。不正確な説明により、論争を招く可能性があります。
- 医療に関するアドバイスや診断、法的コンテンツ、財務文書など、正確な説明が不可欠な場合。
画像の説明の例
次の例は、指定した説明の種類 (省略可能) とコンテンツ モデレーションのレベル (省略可能) に基づいて画像のテキストの説明を取得する方法を示しています。
メモ
SoftwareBitmap は現在サポートされていないため、イメージは ImageBuffer オブジェクトである必要があります (この例では、SoftwareBitmap を ImageBuffer に変換する方法を示します)。
GetReadyState メソッドを呼び出し、EnsureReadyAsync メソッドが正常に返されるのを待って、イメージスーパー解像度モデルを使用できることを確認します。
Image Super Resolution モデルが使用可能になったら、 ImageDescriptionGenerator オブジェクトを作成して参照します。
(省略可能) ContentFilterOptions オブジェクトを作成し、任意の値を指定します。 既定値を使用する場合は、null オブジェクトを渡すことができます。
元のイメージ、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();
auto loadResult = ImageScaler::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(bitmap); (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(inputImage, ImageDescriptionKind::BriefDescription, contentFilter).get();
string text = response.Description();
画像のセグメント化でできること
画像セグメンテーションを使用すると、画像内の特定のオブジェクトを識別できます。 モデルは、画像と "ヒント" オブジェクトの両方を受け取り、識別されたオブジェクトのマスクを返します。
ヒントは、以下の任意の組み合わせで提供できます。
- 識別対象に属するポイントの座標。
- 識別対象に属さないポイントの座標。
- 識別対象を囲む座標四角形。
提供するヒントが多いほど、モデルの精度が高くなります。 不正確な結果やエラーを最小限に抑えるには、次のヒントのガイドラインに従ってください。
- ヒントで複数の四角形を使用しないでください。不正確なマスクが生成される可能性があります。
- 包含ポイントまたは四角形を使用せずに、除外ポイントのみを使用することは避けてください。
- サポートされている最大 32 の座標 (ポイントの場合は 1、四角形の場合は 2) を超える座標を指定しないでください。この数を超えると、エラーが返されます。
返されるマスクはグレースケール 8 形式で、識別されたオブジェクトのマスクのピクセルの値は 255 です (その他の値はすべて 0)。
画像のセグメント化の例
次の例は、画像内のオブジェクトを識別する方法を示しています。 この例では、入力用のソフトウェア ビットマップ オブジェクト (softwareBitmap) が既にあることを前提としています。
GetReadyState メソッドを呼び出し、EnsureReadyAsync メソッドが正常に返されるのを待って、イメージセグメント化モデルを使用できることを確認します。
画像セグメント化モデルが使用可能になったら、 ImageObjectExtractor オブジェクトを作成して参照します。
イメージを CreateWithSoftwareBitmapAsync に渡します。
ImageObjectExtractorHint オブジェクトを作成します。 異なる入力値を持つヒント オブジェクトを作成するその他の方法については、後で説明します。
最終的な結果を返す GetSoftwareBitmapObjectMask メソッドを使用して、ヒントをモデルに送信します。
using Microsoft.Graphics.Imaging;
using Microsoft.Windows.AI;
using Microsoft.Windows.Management.Deployment;
using Windows.Graphics.Imaging;
if (ImageObjectExtractor::GetReadyState() == AIFeatureReadyState.NotReady)
{
var result = await ImageObjectExtractor.EnsureReadyAsync();
if (result.Status != AIFeatureReadyResultState.Success)
{
throw result.ExtendedError;
}
}
ImageObjectExtractor imageObjectExtractor = await ImageObjectExtractor.CreateWithSoftwareBitmapAsync(softwareBitmap);
ImageObjectExtractorHint hint = new ImageObjectExtractorHint{
includeRects: null,
includePoints:
new List<PointInt32> { new PointInt32(306, 212),
new PointInt32(216, 336)},
excludePoints: null};
SoftwareBitmap finalImage = imageObjectExtractor.GetSoftwareBitmapObjectMask(hint);
#include <winrt/Microsoft.Graphics.Imaging.h>
#include <winrt/Microsoft.Windows.AI.Imaging.h>
#include <winrt/Windows.Graphics.Imaging.h>
#include <winrt/Windows.Foundation.h>
using namespace winrt::Microsoft::Graphics::Imaging;
using namespace winrt::Microsoft::Windows::AI.Imaging;
using namespace winrt::Windows::Graphics::Imaging;
using namespace winrt::Windows::Foundation;
if (ImageObjectExtractor::GetReadyState() == AIFeatureReadyState::NotReady)
{
auto loadResult = ImageObjectExtractor::EnsureReadyAsync().get();
if (loadResult.Status() != AIFeatureReadyResultState::Success)
{
throw winrt::hresult_error(loadResult.ExtendedError());
}
}
ImageObjectExtractor imageObjectExtractor = ImageObjectExtractor::CreateWithSoftwareBitmapAsync(softwareBitmap).get();
ImageObjectExtractorHint hint(
{},
{
Windows::Graphics::PointInt32{306, 212},
Windows::Graphics::PointInt32{216, 336}
},
{}
);
Windows::Graphics::Imaging::SoftwareBitmap finalImage = imageObjectExtractor.GetSoftwareBitmapObjectMask(hint);
含まれるポイントと除外されるポイントでヒントを指定する
このコード スニペットは、含まれるポイントと除外されたポイントの両方をヒントとして使用する方法を示しています。
ImageObjectExtractorHint hint(
includeRects: null,
includePoints:
new List<PointInt32> { new PointInt32(150, 90),
new PointInt32(216, 336),
new PointInt32(550, 330)},
excludePoints:
new List<PointInt32> { new PointInt32(306, 212) });
ImageObjectExtractorHint hint(
{},
{
PointInt32{150, 90},
PointInt32{216, 336},
PointInt32{550, 330}
},
{
PointInt32{306, 212}
}
);
四角形でヒントを指定する
このコード スニペットは、四角形 (RectInt32 が X, Y, Width, Height) をヒントとして使用する方法を示しています。
ImageObjectExtractorHint hint(
includeRects:
new List<RectInt32> {new RectInt32(370, 278, 285, 126)},
includePoints: null,
excludePoints: null );
ImageObjectExtractorHint hint(
{
RectInt32{370, 278, 285, 126}
},
{},
{}
);
Image Foreground Extractor でできること
ImageForegroundExtractor を使用して入力イメージの前景をセグメント化し、背景の削除やステッカーの生成などの機能を有効にします。
返されるマスクはグレースケール 8 形式です。 ピクセル値の範囲は 0 から 255 で、0 は背景ピクセルを表し、255 は前景ピクセルを表し、中間値は前景ピクセルと背景ピクセルのブレンドを示します。
ビットマップ イメージからマスクを生成する
- GetReadyState を呼び出し、EnsureReadyAsync が正常に完了するのを待って、ImageForegroundExtractor オブジェクトの準備ができていることを確認します。
- モデルの準備ができたら、 CreateAsync を 呼び出して ImageForegroundExtractor オブジェクトをインスタンス化します。
- GetMaskFromSoftwareBitmap を入力イメージと共に呼び出して、フォアグラウンド マスクを生成します。
using Microsoft.Windows.AI.Imaging;
using Microsoft.Windows.AI;
if (ImageForegroundExtractor.GetReadyState() == AIFeatureReadyState.NotReady)
{
var result = await ImageForegroundExtractor.EnsureReadyAsync();
if (result.Status != AIFeatureReadyResultState.Success)
{
throw result.ExtendedError;
}
}
var model = await ImageForegroundExtractor.CreateAsync();
// Insert your own softwareBitmap here.
var foregroundMask = model.GetMaskFromSoftwareBitmap(softwareBitmap);
#include <winrt/Microsoft.Graphics.Imaging.h>
#include <winrt/Microsoft.Windows.AI.Imaging.h>
#include <winrt/Windows.Graphics.Imaging.h>
#include <winrt/Windows.Foundation.h>
using namespace winrt::Microsoft::Graphics::Imaging;
using namespace winrt::Microsoft::Windows::AI.Imaging;
using namespace winrt::Windows::Graphics::Imaging;
using namespace winrt::Windows::Foundation;
if (ImageForegroundExtractor::GetReadyState() == AIFeatureReadyState::NotReady)
{
auto loadResult = ImageForegroundExtractor::EnsureReadyAsync().get();
if (loadResult.Status() != AIFeatureReadyResultState::Success)
{
throw winrt::hresult_error(loadResult.ExtendedError());
}
}
auto model = co_await ImageForegroundExtractor::CreateAsync();
// Insert your own softwareBitmap here.
auto foregroundMask = model.GetMaskFromSoftwareBitmap(softwareBitmap);
オブジェクト消去機能で何ができるか
オブジェクトの消去を使用すると、イメージからオブジェクトを削除できます。 モデルは、削除するオブジェクトを示すイメージとグレースケール マスクの両方を受け取り、マスクされた領域をイメージから消去し、消去された領域を画像の背景に置き換えます。
オブジェクトの消去の例
次の例は、イメージからオブジェクトを削除する方法を示しています。 この例では、イメージとマスクの両方のソフトウェア ビットマップ オブジェクト (softwareBitmap) が既にあることを前提としています。 マスクは Gray8 形式で、削除する領域の各ピクセルを 255 に設定し、他のすべてのピクセルを 0 に設定する必要があります。
- GetReadyState メソッドを呼び出し、EnsureReadyAsync メソッドが正常に返されるのを待って、イメージセグメント化モデルを使用できることを確認します。
- オブジェクト消去モデルが使用可能になったら、それを参照する ImageObjectRemover オブジェクトを作成します。
- 最後に、 RemoveFromSoftwareBitmap メソッドを使用してイメージとマスクをモデルに送信し、最終的な結果を返します。
using Microsoft.Graphics.Imaging;
using Microsoft.Windows.AI;
using Microsoft.Windows.Management.Deployment;
using Windows.Graphics.Imaging;
if (ImageObjectRemover::GetReadyState() == AIFeatureReadyState.NotReady)
{
var result = await ImageObjectRemover.EnsureReadyAsync();
if (result.Status != AIFeatureReadyResultState.Success)
{
throw result.ExtendedError;
}
}
ImageObjectRemover imageObjectRemover = await ImageObjectRemover.CreateAsync();
SoftwareBitmap finalImage = imageObjectRemover.RemoveFromSoftwareBitmap(imageBitmap, maskBitmap); // Insert your own imagebitmap and maskbitmap
#include <winrt/Microsoft.Graphics.Imaging.h>
#include <winrt/Microsoft.Windows.AI.Imaging.h>
#include <winrt/Windows.Graphics.Imaging.h>
#include <winrt/Windows.Foundation.h>
using namespace winrt::Microsoft::Graphics::Imaging;
using namespace winrt::Microsoft::Windows::AI.Imaging;
using namespace winrt::Windows::Graphics::Imaging;
using namespace winrt::Windows::Foundation;
if (ImageObjectRemover::GetReadyState() == AIFeatureReadyState::NotReady)
{
auto loadResult = ImageObjectRemover::EnsureReadyAsync().get();
if (loadResult.Status() != AIFeatureReadyResultState::Success)
{
throw winrt::hresult_error(loadResult.ExtendedError());
}
}
ImageObjectRemover imageObjectRemover = ImageObjectRemover::CreateAsync().get();
// Insert your own imagebitmap and maskbitmap
Windows::Graphics::Imaging::SoftwareBitmap buffer =
imageObjectRemover.RemoveFromSoftwareBitmap(imageBitmap, maskBitmap);
責任ある AI
これらのイメージング API が、信頼性が高く、安全で、責任ある形で開発されていることを保証するために、次の手順を組み合わせて実施しました。 アプリで AI 機能を実装する場合は、「Windows での責任ある生成 AI 開発」で説明されているベスト プラクティスを確認することをお勧めします。