Windows AI API 支援的 AI 影像功能可實現以下功能:
如需 API 詳細數據,請參閱 適用於 AI 映像功能的 API 參考。
如需 內容仲裁詳細數據,請參閱 使用衍生式 AI API 的內容安全性。
重要
套件資訊清單需求:若要使用 Windows AI 影像處理 API,您的應用程式必須封裝為 MSIX 套件,並在您的systemAIModels中聲明 Package.appxmanifest 功能。 此外,請確保您的清單的 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 可啟用影像銳化和縮放。
縮放比例限制最高為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);
我可以使用影像描述做什麼?
重要
圖片描述目前在中國無法使用。
影像描述 API 可讓您為影像產生各種類型的文字描述。
支援下列型態的文字描述:
- 簡介 - 提供適用於圖表和圖形的描述。
- 詳細 - 提供詳細描述。
- 圖表 — 提供適合影像標題的簡短描述。 如果未指定任何值,則為預設值。
- 無障礙 - 提供詳細說明,為有協助工具需求的使用者提供詳細資料。
由於這些 API 使用 Machine Learning (ML) 模型,因此文字無法正確描述影像時,偶爾會發生錯誤。 因此,在下列案例中,不建議將這些 API 用於影像:
- 如果影像包含潛在的敏感性內容和不正確的描述,可能會引起爭議,例如旗標、地圖、地球、文化符號或宗教符號。
- 當正確描述很重要時,例如醫療建議或診斷、法律內容或財務檔。
影像描述範例
下列範例顯示如何根據指定的描述型別 (選用) 和內容審核層級 (選用) 取得影像的文字描述。
注意
映像必須是 ImageBuffer 物件,因為目前不支援 SoftwareBitmap (此範例示範如何將 SoftwareBitmap 轉換成 ImageBuffer) 。
呼叫 GetReadyState 方法,然後等候 EnsureReadyAsync 方法成功傳回,以確保影像超解析度模型可供使用。
一旦影像超解析度模型可供使用,請建立 ImageDescriptionGenerator 對象來參考它。
(選擇性)建立 ContentFilterOptions 物件,並指定您慣用的值。 如果您選擇使用預設值,則可以傳入 Null 物件。
呼叫指定原始影像的 DescribeAsync 方法、ImageDescriptionKind (慣用描述類型的選擇性值) 和 ContentFilterOptions 物件 (選擇性) 來取得影像描述 (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}
},
{},
{}
);
我可以用圖像前景提取器做什麼?
使用 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);
負責任的人工智慧
我們已使用下列步驟的組合,以確保這些映像 API 值得信任、安全且負責任地建置。 建議您檢閱在應用程式中實作 AI 功能時,在 Windows 上負責任產生 AI 開發 中所述的最佳做法。