您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

示例:使用 C# 探索图像处理应用Sample: Explore an image processing app with C#

探索一款基本 Windows 应用程序,该应用程序使用计算机视觉执行光学字符识别 (OCR)、创建智能裁剪缩略图,并对图像中的视觉特征(包括人脸)进行检测、分类、标记和描述。Explore a basic Windows application that uses Computer Vision to perform optical character recognition (OCR), create smart-cropped thumbnails, plus detect, categorize, tag and describe visual features, including faces, in an image. 通过以下示例可提交图像 URL 或存储于本地的文件。The below example lets you submit an image URL or a locally stored file. 可将此开放源代码示例作为模板,使用计算机视觉 API 和 .NET Framework 中的 Windows Presentation Foundation (WPF) 构建自己的 Windows 应用程序。You can use this open source example as a template for building your own app for Windows using the Computer Vision API and Windows Presentation Foundation (WPF), a part of .NET Framework.

  • 从 GitHub 获取示例应用Get the sample app from GitHub
  • 在 Visual Studio 中打开并构建示例应用Open and build the sample app in Visual Studio
  • 运行示例应用并与其交互以执行各种场景Run the sample app and interact with it to perform various scenarios
  • 探索示例应用附带的各种场景Explore the various scenarios included with the sample app

先决条件Prerequisites

在探索示例应用之前,请确保已满足以下先决条件:Before exploring the sample app, ensure that you've met the following prerequisites:

获取示例应用Get the sample app

计算机视觉示例应用已在 Microsoft/Cognitive-Vision-Windows 存储库的 GitHub 上发布。The Computer Vision sample app is available on GitHub from the Microsoft/Cognitive-Vision-Windows repository. 此存储库还包含 Microsoft/Cognitive-Common-Windows 存储库作为 Git 子模块。This repository also includes the Microsoft/Cognitive-Common-Windows repository as a Git submodule. 可以通过使用命令行中的 git clone --recurse-submodules 命令或使用 GitHub 桌面以递归方式克隆此存储库(包括子模块)。You can recursively clone this repository, including the submodule, either by using the git clone --recurse-submodules command from the command line, or by using GitHub Desktop.

例如,若要从命令提示符以递归方式克隆计算机视觉示例应用的存储库,请运行以下命令:For example, to recursively clone the repository for the Computer Vision sample app from a command prompt, run the following command:

git clone --recurse-submodules https://github.com/Microsoft/Cognitive-Vision-Windows.git

重要

请勿将此存储库下载为 Zip 文件。Do not download this repository as a ZIP. 若将存储库下载为 Zip 文件,则 Git 不包含子模块。Git doesn't include submodules when downloading a repository as a ZIP.

获取可选的示例图像Get optional sample images

可以选择使用人脸示例应用中包含的示例图像,该应用已在 Microsoft/Cognitive-Face-Windows 存储库的 GitHub 上发布。You can optionally use the sample images included with the Face sample app, available on GitHub from the Microsoft/Cognitive-Face-Windows repository. 该示例应用包含一个文件夹 /Data,其中包含多个人物图像。That sample app includes a folder, /Data, which contains multiple images of people. 也可以通过描述用于计算机视觉示例应用的方法以递归方式克隆此存储库。You can recursively clone this repository, as well, by the methods described for the Computer Vision sample app.

例如,要从命令提示符递归克隆人脸示例应用的存储库,请运行以下命令:For example, to recursively clone the repository for the Face sample app from a command prompt, run the following command:

git clone --recurse-submodules https://github.com/Microsoft/Cognitive-Face-Windows.git

在 Visual Studio 中打开并构建示例应用Open and build the sample app in Visual Studio

首先必须构建示例应用,以让 Visual Studio 解析依赖关系,然后才能运行或浏览示例应用。You must build the sample app first, so that Visual Studio can resolve dependencies, before you can run or explore the sample app. 要打开并构建示例应用,请执行以下步骤:To open and build the sample app, do the following steps:

  1. 在 Visual Studio 中打开 Visual Studio 解决方案文件 /Sample-WPF/VisionAPI-WPF-Samples.slnOpen the Visual Studio solution file, /Sample-WPF/VisionAPI-WPF-Samples.sln, in Visual Studio.

  2. 确保 Visual Studio 解决方案包含两个项目:Ensure that the Visual Studio solution contains two projects:

    • SampleUserControlLibrarySampleUserControlLibrary
    • VisionAPI-WPF-SamplesVisionAPI-WPF-Samples

    如果没有 SampleUserControlLibrary 项目,请确认已经以递归方式克隆 Microsoft/Cognitive-Vision-Windows 存储库。If the SampleUserControlLibrary project is unavailable, confirm that you've recursively cloned the Microsoft/Cognitive-Vision-Windows repository.

  3. 在 Visual Studio 中,按 Ctrl+Shift+B 或从功能区菜单中选择“构建”,然后选择“构建解决方案”以构建该解决方案。In Visual Studio, either press Ctrl+Shift+B or choose Build from the ribbon menu and then choose Build Solution to build the solution.

运行示例应用并与之交互Run and interact with the sample app

可以运行示例应用,以便在执行各种任务(例如生成缩略图或标记图像)时查看它与你以及与计算机视觉客户端库的交互方式。You can run the sample app, to see how it interacts with you and with the Computer Vision client library when performing various tasks, such as generating thumbnails or tagging images. 要运行示例应用并与之交互,请执行以下步骤:To run and interact with the sample app, do the following steps:

  1. 构建完成后,按“F5”或从功能区菜单中选择“调试”,然后选择“开始调试”以运行示例应用。After the build is complete, either press F5 or choose Debug from the ribbon menu and then choose Start debugging to run the sample app.

  2. 显示示例应用时,从导航窗格中选择“订阅密钥管理”以显示“订阅密钥管理”页面。When the sample app is displayed, choose Subscription Key Management from the navigation pane to display the Subscription Key Management page. 订阅密钥管理页面Subscription Key Management page

  3. 在“订阅密钥”中输入订阅密钥。Enter your subscription key in Subscription Key.

  4. 在“终结点”中输入订阅密钥的计算机视觉资源的终结点 URL(省略 /vision/v1.0)。Enter the endpoint URL, omitting the /vision/v1.0, of the Computer Vision resource for your subscription key in Endpoint.
    例如,如果你使用的是计算机视觉免费试用版中的订阅密钥,请输入以下美国中西部 Azure 区域的终结点 URL:https://westcentralus.api.cognitive.microsoft.comFor example, if you're using the subscription key from the Computer Vision free trial, enter the following endpoint URL for the West Central US Azure region: https://westcentralus.api.cognitive.microsoft.com

  5. 如果不想在下次运行该示例应用时输入订阅密钥和终结点 URL,请选择“保存设置”以将订阅密钥和终结点 URL 保存到计算机。If you don't want to enter your subscription key and endpoint URL the next time you run the sample app, choose Save Setting to save the subscription key and endpoint URL to your computer. 如果要删除以前保存的订阅密钥和终结点 URL,请选择“删除设置”。If you want to delete your previously-saved subscription key and endpoint URL, choose Delete Setting.

    备注

    示例应用使用独立存储和 System.IO.IsolatedStorage 来存储订阅密钥和终结点 URL。The sample app uses isolated storage, and System.IO.IsolatedStorage, to store your subscription key and endpoint URL.

  6. 在导航窗格中的“选择场景”处,选择示例应用当前包含的场景之一:Under Select a scenario in the navigation pane, select one of the scenarios currently included with the sample app:

    场景Scenario 说明Description
    分析图像Analyze Image 使用分析图像操作来分析本地或远程图像。Uses the Analyze Image operation to analyze a local or remote image. 可以选择视觉特征和语言进行分析,并查看图像和结果。You can choose the visual features and language for the analysis, and see both the image and the results.
    使用域模型分析图像Analyze Image with Domain Model 使用列出域特定模型操作列出可供选择的域模型,并使用识别域特定内容操作使用所选域模型分析本地或远程图像。Uses the List Domain Specific Models operation to list the domain models from which you can select, and the Recognize Domain Specific Content operation to analyze a local or remote image using the selected domain model. 此外还可以选择分析的语言。You can also choose the language for the analysis.
    描述图像Describe Image 使用描述图像操作创建本地或远程图像的用户可读描述。Uses the Describe Image operation to create a human-readable description of a local or remote image. 此外还可以选择描述的语言。You can also choose the language for the description.
    生成标记Generate Tags 使用标记图像操作标记本地或远程图像的视觉特征。Uses the Tag Image operation to tag the visual features of a local or remote image. 此外还可以选择标记的语言。You can also choose the language used for the tags.
    识别文本 (OCR)Recognize Text (OCR) 使用 OCR 操作识别图像并从图像中提取打印的文本。Uses the OCR operation to recognize and extract printed text from an image. 可以选择要使用的语言,也可以让计算机视觉自动检测语言。You can either choose the language to use, or let Computer Vision auto-detect the language.
    识别文本 V2(英语)Recognize Text V2 (English) 使用识别文本获取识别文本操作结果操作以异步方式识别图像和提取图像中的打印文本或手写文本。Uses the Recognize Text and Get Recognize Text Operation Result operations to asynchronously recognize and extract printed or handwritten text from an image.
    获取缩略图Get Thumbnail 使用获取缩略图操作生成本地或远程图像的缩略图。Uses the Get Thumbnail operation to generate a thumbnail for a local or remote image.

    以下屏幕截图展示了在分析样本图像后为分析图像场景提供的页面。The following screenshot illustrates the page provided for the Analyze Image scenario, after analyzing a sample image. “分析图像”页的屏幕截图Screenshot of the Analyze image page

探索示例应用Explore the sample app

计算机视觉示例应用的 Visual Studio 解决方案包含两个项目:The Visual Studio solution for the Computer Vision sample app contains two projects:

  • SampleUserControlLibrarySampleUserControlLibrary
    SampleUserControlLibrary 项目提供多项认知服务样本共享的功能。The SampleUserControlLibrary project provides functionality shared by multiple Cognitive Services samples. 该项目包含以下内容:The project contains the following:
    • SampleScenariosSampleScenarios
      UserControl 为样本提供标准化的表示方式,例如标题栏、导航窗格和内容窗格。A UserControl that provides a standardized presentation, such as the title bar, navigation pane, and content pane, for samples. 计算机视觉示例应用在 MainWindow.xaml 窗口中使用此控件来显示场景页面并访问多个场景共享的信息,例如订阅密钥和终结点 URL。The Computer Vision sample app uses this control in the MainWindow.xaml window to display scenario pages and access information shared across scenarios, such as the subscription key and endpoint URL.
    • SubscriptionKeyPageSubscriptionKeyPage
      是一个提供标准化布局的页面,用于输入示例应用的订阅密钥和终结点 URL。A Page that provides a standardized layout for entering a subscription key and endpoint URL for the sample app. 计算机视觉示例应用使用此页面来管理场景页面使用的订阅密钥和终结点 URL。The Computer Vision sample app uses this page to manage the subscription key and endpoint URL used by the scenario pages.
    • VideoResultControlVideoResultControl
      UserControl 提供视频信息的标准化表示方式。A UserControl that provides a standardized presentation for video information. 计算机视觉示例应用不使用此控件。The Computer Vision sample app doesn't use this control.
  • VisionAPI-WPF-SamplesVisionAPI-WPF-Samples
    是计算机视觉示例应用的主项目,该项目包含计算机视觉全部有意思的功能。The main project for the Computer Vision sample app, this project contains all of the interesting functionality for Computer Vision. 该项目包含以下内容:The project contains the following:
    • AnalyzeInDomainPage.xamlAnalyzeInDomainPage.xaml
      分析图像与域模型场景的场景页面。The scenario page for the Analyze Image with Domain Model scenario.
    • AnalyzeImage.xamlAnalyzeImage.xaml
      分析图像场景的场景页面。The scenario page for the Analyze Image scenario.
    • DescribePage.xamlDescribePage.xaml
      描述图像场景的场景页面。The scenario page for the Describe Image scenario.
    • ImageScenarioPage.csImageScenarioPage.cs
      从其中派生示例应用的所有场景页面的 ImageScenarioPage 类。The ImageScenarioPage class, from which all of the scenario pages in the sample app are derived. 此类管理所有场景页面的共享功能,例如提供凭据以及输出的格式设置功能。This class manages functionality, such as providing credentials and formatting output, shared by all of the scenario pages.
    • MainWindow.xamlMainWindow.xaml
      是示例应用的主窗口,它使用 SampleScenarios 控件来显示 SubscriptionKeyPage 和场景页面。The main window for the sample app, it uses the SampleScenarios control to present the SubscriptionKeyPage and scenario pages.
    • OCRPage.xamlOCRPage.xaml
      识别文本 (OCR) 场景的场景页面。The scenario page for the Recognize Text (OCR) scenario.
    • RecognizeLanguage.csRecognizeLanguage.cs
      RecognizeLanguage 类:提供有关示例应用中的各种方法支持的语言的信息。The RecognizeLanguage class, which provides information about the languages supported by the various methods in the sample app.
    • TagsPage.xamlTagsPage.xaml
      生成标记场景的场景页面。The scenario page for the Generate Tags scenario.
    • TextRecognitionPage.xamlTextRecognitionPage.xaml
      识别文本 V2(英语)场景的场景页面。The scenario page for the Recognize Text V2 (English) scenario.
    • ThumbnailPage.xamlThumbnailPage.xaml
      获取缩略图场景的场景页面。The scenario page for the Get Thumbnail scenario.

探索示例代码Explore the sample code

示例代码的关键部分包含以 KEY SAMPLE CODE STARTS HERE 开头并以 KEY SAMPLE CODE ENDS HERE 结尾的注释块,以便更轻松地浏览示例应用。Key portions of sample code are framed with comment blocks that start with KEY SAMPLE CODE STARTS HERE and end with KEY SAMPLE CODE ENDS HERE, to make it easier for you to explore the sample app. 示例代码的关键部分包含与学习如何使用计算机视觉 API 客户端库执行各种任务最相关的代码。These key portions of sample code contain the code most relevant to learning how to use the Computer Vision API client library to do various tasks. 可以在 Visual Studio 中搜索 KEY SAMPLE CODE STARTS HERE,以便在计算机视觉示例应用中最相关的代码段之间移动。You can search for KEY SAMPLE CODE STARTS HERE in Visual Studio to move between the most relevant sections of code in the Computer Vision sample app.

例如,下面显示并包含在 AnalyzePage.xaml 中的 UploadAndAnalyzeImageAsync 方法演示了如何使用客户端库通过调用 ComputerVisionClient.AnalyzeImageInStreamAsync 方法来分析本地图像。For example, the UploadAndAnalyzeImageAsync method, shown following and included in AnalyzePage.xaml, demonstrates how to use the client library to analyze a local image by invoking the ComputerVisionClient.AnalyzeImageInStreamAsync method.

private async Task<ImageAnalysis> UploadAndAnalyzeImageAsync(string imageFilePath)
{
    // -----------------------------------------------------------------------
    // KEY SAMPLE CODE STARTS HERE
    // -----------------------------------------------------------------------

    //
    // Create Cognitive Services Vision API Service client.
    //
    using (var client = new ComputerVisionClient(Credentials) { Endpoint = Endpoint })
    {
        Log("ComputerVisionClient is created");

        using (Stream imageFileStream = File.OpenRead(imageFilePath))
        {
            //
            // Analyze the image for all visual features.
            //
            Log("Calling ComputerVisionClient.AnalyzeImageInStreamAsync()...");
            VisualFeatureTypes[] visualFeatures = GetSelectedVisualFeatures();
            string language = (_language.SelectedItem as RecognizeLanguage).ShortCode;
            ImageAnalysis analysisResult = await client.AnalyzeImageInStreamAsync(imageFileStream, visualFeatures, null, language);
            return analysisResult;
        }
    }

    // -----------------------------------------------------------------------
    // KEY SAMPLE CODE ENDS HERE
    // -----------------------------------------------------------------------
}

探索客户端库Explore the client library

此示例应用使用计算机视觉 API 客户端库,这是 Azure 认知服务中的计算机视觉 API 的精简 C# 客户端包装器。This sample app uses the Computer Vision API client library, a thin C# client wrapper for the Computer Vision API in Azure Cognitive Services. 可从 NuGet 的 Microsoft.Azure.CognitiveServices.Vision.ComputerVision 包中获得客户端库。The client library is available from NuGet in the Microsoft.Azure.CognitiveServices.Vision.ComputerVision package. 在构建 Visual Studio 应用示例时,会从相应的 NuGet 包中检索了客户端库。When you built the Visual Studio application, you retrieved the client library from its corresponding NuGet package. 还可以在 Microsoft/Cognitive-Vision-Windows 存储库的 /ClientLibrary 文件夹中查看客户端库的源代码。You can also view the source code for the client library in the /ClientLibrary folder of the Microsoft/Cognitive-Vision-Windows repository.

客户端库的功能以 Microsoft.Azure.CognitiveServices.Vision.ComputerVision 命名空间中的 ComputerVisionClient 类为中心,而 ComputerVisionClient 类与计算机视觉交互时使用的模型位于 Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models 命名空间中。The client library's functionality centers around the ComputerVisionClient class, in the Microsoft.Azure.CognitiveServices.Vision.ComputerVision namespace, while the models used by the ComputerVisionClient class when interacting with Computer Vision are found in the Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models namespace. 在示例应用附带的各种 XAML 场景页面中,将找到以下针对这些命名空间的 using 指令:In the various XAML scenario pages included with the sample app, you'll find the following using directives for those namespaces:

// -----------------------------------------------------------------------
// KEY SAMPLE CODE STARTS HERE
// Use the following namespace for ComputerVisionClient.
// -----------------------------------------------------------------------
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
// -----------------------------------------------------------------------
// KEY SAMPLE CODE ENDS HERE
// -----------------------------------------------------------------------

在探索计算机视觉示例应用附带场景时,将详细了解 ComputerVisionClient 类中包含的各种方法。You'll learn more about the various methods included with the ComputerVisionClient class as you explore the scenarios included with the Computer Vision sample app.

探索场景:分析图像Explore the Analyze Image scenario

此场景由 AnalyzePage.xaml 页面管理。This scenario is managed by the AnalyzePage.xaml page. 可以选择视觉特征和语言进行分析,并查看图像和结果。You can choose the visual features and language for the analysis, and see both the image and the results. 场景页面通过使用以下方法之一来执行此操作,具体取决于图像的来源:The scenario page does this by using one of the following methods, depending on the source of the image:

  • UploadAndAnalyzeImageAsyncUploadAndAnalyzeImageAsync
    此方法用于本地图像,其中图像必须编码为 Stream 并通过调用 ComputerVisionClient.AnalyzeImageInStreamAsync 方法将其发送到计算机视觉。This method is used for local images, in which the image must be encoded as a Stream and sent to Computer Vision by calling the ComputerVisionClient.AnalyzeImageInStreamAsync method.
  • AnalyzeUrlAsyncAnalyzeUrlAsync
    此方法用于远程图像,通过调用 ComputerVisionClient.AnalyzeImageAsync 方法将图像的 URL 发送到计算机视觉。This method is used for remote images, in which the URL for the image is sent to Computer Vision by calling the ComputerVisionClient.AnalyzeImageAsync method.

UploadAndAnalyzeImageAsync 方法使用指定的订阅密钥和终结点 URL 创建新的 ComputerVisionClient 实例。The UploadAndAnalyzeImageAsync method creates a new ComputerVisionClient instance, using the specified subscription key and endpoint URL. 因为示例应用要分析本地图像,所以必须将该图像的内容发送到计算机视觉。Because the sample app is analyzing a local image, it has to send the contents of that image to Computer Vision. 它会打开 imageFilePath 中指定的本地文件,以 Stream 格式读取,然后获取在场景页面中选择的视觉特征和语言。It opens the local file specified in imageFilePath for reading as a Stream, then gets the visual features and language selected in the scenario page. 它将调用 ComputerVisionClient.AnalyzeImageInStreamAsync 方法,传递文件的 Stream、视觉特征和语言,然后将结果作为 ImageAnalysis 实例返回。It calls the ComputerVisionClient.AnalyzeImageInStreamAsync method, passing the Stream for the file, the visual features, and the language, then returns the result as an ImageAnalysis instance. ImageScenarioPage 类继承的方法会在场景页面中显示返回的结果。The methods inherited from the ImageScenarioPage class present the returned results in the scenario page.

AnalyzeUrlAsync 方法使用指定的订阅密钥和终结点 URL 创建新的 ComputerVisionClient 实例。The AnalyzeUrlAsync method creates a new ComputerVisionClient instance, using the specified subscription key and endpoint URL. 它获取场景页面中选择的视觉特征和语言。It gets the visual features and language selected in the scenario page. 它将调用 ComputerVisionClient.AnalyzeImageInStreamAsync 方法,传递图像 URL、视觉特征和语言,然后将结果作为 ImageAnalysis 实例返回。It calls the ComputerVisionClient.AnalyzeImageInStreamAsync method, passing the image URL, the visual features, and the language, then returns the result as an ImageAnalysis instance. ImageScenarioPage 类继承的方法会在场景页面中显示返回的结果。The methods inherited from the ImageScenarioPage class present the returned results in the scenario page.

探索场景:使用域模型分析图像Explore the Analyze Image with Domain Model scenario

此场景由 AnalyzeInDomainPage.xaml 页面管理。This scenario is managed by the AnalyzeInDomainPage.xaml page. 可以选择域模型(例如 celebritieslandmarks)和语言来执行特定于域的图像分析,并查看图像和结果。You can choose a domain model, such as celebrities or landmarks, and language to perform a domain-specific analysis of the image, and see both the image and the results. 该场景页面使用以下方法,具体取决于图像的来源:The scenario page uses the following methods, depending on the source of the image:

  • GetAvailableDomainModelsAsyncGetAvailableDomainModelsAsync
    此方法从计算机视觉获取可用的域模型列表,并使用 ComputerVisionClient.ListModelsAsync 方法填充页面上的 _domainModelComboBox ComboBox 控件。This method gets the list of available domain models from Computer Vision and populates the _domainModelComboBox ComboBox control on the page, using the ComputerVisionClient.ListModelsAsync method.
  • UploadAndAnalyzeInDomainImageAsyncUploadAndAnalyzeInDomainImageAsync
    此方法用于本地图像,其中图像必须编码为 Stream 并通过调用 ComputerVisionClient.AnalyzeImageByDomainInStreamAsync 方法将其发送到计算机视觉。This method is used for local images, in which the image must be encoded as a Stream and sent to Computer Vision by calling the ComputerVisionClient.AnalyzeImageByDomainInStreamAsync method.
  • AnalyzeInDomainUrlAsyncAnalyzeInDomainUrlAsync
    此方法用于远程图像,通过调用 ComputerVisionClient.AnalyzeImageByDomainAsync 方法将图像的 URL 发送到计算机视觉。This method is used for remote images, in which the URL for the image is sent to Computer Vision by calling the ComputerVisionClient.AnalyzeImageByDomainAsync method.

UploadAndAnalyzeInDomainImageAsync 方法使用指定的订阅密钥和终结点 URL 创建新的 ComputerVisionClient 实例。The UploadAndAnalyzeInDomainImageAsync method creates a new ComputerVisionClient instance, using the specified subscription key and endpoint URL. 因为示例应用要分析本地图像,所以必须将该图像的内容发送到计算机视觉。Because the sample app is analyzing a local image, it has to send the contents of that image to Computer Vision. 它会打开 imageFilePath 中指定的本地文件,以 Stream 格式读取,然后获取在场景页面中选择的语言。It opens the local file specified in imageFilePath for reading as a Stream, then gets the language selected in the scenario page. 它将调用 ComputerVisionClient.AnalyzeImageByDomainInStreamAsync 方法,传递文件的 Stream、域模型的名称和语言,然后将结果作为 DomainModelResults 实例返回。It calls the ComputerVisionClient.AnalyzeImageByDomainInStreamAsync method, passing the Stream for the file, the name of the domain model, and the language, then returns the result as an DomainModelResults instance. ImageScenarioPage 类继承的方法会在场景页面中显示返回的结果。The methods inherited from the ImageScenarioPage class present the returned results in the scenario page.

AnalyzeInDomainUrlAsync 方法使用指定的订阅密钥和终结点 URL 创建新的 ComputerVisionClient 实例。The AnalyzeInDomainUrlAsync method creates a new ComputerVisionClient instance, using the specified subscription key and endpoint URL. 它获取场景页面中选择的语言。It gets the language selected in the scenario page. 它将调用 ComputerVisionClient.AnalyzeImageByDomainAsync 方法,传递图像 URL、视觉特征和语言,然后将结果作为 DomainModelResults 实例返回。It calls the ComputerVisionClient.AnalyzeImageByDomainAsync method, passing the image URL, the visual features, and the language, then returns the result as an DomainModelResults instance. ImageScenarioPage 类继承的方法会在场景页面中显示返回的结果。The methods inherited from the ImageScenarioPage class present the returned results in the scenario page.

探索场景:描述图像Explore the Describe Image scenario

此场景由 DescribePage.xaml 页面管理。This scenario is managed by the DescribePage.xaml page. 可以选择一种语言来创建图像的用户可读的描述,并查看图像和结果。You can choose a language to create a human-readable description of the image, and see both the image and the results. 该场景页面使用以下方法,具体取决于图像的来源:The scenario page uses the following methods, depending on the source of the image:

  • UploadAndDescribeImageAsyncUploadAndDescribeImageAsync
    此方法用于本地图像,其中图像必须编码为 Stream 并通过调用 ComputerVisionClient.DescribeImageInStreamAsync 方法将其发送到计算机视觉。This method is used for local images, in which the image must be encoded as a Stream and sent to Computer Vision by calling the ComputerVisionClient.DescribeImageInStreamAsync method.
  • DescribeUrlAsyncDescribeUrlAsync
    此方法用于远程图像,通过调用 ComputerVisionClient.DescribeImageAsync 方法将图像的 URL 发送到计算机视觉。This method is used for remote images, in which the URL for the image is sent to Computer Vision by calling the ComputerVisionClient.DescribeImageAsync method.

UploadAndDescribeImageAsync 方法使用指定的订阅密钥和终结点 URL 创建新的 ComputerVisionClient 实例。The UploadAndDescribeImageAsync method creates a new ComputerVisionClient instance, using the specified subscription key and endpoint URL. 因为示例应用要分析本地图像,所以必须将该图像的内容发送到计算机视觉。Because the sample app is analyzing a local image, it has to send the contents of that image to Computer Vision. 它会打开 imageFilePath 中指定的本地文件,以 Stream 格式读取,然后获取在场景页面中选择的语言。It opens the local file specified in imageFilePath for reading as a Stream, then gets the language selected in the scenario page. 它将调用 ComputerVisionClient.DescribeImageInStreamAsync 方法,传递文件的 Stream、候选项的最大数量(在本例中为 3)和语言,然后将结果作为 ImageDescription 实例返回。It calls the ComputerVisionClient.DescribeImageInStreamAsync method, passing the Stream for the file, the maximum number of candidates (in this case, 3), and the language, then returns the result as an ImageDescription instance. ImageScenarioPage 类继承的方法会在场景页面中显示返回的结果。The methods inherited from the ImageScenarioPage class present the returned results in the scenario page.

DescribeUrlAsync 方法使用指定的订阅密钥和终结点 URL 创建新的 ComputerVisionClient 实例。The DescribeUrlAsync method creates a new ComputerVisionClient instance, using the specified subscription key and endpoint URL. 它获取场景页面中选择的语言。It gets the language selected in the scenario page. 它将调用 ComputerVisionClient.DescribeImageAsync 方法,传递图像 URL、候选项的最大数量(在本例中为 3)和语言,然后将结果作为 ImageDescription 实例返回。It calls the ComputerVisionClient.DescribeImageAsync method, passing the image URL, the maximum number of candidates (in this case, 3), and the language, then returns the result as an ImageDescription instance. ImageScenarioPage 类继承的方法会在场景页面中显示返回的结果。The methods inherited from the ImageScenarioPage class present the returned results in the scenario page.

探索场景:生成标记Explore the Generate Tags scenario

此场景由 TagsPage.xaml 页面管理。This scenario is managed by the TagsPage.xaml page. 可以选择一种语言来标记图像的视觉特征,并查看图像和结果。You can choose a language to tag the visual features of an image, and see both the image and the results. 该场景页面使用以下方法,具体取决于图像的来源:The scenario page uses the following methods, depending on the source of the image:

  • UploadAndGetTagsForImageAsyncUploadAndGetTagsForImageAsync
    此方法用于本地图像,其中图像必须编码为 Stream 并通过调用 ComputerVisionClient.TagImageInStreamAsync 方法将其发送到计算机视觉。This method is used for local images, in which the image must be encoded as a Stream and sent to Computer Vision by calling the ComputerVisionClient.TagImageInStreamAsync method.
  • GenerateTagsForUrlAsyncGenerateTagsForUrlAsync
    此方法用于远程图像,通过调用 ComputerVisionClient.TagImageAsync 方法将图像的 URL 发送到计算机视觉。This method is used for remote images, in which the URL for the image is sent to Computer Vision by calling the ComputerVisionClient.TagImageAsync method.

UploadAndGetTagsForImageAsync 方法使用指定的订阅密钥和终结点 URL 创建新的 ComputerVisionClient 实例。The UploadAndGetTagsForImageAsync method creates a new ComputerVisionClient instance, using the specified subscription key and endpoint URL. 因为示例应用要分析本地图像,所以必须将该图像的内容发送到计算机视觉。Because the sample app is analyzing a local image, it has to send the contents of that image to Computer Vision. 它会打开 imageFilePath 中指定的本地文件,以 Stream 格式读取,然后获取在场景页面中选择的语言。It opens the local file specified in imageFilePath for reading as a Stream, then gets the language selected in the scenario page. 它将调用 ComputerVisionClient.TagImageInStreamAsync 方法,为传递文件的 Stream 和语言,然后将结果作为 TagResult 实例返回。It calls the ComputerVisionClient.TagImageInStreamAsync method, passing the Stream for the file and the language, then returns the result as a TagResult instance. ImageScenarioPage 类继承的方法会在场景页面中显示返回的结果。The methods inherited from the ImageScenarioPage class present the returned results in the scenario page.

GenerateTagsForUrlAsync 方法使用指定的订阅密钥和终结点 URL 创建新的 ComputerVisionClient 实例。The GenerateTagsForUrlAsync method creates a new ComputerVisionClient instance, using the specified subscription key and endpoint URL. 它获取场景页面中选择的语言。It gets the language selected in the scenario page. 它调用 ComputerVisionClient.TagImageAsync 方法,传递图像 URL 和语言,然后将结果作为 TagResult 实例返回。It calls the ComputerVisionClient.TagImageAsync method, passing the image URL and the language, then returns the result as a TagResult instance. ImageScenarioPage 类继承的方法会在场景页面中显示返回的结果。The methods inherited from the ImageScenarioPage class present the returned results in the scenario page.

探索场景:识别文本 (OCR)Explore the Recognize Text (OCR) scenario

此场景由 OCRPage.xaml 页面管理。This scenario is managed by the OCRPage.xaml page. 可以选择一种语言来识别和提取图像中的打印文本,并查看图像和结果。You can choose a language to recognize and extract printed text from an image, and see both the image and the results. 该场景页面使用以下方法,具体取决于图像的来源:The scenario page uses the following methods, depending on the source of the image:

  • UploadAndRecognizeImageAsyncUploadAndRecognizeImageAsync
    此方法用于本地图像,其中图像必须编码为 Stream 并通过调用 ComputerVisionClient.RecognizePrintedTextInStreamAsync 方法将其发送到计算机视觉。This method is used for local images, in which the image must be encoded as a Stream and sent to Computer Vision by calling the ComputerVisionClient.RecognizePrintedTextInStreamAsync method.
  • RecognizeUrlAsyncRecognizeUrlAsync
    此方法用于远程图像,通过调用 ComputerVisionClient.RecognizePrintedTextAsync 方法将图像的 URL 发送到计算机视觉。This method is used for remote images, in which the URL for the image is sent to Computer Vision by calling the ComputerVisionClient.RecognizePrintedTextAsync method.

UploadAndRecognizeImageAsync 方法使用指定的订阅密钥和终结点 URL 创建新的 ComputerVisionClient 实例。The UploadAndRecognizeImageAsync method creates a new ComputerVisionClient instance, using the specified subscription key and endpoint URL. 因为示例应用要分析本地图像,所以必须将该图像的内容发送到计算机视觉。Because the sample app is analyzing a local image, it has to send the contents of that image to Computer Vision. 它会打开 imageFilePath 中指定的本地文件,以 Stream 格式读取,然后获取在场景页面中选择的语言。It opens the local file specified in imageFilePath for reading as a Stream, then gets the language selected in the scenario page. 它将调用 ComputerVisionClient.RecognizePrintedTextInStreamAsync 方法,指示未检测到方向并传递文件的 Stream 和语言,然后将结果作为 OcrResult 实例返回。It calls the ComputerVisionClient.RecognizePrintedTextInStreamAsync method, indicating that orientation is not detected and passing the Stream for the file and the language, then returns the result as an OcrResult instance. ImageScenarioPage 类继承的方法会在场景页面中显示返回的结果。The methods inherited from the ImageScenarioPage class present the returned results in the scenario page.

RecognizeUrlAsync 方法使用指定的订阅密钥和终结点 URL 创建新的 ComputerVisionClient 实例。The RecognizeUrlAsync method creates a new ComputerVisionClient instance, using the specified subscription key and endpoint URL. 它获取场景页面中选择的语言。It gets the language selected in the scenario page. 它将调用 ComputerVisionClient.RecognizePrintedTextAsync 方法,指示未检测到方向并传递图像 URL 和语言,然后将结果作为 OcrResult 实例返回。It calls the ComputerVisionClient.RecognizePrintedTextAsync method, indicating that orientation is not detected and passing the image URL and the language, then returns the result as an OcrResult instance. ImageScenarioPage 类继承的方法会在场景页面中显示返回的结果。The methods inherited from the ImageScenarioPage class present the returned results in the scenario page.

探索场景:识别文本 V2(英语)Explore the Recognize Text V2 (English) scenario

此场景由 TextRecognitionPage.xaml 页面管理。This scenario is managed by the TextRecognitionPage.xaml page. 可以选择识别模式和语言,以异步方式识别和提取图像中的打印或手写文本,并查看图像和结果。You can choose the recognition mode and a language to asynchronously recognize and extract either printed or handwritten text from an image, and see both the image and the results. 该场景页面使用以下方法,具体取决于图像的来源:The scenario page uses the following methods, depending on the source of the image:

  • UploadAndRecognizeImageAsyncUploadAndRecognizeImageAsync
    此方法用于本地图像,其中图像必须编码为 Stream,通过调用 RecognizeAsync 方法并传递 ComputerVisionClient.RecognizeTextInStreamAsync 方法的参数化委托将其发送至计算机视觉。This method is used for local images, in which the image must be encoded as a Stream and sent to Computer Vision by calling the RecognizeAsync method and passing a parameterized delegate for the ComputerVisionClient.RecognizeTextInStreamAsync method.
  • RecognizeUrlAsyncRecognizeUrlAsync
    此方法用于远程图像,通过调用 RecognizeAsync 方法并传递 ComputerVisionClient.RecognizeTextAsync 方法的参数化委托将图像的 URL 发送到计算机视觉。This method is used for remote images, in which the URL for the image is sent to Computer Vision by calling the RecognizeAsync method and passing a parameterized delegate for the ComputerVisionClient.RecognizeTextAsync method.
  • RecognizeAsync 方法处理 UploadAndRecognizeImageAsyncRecognizeUrlAsync 方法的异步调用,并通过调用 ComputerVisionClient.GetTextOperationResultAsync 方法轮询结果。RecognizeAsync This method handles the asynchronous calling for both the UploadAndRecognizeImageAsync and RecognizeUrlAsync methods, as well as polling for results by calling the ComputerVisionClient.GetTextOperationResultAsync method.

与计算机视觉示例应用中包含的其他场景不同,此场景是异步的,它会调用一个方法来启动该进程,然后调用另一个方法来检查状态并返回该进程的结果。Unlike the other scenarios included in the Computer Vision sample app, this scenario is asynchronous, in that one method is called to start the process, but a different method is called to check on the status and return the results of that process. 此场景中的逻辑流与其他场景中的逻辑流略有不同。The logical flow in this scenario is somewhat different from that in the other scenarios.

UploadAndRecognizeImageAsync 方法打开 imageFilePath 中指定的本地文件,以 Stream 格式读取,然后调用 RecognizeAsync 方法,传递以下内容:The UploadAndRecognizeImageAsync method opens the local file specified in imageFilePath for reading as a Stream, then calls the RecognizeAsync method, passing:

  • ComputerVisionClient.RecognizeTextInStreamAsync 方法的参数化异步委托的 Lambda 表达式,其中将文件的 Stream 和识别模式作为 GetHeadersAsyncFunc 中的参数。A lambda expression for a parameterized asynchronous delegate of the ComputerVisionClient.RecognizeTextInStreamAsync method, with the Stream for the file and the recognition mode as parameters, in GetHeadersAsyncFunc.
  • 委托的 Lambda表达式,用于获取 GetOperationUrlFunc 中的 Operation-Location 响应标头值。A lambda expression for a delegate to get the Operation-Location response header value, in GetOperationUrlFunc.

RecognizeUrlAsync 方法调用 RecognizeAsync 方法,传递以下内容:The RecognizeUrlAsync method calls the RecognizeAsync method, passing:

  • ComputerVisionClient.RecognizeTextAsync 方法的参数化异步委托的 Lambda 表达式,其中将远程文件的 URL 和识别模式作为 GetHeadersAsyncFunc 中的参数。A lambda expression for a parameterized asynchronous delegate of the ComputerVisionClient.RecognizeTextAsync method, with the URL of the remote image and the recognition mode as parameters, in GetHeadersAsyncFunc.
  • 委托的 Lambda表达式,用于获取 GetOperationUrlFunc 中的 Operation-Location 响应标头值。A lambda expression for a delegate to get the Operation-Location response header value, in GetOperationUrlFunc.

完成 RecognizeAsync 方法后,UploadAndRecognizeImageAsyncRecognizeUrlAsync 方法都将结果作为 TextOperationResult 实例返回。When the RecognizeAsync method is completed, both UploadAndRecognizeImageAsync and RecognizeUrlAsync methods return the result as a TextOperationResult instance. ImageScenarioPage 类继承的方法会在场景页面中显示返回的结果。The methods inherited from the ImageScenarioPage class present the returned results in the scenario page.

RecognizeAsync 方法为 GetHeadersAsyncFunc 中传递的 ComputerVisionClient.RecognizeTextInStreamAsyncComputerVisionClient.RecognizeTextAsync 方法调用参数化委托,并等待响应。The RecognizeAsync method calls the parameterized delegate for either the ComputerVisionClient.RecognizeTextInStreamAsync or ComputerVisionClient.RecognizeTextAsync method passed in GetHeadersAsyncFunc and waits for the response. 该方法随后调用 GetOperationUrlFunc 中传递的委托,以从响应中获取 Operation-Location 响应标头值。The method then calls the delegate passed in GetOperationUrlFunc to get the Operation-Location response header value from the response. 此值是一个 URL,用于从计算机视觉检索 GetHeadersAsyncFunc 中传递的方法的结果。This value is the URL used to retrieve the results of the method passed in GetHeadersAsyncFunc from Computer Vision.

然后,RecognizeAsync 方法调用 ComputerVisionClient.GetTextOperationResultAsync 方法,传递从 Operation-Location 响应标头中检索到的 URL,以获取在 GetHeadersAsyncFunc 中传递的方法的状态和结果。The RecognizeAsync method then calls the ComputerVisionClient.GetTextOperationResultAsync method, passing the URL retrieved from the Operation-Location response header, to get the status and result of the method passed in GetHeadersAsyncFunc. 如果状态未指示方法是否已成功完成,则 RecognizeAsync 方法将再调用 ComputerVisionClient.GetTextOperationResultAsync 3 次,每次调用之间会等待 3 秒。If the status doesn't indicate that the method completed, successfully or unsuccessfully, the RecognizeAsync method calls ComputerVisionClient.GetTextOperationResultAsync 3 more times, waiting 3 seconds between calls. RecognizeAsync 方法将结果返回给调用它的方法。The RecognizeAsync method returns the results to the method that called it.

探索场景:获取缩略图Explore the Get Thumbnail scenario

此场景由 ThumbnailPage.xaml 页面管理。This scenario is managed by the ThumbnailPage.xaml page. 可以指示是否使用智能裁剪,并指定所需的高度和宽度,从图像生成缩略图,并查看图像和结果。You can indicate whether to use smart cropping, and specify desired height and width, to generate a thumbnail from an image, and see both the image and the results. 该场景页面使用以下方法,具体取决于图像的来源:The scenario page uses the following methods, depending on the source of the image:

  • UploadAndThumbnailImageAsyncUploadAndThumbnailImageAsync
    此方法用于本地图像,其中图像必须编码为 Stream 并通过调用 ComputerVisionClient.GenerateThumbnailInStreamAsync 方法将其发送到计算机视觉。This method is used for local images, in which the image must be encoded as a Stream and sent to Computer Vision by calling the ComputerVisionClient.GenerateThumbnailInStreamAsync method.
  • ThumbnailUrlAsyncThumbnailUrlAsync
    此方法用于远程图像,通过调用 ComputerVisionClient.GenerateThumbnailAsync 方法将图像的 URL 发送到计算机视觉。This method is used for remote images, in which the URL for the image is sent to Computer Vision by calling the ComputerVisionClient.GenerateThumbnailAsync method.

UploadAndThumbnailImageAsync 方法使用指定的订阅密钥和终结点 URL 创建新的 ComputerVisionClient 实例。The UploadAndThumbnailImageAsync method creates a new ComputerVisionClient instance, using the specified subscription key and endpoint URL. 因为示例应用要分析本地图像,所以必须将该图像的内容发送到计算机视觉。Because the sample app is analyzing a local image, it has to send the contents of that image to Computer Vision. 它将打开 imageFilePath 中指定的本地文件,以 Stream 格式读取。It opens the local file specified in imageFilePath for reading as a Stream. 它将调用 ComputerVisionClient.GenerateThumbnailInStreamAsync 方法,传递宽度、高度、文件的 Stream 以及是否使用智能裁剪,然后以 Stream 格式返回结果。It calls the ComputerVisionClient.GenerateThumbnailInStreamAsync method, passing the width, height, the Stream for the file, and whether to use smart cropping, then returns the result as a Stream. ImageScenarioPage 类继承的方法会在场景页面中显示返回的结果。The methods inherited from the ImageScenarioPage class present the returned results in the scenario page.

RecognizeUrlAsync 方法使用指定的订阅密钥和终结点 URL 创建新的 ComputerVisionClient 实例。The RecognizeUrlAsync method creates a new ComputerVisionClient instance, using the specified subscription key and endpoint URL. 它将调用 ComputerVisionClient.GenerateThumbnailAsync 方法,传递图像的宽度、高度、URL 以及是否使用智能裁剪,然后以 Stream 格式返回结果。It calls the ComputerVisionClient.GenerateThumbnailAsync method, passing the width, height, the URL for the image, and whether to use smart cropping, then returns the result as a Stream. ImageScenarioPage 类继承的方法会在场景页面中显示返回的结果。The methods inherited from the ImageScenarioPage class present the returned results in the scenario page.

清理资源Clean up resources

若不再需要资源,请删除克隆到 Microsoft/Cognitive-Vision-Windows 存储库的文件夹。When no longer needed, delete the folder into which you cloned the Microsoft/Cognitive-Vision-Windows repository. 若选择使用示例图像,则还要删除向其克隆 Microsoft/Cognitive-Face-Windows 存储库的文件夹。If you opted to use the sample images, also delete the folder into which you cloned the Microsoft/Cognitive-Face-Windows repository.

后续步骤Next steps