图像说明

重要

图像说明目前在中国不可用。

可以使用图像说明 API 为图像生成各种类型的文本说明。

有关 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>

说明类型

支持以下类型的文本说明:

  • 简要 - 提供适合图表和图示的说明。
  • 详细 - 提供较长的说明。
  • 图表 - 提供适合用作图像说明的简短描述。 如果未指定任何值,则为默认值。
  • 可访问 - 提供详细的长说明,旨在为有辅助功能需求的用户提供信息。

局限性

由于这些 API 使用机器学习 (ML) 模型,因此在文本无法正确描述图像的位置偶尔会发生错误。 因此,不建议在以下方案中将这些 API 用于映像:

  • 其中图像包含潜在的敏感内容和不准确的描述可能会引起争议,例如国旗、地图、地球、文化符号或宗教符号。
  • 当准确的描述至关重要时,例如医疗建议或诊断、法律内容或财务文档。

图像说明示例

以下示例演示如何根据指定的说明类型(可选)和内容审查级别(可选)获取图像的文本说明。

注释

图像必须是 ImageBuffer 对象,因为当前不支持 SoftwareBitmap (此示例演示如何将 SoftwareBitmap 转换为 ImageBuffer)。

  1. 通过调用 GetReadyState 方法,然后等待 EnsureReadyAsync 方法成功返回,确保图像说明模型可用。

  2. 映像说明模型可用后,创建 ImageDescriptionGenerator 对象来引用它。

  3. (可选)创建 ContentFilterOptions 对象并指定首选值。 如果选择使用默认值,则可以传入 null 对象。

  4. 通过调用指定原始图像的 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();

    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(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(inputBuffer, ImageDescriptionKind::BriefDescription, contentFilter).get();
string text = response.Description();

另请参阅