共用方式為


影像描述

這很重要

圖片描述目前在中國無法使用。

你可以使用圖片描述 API,為圖片產生各種文字描述。

如需 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 使用 Machine Learning (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();

另請參閱