共用方式為


影像物件擷取器

你可以使用影像物件擷取器來辨識影像中的特定物件。 模型會同時接收影像和「提示」對象,並傳回已識別物件的遮罩。

如需 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>

提供提示

你可以透過以下任意組合提供提示:

  • 屬於你所識別之點的座標。
  • 與您所識別的點無關的座標。
  • 一個座標矩形,環繞您要識別的內容。

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

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

回傳的遮罩為灰階 8 格式,識別物件遮罩像素值為 255(其他像素為 0)。

影像物件擷取器範例

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

  1. 請呼叫 GetReadyState 方法並等待 EnsureReadyAsync 方法成功返回,確保影像物件擷取器模型可用。

  2. 一旦影像物件擷取器模型可用,建立一個 ImageObjectExtractor 物件來參考它。

  3. 將映像傳遞至 CreateWithSoftwareBitmapAsync

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

  5. 使用 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}
    }, 
    {},
    {}
);

另請參閱