可以使用图像对象提取程序来标识图像中的特定对象。 该模型同时接收图像和“提示”对象,并返回已识别对象的掩码。
有关 API 详细信息,请参阅 人工智能图像功能的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)。
Image Object Extractor 示例
以下示例演示了标识图像中的对象的方法。 这些示例假定你已经有一个用于输入的软件位图对象(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}
},
{},
{}
);