共用方式為


在 Windows 應用程式 SDK 開始使用 AI 映像

重要

Windows App SDK 的最新 實驗通道 版本中提供。

Windows App SDK 實驗通道包含開發初期階段的 API 和功能。 實驗通道中的所有 API 都受限於廣泛的修訂和中斷性變更,而且可以隨時從後續版本中移除。 實驗性功能不支援在生產環境中使用,且使用它們的應用程式無法發佈至Microsoft市集。

  • 影像描述功能不適用於中國大陸。
  • 不支援獨立應用程式。

映射功能是由 Windows App SDK 透過一組由人工智慧 (AI) 支援的 API 來提供,可支援下列功能:

  • 影像超級解析度:縮放和銳化影像
  • 影像描述:產生描述影像的文字
  • 影像分割:識別影像內的物件

如需 API 詳細資料,請參閱 Windows App SDK 中適用於 AI 映像功能的API 參考。

如需 內容管理詳細資料,請參閱 生成式 AI API 的內容安全性

提示

在 Windows App SDK GitHub 存放庫中建立 新問題(在標題中包含 Imaging)或回應 現有問題,以提供這些 API 及其功能的意見反應。

必要條件

我可以使用影像超解析度做什麼?

Windows App SDK 中的影像進階解析 API 會啟用影像銳化和調整。

縮放比例上限為8倍。 較高的比例因子可能會導致失真並影響影像精確度。 如果最終寬度或高度大於其原始值 8 倍,則會擲回例外狀況。

下列範例示範如何使用 targetWidth 對象來變更現有軟體點圖影像的縮放比例(targetHeightsoftwareBitmapImageScaler),以及改善影像銳度(以改善影像的銳度而不調整影像,只需指定現有的影像寬度和高度)。

  1. 呼叫 ImageScaler.IsAvailable 方法,然後等候 ImageScaler.MakeAvailableAsync 方法順利傳回,以確保影像超解析度模型可供使用。

  2. 一旦影像超解析度模型可供使用,請建立 ImageScaler 對象來參考它。

  3. 使用 ScaleSoftwareBitmap 方法,將現有的影像和所需的寬度和高度傳遞至模型,以取得現有影像的銳化和縮放版本。

using Microsoft.Graphics.Imaging;
using Microsoft.Windows.Management.Deployment;
using Windows.Graphics.Imaging;

if (!ImageScaler.IsAvailable())
{
    var result = await ImageScaler.MakeAvailableAsync();
    if (result.Status != PackageDeploymentStatus.CompletedSuccess)
    {
        throw result.ExtendedError;
    }
}
ImageScaler imageScaler = await ImageScaler.CreateAsync();
SoftwareBitmap finalImage = imageScaler.ScaleSoftwareBitmap(softwareBitmap, targetWidth, targetHeight);
#include <winrt/Microsoft.Graphics.Imaging.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Graphics.Imaging.h>

using namespace winrt::Microsoft::Graphics::Imaging;
using namespace winrt::Windows::Foundation; 
using namespace winrt::Windows::Graphics::Imaging; 

 
if (!ImageScaler::IsAvailable()) 
{ 
    winrt::PackageDeploymentResult result = ImageScaler::MakeAvailableAsync().get(); 
    if (result.Status() != PackageDeploymentStatus::CompletedSuccess)
    {
       throw result.ExtendedError();
    }
}

ImageScaler imageScaler = ImageScaler::CreateAsync().get(); 
SoftwareBitmap finalImage = imageScaler.ScaleSoftwareBitmap(softwareBitmap, targetWidth, targetHeight);

我可以使用影像描述做什麼?

重要

影像描述目前無法在中國大陸中使用。

Windows App SDK 中的影像描述 API 可讓您產生影像的各種文字描述類型。

支援下列型態的文字描述:

  • 無障礙功能 - 提供詳細說明,包含使用者若有無障礙需求時所需的詳細資訊。
  • Caption - 提供適合影像標題的簡短描述。 如果未指定任何值,則為預設值。
  • DetailedNarration - 提供冗長的描述。
  • OfficeCharts - 提供適合圖表和圖解的描述。

由於這些 API 使用 Machine Learning (ML) 模型,因此文字無法正確描述影像時,偶爾會發生錯誤。 因此,在下列案例中,不建議將這些 API 用於影像:

  • 如果影像包含潛在的敏感性內容和不正確的描述,可能會引起爭議,例如旗標、地圖、地球、文化符號或宗教符號。
  • 當正確描述很重要時,例如醫療建議或診斷、法律內容或財務檔。

從影像取得文字描述

影像描述 API 會擷取影像、所需的文字描述類型(選擇性),以及您想要採用的內容仲裁層級(選擇性),以防止有害的使用。

下列範例示範如何取得影像的文字描述。

注意

映像必須是 ImageBuffer 對象,因為目前不支援 SoftwareBitmap。 這個範例示範如何將 SoftwareBitmap 轉換成 ImageBuffer

  1. 呼叫 ImageDescriptionGenerator.IsAvailable 方法,然後等候 ImageDescriptionGenerator.MakeAvailableAsync 方法順利傳回,以確保影像超解析度模型可供使用。

  2. 一旦影像超解析度模型可供使用,請建立 ImageDescriptionGenerator 對象來參考它。

  3. (選擇性)建立 ContentFilterOptions 物件,並指定您慣用的值。 如果您選擇使用預設值,則可以傳入 Null 物件。

  4. 使用原始影像呼叫 LanguageModelResponse.Response 方法、慣用描述類型的列舉和 ImageDescriptionGenerator.DescribeAsync 物件(選擇性),以取得影像描述 (ContentFilterOptions)。

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

if (!ImageDescriptionGenerator.IsAvailable())
{
    var result = await ImageDescriptionGenerator.MakeAvailableAsync();
    if (result.Status != PackageDeploymentStatus.CompletedSuccess)
    {
        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.ContentModeration.h>
#include <winrt/Microsoft.Windows.AI.Generative.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::ContentModeration; 
using namespace winrt::Microsoft::Windows::AI::Generative; 
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::IsAvailable()) 
{ 
    winrt::PackageDeploymentResult result = ImageDescriptionGenerator::MakeAvailableAsync().get(); 
    if (result.Status() != PackageDeploymentStatus::CompletedSuccess)
    {
       throw result.ExtendedError();
    }
}

ImageDescriptionGenerator imageDescriptionGenerator = ImageDescriptionGenerator::CreateAsync().get(); 
// Convert already available softwareBitmap to ImageBuffer.
auto inputBuffer = ImageBuffer::CreateCopyFromBitmap(softwareBitmap); 

// Create content moderation thresholds object.
 ContentFilterOptions contentFilter{};
 contentFilter.PromptMinSeverityLevelToBlock().ViolentContentSeverity(SeverityLevel::Medium);
 contentFilter.ResponseMinSeverityLevelToBlock().ViolentContentSeverity(SeverityLevel::Medium);


// Get text description.
LanguageModelResponse languageModelResponse = imageDescriptionGenerator.DescribeAsync(inputImage, ImageDescriptionScenario::Caption, contentFilter).get();
string text = languageModelResponse.Response();

我可以使用影像分割做什麼?

影像分割可用來識別影像中的特定物件。 模型會同時取得影像和「提示」物件,並傳回已識別物件的遮罩。

您可以透過下列任何組合來提供提示:

  • 屬於您所識別之點的座標。
  • 不屬於您所識別之點的座標。
  • 一個座標矩形,以括住您要識別的內容。

您提供的提示越多,模型就越精確。 請遵循這些提示指導方針,將不正確的結果或錯誤降到最低。

  • 避免在提示中使用多個矩形,因為它們會產生不正確的遮罩。
  • 避免在不含包含點或矩形的情況下獨佔使用排除點。
  • 請勿指定超過支援的 32 個座標上限 (1 代表點,矩形為 2),因為這樣會傳回錯誤。

傳回的遮罩採用灰階-8 格式,且已識別物件的遮罩圖元值為 255(所有其他值為 0)。

識別影像中的物件

下列範例示範如何識別影像中的物件。 這些範例假設您已經有作為輸入的軟體位圖物件(softwareBitmap)。

  1. 呼叫 IsAvailable 方法並等候 MakeAvailableAsync 方法順利傳回,以確保影像分割模型可供使用。

  2. 一旦影像分割模型可供使用,請建立 ImageObjectExtractor 對象來參考它。

  3. 將映像傳遞至 ImageObjectExtractor.CreateWithSoftwareBitmapAsync

  4. 建立 ImageObjectExtractorHint 物件。 稍後會示範建立具有不同輸入的提示物件的其他方式。

  5. 使用傳回最終結果的 GetSoftwareBitmapObjectMask 方法,將提示提交至模型。

using Microsoft.Graphics.Imaging;
using Microsoft.Windows.Management.Deployment;
using Windows.Graphics.Imaging;

if (!ImageObjectExtractor.IsAvailable())
{
    var result = await ImageObjectExtractor.MakeAvailableAsync();
    if (result.Status != PackageDeploymentStatus.CompletedSuccess)
    {
        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/Windows.Graphics.Imaging.h>
#include <winrt/Windows.Foundation.h>
using namespace winrt::Microsoft::Graphics::Imaging; 
using namespace winrt::Windows::Graphics::Imaging; 
using namespace winrt::Windows::Foundation; 


if (!ImageObjectExtractor::IsAvailable()) 
{ 
    winrt::PackageDeploymentResult result = ImageObjectExtractor::MakeAvailableAsync().get(); 
    if (result.Status() != PackageDeploymentStatus::CompletedSuccess)
    {
       throw result.ExtendedError();
    }
}

ImageObjectExtractor imageObjectExtractor =  ImageObjectExtractor::CreateWithSoftwareBitmapAsync(softwareBitmap).get();

ImageObjectExtractorHint hint(
    {}, 
    { 
        PointInt32{306, 212}, 
        PointInt32{216, 336} 
    },
    {}
);

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}
    }, 
    {},
    {}
);

負責任的人工智慧

這些映像 API 可為開發人員提供強大且值得信任的模型,以安全且安全的 AI 體驗建置應用程式。 我們已使用下列步驟的組合,以確保這些映像 API 值得信任、安全且負責任地建置。 建議您在應用程式中實作 AI 功能時,參考 Windows 上 負責任生成式 AI 開發 所述的最佳實踐進行檢閱。

  • 徹底測試和評估模型品質,以識別並降低潛在風險。
  • 累加推出映像 API 實驗性版本。 在最後一個實驗性版本之後,擴展過程將會涉及已簽署的應用程式,以確保惡意軟體掃描已套用至具備本地模型功能的應用程式。
  • 提供一個本地化 AI 模型,用於內容審核,以識別和過濾任何使用生成式 AI 模型的 API 的輸入內容和 AI 生成的輸出內容中的有害資訊。 此本機 con 帳篷模式 ration 模型是以 Azure AI 內容安全性模型為基礎,以進行文字仲裁並提供類似的效能。

重要

沒有任何內容安全性系統無法發生,而且偶爾會發生錯誤,因此我們建議整合補充責任 AI(RAI) 工具和做法。 如需詳細資訊,請參閱 Windows 上的負責任 Generative AI 開發。