快速入门:使用计算机视觉客户端库Quickstart: Use the Computer Vision client library

开始使用计算机视觉客户端库。Get started with the Computer Vision client library. 请按照以下步骤安装程序包并试用基本任务的示例代码。Follow these steps to install the package and try out the example code for basic tasks. 使用计算机视觉可以访问用于处理图像并返回信息的高级算法Computer Vision provides you with access to advanced algorithms for processing images and returning information

使用计算机视觉客户端库可以:Use the Computer Vision client library to:

  • 分析图像中的标记、文本说明、人脸、成人内容,等等。Analyze an image for tags, text description, faces, adult content, and more.
  • 使用批量读取 API 识别印刷体文本和手写文本。Recognize printed and handwritten text with the Batch Read API.

参考文档 | 库源代码 | 包 (NuGet) | 示例Reference documentation | Library source code | Package (NuGet) | Samples

先决条件Prerequisites

设置Setting up

创建计算机视觉 Azure 资源Create a Computer Vision Azure resource

Azure 认知服务由你订阅的 Azure 资源表示。Azure Cognitive Services are represented by Azure resources that you subscribe to. 在本地计算机上使用 Azure 门户Azure CLI 创建计算机视觉的资源。Create a resource for Computer Vision using the Azure portal or Azure CLI on your local machine. 也可执行以下操作:You can also:

获取试用订阅或资源的密钥后,请为该密钥和终结点 URL 创建环境变量,分别名为 COMPUTER_VISION_SUBSCRIPTION_KEYCOMPUTER_VISION_ENDPOINTAfter you get a key from your trial subscription or resource, create environment variables for the key and endpoint URL, named COMPUTER_VISION_SUBSCRIPTION_KEY and COMPUTER_VISION_ENDPOINT, respectively.

新建 C# 应用程序Create a new C# application

在首选编辑器或 IDE 中创建新的 .NET Core 应用程序。Create a new .NET Core application in your preferred editor or IDE.

在控制台窗口(例如 cmd、PowerShell 或 Bash)中,使用 dotnet new 命令创建名为 computer-vision-quickstart 的新控制台应用。In a console window (such as cmd, PowerShell, or Bash), use the dotnet new command to create a new console app with the name computer-vision-quickstart. 此命令将创建包含单个源文件的简单“Hello World”C# 项目:ComputerVisionQuickstart.cs。This command creates a simple "Hello World" C# project with a single source file: ComputerVisionQuickstart.cs.

dotnet new console -n computer-vision-quickstart

将目录更改为新创建的应用文件夹。Change your directory to the newly created app folder. 可使用以下代码生成应用程序:You can build the application with:

dotnet build

生成输出不应包含警告或错误。The build output should contain no warnings or errors.

...
Build succeeded.
 0 Warning(s)
 0 Error(s)
...

在你喜欢使用的编辑器或 IDE 中,打开项目目录中的 ComputerVisionQuickstart.cs 文件。From the project directory, open the ComputerVisionQuickstart.cs file in your preferred editor or IDE. 然后,添加以下 using 指令:Add the following using directives:

using System;
using System.Collections.Generic;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

在应用程序的 Program 类中,为资源的 Azure 终结点和密钥创建变量。In the application's Program class, create variables for your resource's Azure endpoint and key.

// Add your Computer Vision subscription key and endpoint to your environment variables. 
// Close/reopen your project for them to take effect.
static string subscriptionKey = Environment.GetEnvironmentVariable("COMPUTER_VISION_SUBSCRIPTION_KEY");
static string endpoint = Environment.GetEnvironmentVariable("COMPUTER_VISION_ENDPOINT");

安装客户端库Install the client library

在应用程序目录中,使用以下命令安装适用于 .NET 的计算机视觉客户端库:Within the application directory, install the Computer Vision client library for .NET with the following command:

dotnet add package Microsoft.Azure.CognitiveServices.Vision.ComputerVision --version 5.0.0

如果你使用的是 Visual Studio IDE,客户端库可用作可下载的 NuGet 包。If you're using the Visual Studio IDE, the client library is available as a downloadable NuGet package.

对象模型Object model

以下类和接口将处理计算机视觉 .NET SDK 的某些主要功能。The following classes and interfaces handle some of the major features of the Computer Vision .NET SDK.

名称Name 说明Description
ComputerVisionClientComputerVisionClient 所有计算机视觉功能都需要此类。This class is needed for all Computer Vision functionality. 可以使用订阅信息实例化此类,然后使用它来执行大多数图像操作。You instantiate it with your subscription information, and you use it to do most image operations.
ComputerVisionClientExtensionsComputerVisionClientExtensions 此类包含 ComputerVisionClient 的其他方法。This class contains additional methods for the ComputerVisionClient.
VisualFeatureTypesVisualFeatureTypes 此枚举定义可在标准分析操作中执行的不同类型的图像分析。This enum defines the different types of image analysis that can be done in a standard Analyze operation. 请根据需求指定一组 VisualFeatureTypes 值。You specify a set of VisualFeatureTypes values depending on your needs.

代码示例Code examples

这些代码片段演示如何使用适用于 .NET 的计算机视觉客户端库执行以下任务:These code snippets show you how to do the following tasks with the Computer Vision client library for .NET:

验证客户端Authenticate the client

备注

本快速入门假设已经为计算机视觉密钥和终结点(分别名为 COMPUTER_VISION_SUBSCRIPTION_KEYCOMPUTER_VISION_ENDPOINT创建了环境变量This quickstart assumes you've created environment variables for your Computer Vision key and endpoint, named COMPUTER_VISION_SUBSCRIPTION_KEY and COMPUTER_VISION_ENDPOINT respectively.

在新方法中,使用终结点和密钥实例化客户端。In a new method, instantiate a client with your endpoint and key. 使用密钥创建一个 ApiKeyServiceClientCredentials 对象,并在终结点中使用该对象创建一个 ComputerVisionClient 对象。Create a ApiKeyServiceClientCredentials object with your key, and use it with your endpoint to create a ComputerVisionClient object.

/*
 * AUTHENTICATE
 * Creates a Computer Vision client used by each example.
 */
public static ComputerVisionClient Authenticate(string endpoint, string key)
{
    ComputerVisionClient client =
      new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
      { Endpoint = endpoint };
    return client;
}

你可能想要在 Main 方法中调用此方法。You'll likely want to call this method in the Main method.

// Create a client
ComputerVisionClient client = Authenticate(endpoint, subscriptionKey);

分析图像Analyze an image

以下代码定义方法 AnalyzeImageUrl,该方法使用客户端对象分析远程图像并输出结果。The following code defines a method, AnalyzeImageUrl, which uses the client object to analyze a remote image and print the results. 该方法返回文本说明、分类、标记列表、检测到的人脸、成人内容标志、主颜色和图像类型。The method returns a text description, categorization, list of tags, detected faces, adult content flags, main colors, and image type.

Main 方法中添加方法调用。Add the method call in your Main method.

// Analyze an image to get features and other properties.
AnalyzeImageUrl(client, ANALYZE_URL_IMAGE).Wait();

设置测试图像Set up test image

在 Program 类中,保存对要分析的图像的 URL 的引用。In your Program class, save a reference to the URL of the image you want to analyze.

// URL image used for analyzing an image (image of puppy)
private const string ANALYZE_URL_IMAGE = "https://moderatorsampleimages.blob.core.windows.net/samples/sample16.png";

备注

还可以分析本地图像。You can also analyze a local image. 请参阅 GitHub 上的示例代码以了解涉及本地图像的方案。See the sample code on GitHub for scenarios involving local images.

指定视觉特性Specify visual features

定义新的图像分析方法。Define your new method for image analysis. 添加下面的代码,它指定要在分析中提取的视觉特征。Add the code below, which specifies visual features you'd like to extract in your analysis. 有关完整列表,请参阅 VisualFeatureTypes 枚举。See the VisualFeatureTypes enum for a complete list.

/* 
 * ANALYZE IMAGE - URL IMAGE
 * Analyze URL image. Extracts captions, categories, tags, objects, faces, racy/adult content,
 * brands, celebrities, landmarks, color scheme, and image types.
 */
public static async Task AnalyzeImageUrl(ComputerVisionClient client, string imageUrl)
{
    Console.WriteLine("----------------------------------------------------------");
    Console.WriteLine("ANALYZE IMAGE - URL");
    Console.WriteLine();

    // Creating a list that defines the features to be extracted from the image. 
    List<VisualFeatureTypes> features = new List<VisualFeatureTypes>()
{
  VisualFeatureTypes.Categories, VisualFeatureTypes.Description,
  VisualFeatureTypes.Faces, VisualFeatureTypes.ImageType,
  VisualFeatureTypes.Tags, VisualFeatureTypes.Adult,
  VisualFeatureTypes.Color, VisualFeatureTypes.Brands,
  VisualFeatureTypes.Objects
};

将以下任一代码块插入 AnalyzeImageUrl 方法中以实现其功能。Insert any of the following code blocks into your AnalyzeImageUrl method to implement their features. 请记得在末尾添加右括号。Remember to add a closing bracket at the end.

}

分析Analyze

AnalyzeImageAsync 方法将返回包含所有提取信息的 ImageAnalysis 对象。The AnalyzeImageAsync method returns an ImageAnalysis object that contains all of extracted information.

Console.WriteLine($"Analyzing the image {Path.GetFileName(imageUrl)}...");
Console.WriteLine();
// Analyze the URL image 
ImageAnalysis results = await client.AnalyzeImageAsync(imageUrl, features);

以下部分说明如何详细分析此信息。The following sections show how to parse this information in detail.

获取图像说明Get image description

下面的代码获取为图像生成的描述文字列表。The following code gets the list of generated captions for the image. 有关更多详细信息,请参阅描述图像See Describe images for more details.

// Sunmarizes the image content.
Console.WriteLine("Summary:");
foreach (var caption in results.Description.Captions)
{
    Console.WriteLine($"{caption.Text} with confidence {caption.Confidence}");
}
Console.WriteLine();

获取图像类别Get image category

下面的代码获取所检测到的图像类别。The following code gets the detected category of the image. 有关更多详细信息,请参阅对图像进行分类See Categorize images for more details.

// Display categories the image is divided into.
Console.WriteLine("Categories:");
foreach (var category in results.Categories)
{
    Console.WriteLine($"{category.Name} with confidence {category.Score}");
}
Console.WriteLine();

获取图像标记Get image tags

以下代码获取图像中检测到的标记集。The following code gets the set of detected tags in the image. 有关更多详细信息,请参阅内容标记See Content tags for more details.

// Image tags and their confidence score
Console.WriteLine("Tags:");
foreach (var tag in results.Tags)
{
    Console.WriteLine($"{tag.Name} {tag.Confidence}");
}
Console.WriteLine();

检测物体Detect objects

以下代码检测图像中的常见物体并将其输出到控制台。The following code detects common objects in the image and prints them to the console. 有关更多详细信息,请参阅物体检测See Object detection for more details.

// Objects
Console.WriteLine("Objects:");
foreach (var obj in results.Objects)
{
    Console.WriteLine($"{obj.ObjectProperty} with confidence {obj.Confidence} at location {obj.Rectangle.X}, " +
      $"{obj.Rectangle.X + obj.Rectangle.W}, {obj.Rectangle.Y}, {obj.Rectangle.Y + obj.Rectangle.H}");
}
Console.WriteLine();

检测品牌Detect brands

以下代码检测图像中的公司品牌和徽标,并将其输出到控制台。The following code detects corporate brands and logos in the image and prints them to the console. 有关更多详细信息,请参阅品牌检测See Brand detection for more details.

// Well-known (or custom, if set) brands.
Console.WriteLine("Brands:");
foreach (var brand in results.Brands)
{
    Console.WriteLine($"Logo of {brand.Name} with confidence {brand.Confidence} at location {brand.Rectangle.X}, " +
      $"{brand.Rectangle.X + brand.Rectangle.W}, {brand.Rectangle.Y}, {brand.Rectangle.Y + brand.Rectangle.H}");
}
Console.WriteLine();

检测人脸Detect faces

下面的代码返回图像中检测到的人脸及其矩形坐标,以及选择面属性。The following code returns the detected faces in the image with their rectangle coordinates and select face attributes. 有关更多详细信息,请参阅人脸检测See Face detection for more details.

// Faces
Console.WriteLine("Faces:");
foreach (var face in results.Faces)
{
    Console.WriteLine($"A {face.Gender} of age {face.Age} at location {face.FaceRectangle.Left}, " +
      $"{face.FaceRectangle.Left}, {face.FaceRectangle.Top + face.FaceRectangle.Width}, " +
      $"{face.FaceRectangle.Top + face.FaceRectangle.Height}");
}
Console.WriteLine();
``

### Detect adult, racy, or gory content

The following code prints the detected presence of adult content in the image. See [Adult, racy, gory content](../../concept-detecting-adult-content.md) for more details.

```csharp
// Adult or racy content, if any.
Console.WriteLine("Adult:");
Console.WriteLine($"Has adult content: {results.Adult.IsAdultContent} with confidence {results.Adult.AdultScore}");
Console.WriteLine($"Has racy content: {results.Adult.IsRacyContent} with confidence {results.Adult.RacyScore}");
Console.WriteLine();

获取图像配色方案Get image color scheme

以下代码输出图像中检测到的颜色属性,如主色和主题色。The following code prints the detected color attributes in the image, like the dominant colors and accent color. 有关更多详细信息,请参阅配色方案See Color schemes for more details.

// Identifies the color scheme.
Console.WriteLine("Color Scheme:");
Console.WriteLine("Is black and white?: " + results.Color.IsBWImg);
Console.WriteLine("Accent color: " + results.Color.AccentColor);
Console.WriteLine("Dominant background color: " + results.Color.DominantColorBackground);
Console.WriteLine("Dominant foreground color: " + results.Color.DominantColorForeground);
Console.WriteLine("Dominant colors: " + string.Join(",", results.Color.DominantColors));
Console.WriteLine();

获取特定于域的内容Get domain-specific content

计算机视觉可以使用专用模型对图像进行进一步分析。Computer Vision can use specialized models to do further analysis on images. 有关更多详细信息,请参阅特定于域的内容See Domain-specific content for more details.

以下代码分析了图像中检测到的名人的相关数据。The following code parses data about detected celebrities in the image.

// Celebrities in image, if any.
Console.WriteLine("Celebrities:");
foreach (var category in results.Categories)
{
    if (category.Detail?.Celebrities != null)
    {
        foreach (var celeb in category.Detail.Celebrities)
        {
            Console.WriteLine($"{celeb.Name} with confidence {celeb.Confidence} at location {celeb.FaceRectangle.Left}, " +
              $"{celeb.FaceRectangle.Top}, {celeb.FaceRectangle.Height}, {celeb.FaceRectangle.Width}");
        }
    }
}
Console.WriteLine();

以下代码分析了图像中检测到的地标的相关数据。The following code parses data about detected landmarks in the image.

// Popular landmarks in image, if any.
Console.WriteLine("Landmarks:");
foreach (var category in results.Categories)
{
    if (category.Detail?.Landmarks != null)
    {
        foreach (var landmark in category.Detail.Landmarks)
        {
            Console.WriteLine($"{landmark.Name} with confidence {landmark.Confidence}");
        }
    }
}
Console.WriteLine();

获取图像类型Get the image type

以下代码输出有关图像类型的信息—无论它是剪贴画还是线条图。The following code prints information about the type of image—whether it is clip art or a line drawing.

// Detects the image types.
Console.WriteLine("Image Type:");
Console.WriteLine("Clip Art Type: " + results.ImageType.ClipArtType);
Console.WriteLine("Line Drawing Type: " + results.ImageType.LineDrawingType);
Console.WriteLine();

读取印刷体文本和手写文本Read printed and handwritten text

计算机视觉可以读取图像中的可见文本,并将其转换为字符流。Computer Vision can read visible text in an image and convert it to a character stream. 本部分中的代码定义了方法 ExtractTextUrl,该方法使用客户端对象检测并提取图像中的印刷体文本或手写文本。The code in this section defines a method, ExtractTextUrl, which uses the client object to detect and extract printed or handwritten text in the image.

Main 方法中添加方法调用。Add the method call in your Main method.

// Read the batch text from an image (handwriting and/or printed).
BatchReadFileUrl(client, EXTRACT_TEXT_URL_IMAGE).Wait();
BatchReadFileLocal(client, EXTRACT_TEXT_LOCAL_IMAGE).Wait();

设置测试图像Set up test image

Program 类中,保存要从中提取文本的图像的 URL 的引用。In your Program class, save a reference the URL of the image you want to extract text from.

private const string EXTRACT_TEXT_URL_IMAGE = "https://raw.githubusercontent.com/MicrosoftDocs/azure-docs/master/articles/cognitive-services/Computer-vision/Images/readsample.jpg";
// URL image for OCR (optical character recognition).

备注

还可以从本地图像提取文本。You can also extract text from a local image. 请参阅 GitHub 上的示例代码以了解涉及本地图像的方案。See the sample code on GitHub for scenarios involving local images.

调用读取 APICall the Read API

定义用于读取文本的新方法。Define the new method for reading text. 添加以下代码,该代码对给定图像调用 BatchReadFileAsync 方法。Add the code below, which calls the BatchReadFileAsync method for the given image. 这会返回一个操作 ID 并启动异步进程来读取图像的内容。This returns an operation ID and starts an asynchronous process to read the content of the image.

/*
 * BATCH READ FILE - URL IMAGE
 * Recognizes handwritten text. 
 * This API call offers an improvement of results over the Recognize Text calls.
 */
public static async Task BatchReadFileUrl(ComputerVisionClient client, string urlImage)
{
    Console.WriteLine("----------------------------------------------------------");
    Console.WriteLine("BATCH READ FILE - URL IMAGE");
    Console.WriteLine();

    // Read text from URL
    BatchReadFileHeaders textHeaders = await client.BatchReadFileAsync(urlImage);
    // After the request, get the operation location (operation ID)
    string operationLocation = textHeaders.OperationLocation;

获取读取结果Get Read results

接下来,获取从 BatchReadFileAsync 调用返回的操作 ID,并使用它查询服务以获取操作结果。Next, get the operation ID returned from the BatchReadFileAsync call, and use it to query the service for operation results. 下面的代码每隔一秒钟检查一次操作,直到返回结果。The following code checks the operation at one-second intervals until the results are returned. 然后,它将提取的文本数据输出到控制台。It then prints the extracted text data to the console.

// Retrieve the URI where the recognized text will be stored from the Operation-Location header.
    // We only need the ID and not the full URL
    const int numberOfCharsInOperationId = 36;
    string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);

    // Extract the text
    // Delay is between iterations and tries a maximum of 10 times.
    int i = 0;
    int maxRetries = 10;
    ReadOperationResult results;
    Console.WriteLine($"Extracting text from URL image {Path.GetFileName(urlImage)}...");
    Console.WriteLine();
    do
    {
        results = await client.GetReadOperationResultAsync(operationId);
        Console.WriteLine("Server status: {0}, waiting {1} seconds...", results.Status, i);
        await Task.Delay(1000);
        if (i == 9) 
{ 
    Console.WriteLine("Server timed out."); 
}
    }
    while ((results.Status == TextOperationStatusCodes.Running ||
        results.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries);

显示读取结果Display Read results

添加以下代码来分析和显示检索到的文本数据,并完成方法定义。Add the following code to parse and display the retrieved text data, and finish the method definition.

// Display the found text.
    Console.WriteLine();
    var textRecognitionLocalFileResults = results.RecognitionResults;
    foreach (TextRecognitionResult recResult in textRecognitionLocalFileResults)
    {
        foreach (Line line in recResult.Lines)
        {
            Console.WriteLine(line.Text);
        }
    }
    Console.WriteLine();
}

运行应用程序Run the application

从应用程序目录使用 dotnet run 命令运行应用程序。Run the application from your application directory with the dotnet run command.

dotnet run

清理资源Clean up resources

如果想要清理并删除认知服务订阅,可以删除资源或资源组。If you want to clean up and remove a Cognitive Services subscription, you can delete the resource or resource group. 删除资源组同时也会删除与之相关联的任何其他资源。Deleting the resource group also deletes any other resources associated with it.

后续步骤Next steps

参考文档 | 项目 (Maven) | 示例Reference documentation | Artifact (Maven) | Samples

先决条件Prerequisites

设置Setting up

创建计算机视觉 Azure 资源Create a Computer Vision Azure resource

Azure 认知服务由你订阅的 Azure 资源表示。Azure Cognitive Services are represented by Azure resources that you subscribe to. 在本地计算机上使用 Azure 门户Azure CLI 创建计算机视觉的资源。Create a resource for Computer Vision using the Azure portal or Azure CLI on your local machine. 也可执行以下操作:You can also:

然后,为密钥和服务终结点字符串创建环境变量,分别名为 COMPUTER_VISION_SUBSCRIPTION_KEYCOMPUTER_VISION_ENDPOINTThen, create environment variables for the key and service endpoint string, named COMPUTER_VISION_SUBSCRIPTION_KEY and COMPUTER_VISION_ENDPOINT, respectively.

创建新的 Gradle 项目Create a new Gradle project

在控制台窗口(例如 cmd、PowerShell 或 Bash)中,为应用创建一个新目录并导航到该目录。In a console window (such as cmd, PowerShell, or Bash), create a new directory for your app, and navigate to it.

mkdir myapp && cd myapp

从工作目录运行 gradle init 命令。Run the gradle init command from your working directory. 此命令将创建 Gradle 的基本生成文件,包括 build.gradle.kts,在运行时将使用该文件创建并配置应用程序。This command will create essential build files for Gradle, including build.gradle.kts, which is used at runtime to create and configure your application.

gradle init --type basic

当提示你选择一个 DSL 时,选择 KotlinWhen prompted to choose a DSL, select Kotlin.

找到 build.gradle.kts,并使用喜好的 IDE 或文本编辑器将其打开。Locate build.gradle.kts and open it with your preferred IDE or text editor. 然后在该文件中复制以下生成配置。Then copy in the following build configuration. 此配置将项目定义一个 Java 应用程序,其入口点为 ComputerVisionQuickstarts 类。This configuration defines the project as a Java application whose entry point is the class ComputerVisionQuickstarts. 它将导入计算机视觉库。It imports the Computer Vision library.

plugins {
    java
    application
}
application { 
    mainClassName = "ComputerVisionQuickstarts"
}
repositories {
    mavenCentral()
}

从工作目录运行以下命令,以创建项目源文件夹:From your working directory, run the following command to create a project source folder:

mkdir -p src/main/java

导航到新文件夹,创建名为 ComputerVisionQuickstarts.java 的文件。Navigate to the new folder and create a file called ComputerVisionQuickstarts.java. 在喜好的编辑器或 IDE 中打开该文件并添加以下 import 语句:Open it in your preferred editor or IDE and add the following import statements:

import com.microsoft.azure.cognitiveservices.vision.computervision.*;
import com.microsoft.azure.cognitiveservices.vision.computervision.models.*;

import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Files;

import java.util.ArrayList;
import java.util.List;

然后添加 ComputerVisionQuickstarts 的类定义。Then add a class definition for ComputerVisionQuickstarts.

安装客户端库Install the client library

本快速入门使用 Gradle 依赖项管理器。This quickstart uses the Gradle dependency manager. 可以在 Maven 中央存储库中找到客户端库以及其他依赖项管理器的信息。You can find the client library and information for other dependency managers on the Maven Central Repository.

在项目的 build.gradle.kts 文件中,包含计算机视觉客户端库作为依赖项。In your project's build.gradle.kts file, include the Computer Vision client library as a dependency.

dependencies {
    compile(group = "com.microsoft.azure.cognitiveservices", name = "azure-cognitiveservices-computervision", version = "1.0.2-beta")
}

对象模型Object model

以下类和接口将处理人脸计算机视觉 Java SDK 的某些主要功能。The following classes and interfaces handle some of the major features of the Computer Vision Java SDK.

名称Name 说明Description
ComputerVisionClientComputerVisionClient 所有计算机视觉功能都需要此类。This class is needed for all Computer Vision functionality. 请使用你的订阅信息实例化此类,然后使用它来生成其他类的实例。You instantiate it with your subscription information, and you use it to produce instances of other classes.
ComputerVisionComputerVision 此类来自客户端对象,它直接处理所有图像操作,例如图像分析、文本检测和缩略图生成。This class comes from the client object and directly handles all of the image operations, such as image analysis, text detection, and thumbnail generation.
VisualFeatureTypesVisualFeatureTypes 此枚举定义可在标准分析操作中执行的不同类型的图像分析。This enum defines the different types of image analysis that can be done in a standard Analyze operation. 请根据需求指定一组 VisualFeatureTypes 值。You specify a set of VisualFeatureTypes values depending on your needs.

代码示例Code examples

这些代码片段演示如何使用适用于 Java 的计算机视觉客户端库执行以下任务:These code snippets show you how to do the following tasks with the Computer Vision client library for Java:

验证客户端Authenticate the client

备注

本快速入门假设你已为计算机视觉密钥创建了名为 COMPUTER_VISION_SUBSCRIPTION_KEY环境变量This quickstart assumes you've created an environment variable for your Computer Vision key, named COMPUTER_VISION_SUBSCRIPTION_KEY.

以下代码将 main 方法添加到类,并为资源的 Azure 终结点和密钥创建变量。The following code adds a main method to your class and creates variables for your resource's Azure endpoint and key. 你需要输入自己的终结点字符串,可以在 Azure 门户的概述部分找到该字符串。You'll need to enter your own endpoint string, which you can find by checking the Overview section of the Azure portal.

public static void main(String[] args) {
    // Add your Computer Vision subscription key and endpoint to your environment
    // variables.
    // After setting, close and then re-open your command shell or project for the
    // changes to take effect.
    String subscriptionKey = System.getenv("COMPUTER_VISION_SUBSCRIPTION_KEY");
    String endpoint = System.getenv("COMPUTER_VISION_ENDPOINT");

接下来添加以下代码,以创建 ComputerVisionClient 对象并将其传递给稍后要定义的其他方法。Next, add the following code to create a ComputerVisionClient object and passes it into other method(s), which you'll define later.

ComputerVisionClient compVisClient = ComputerVisionManager.authenticate(subscriptionKey).withEndpoint(endpoint);
// END - Create an authenticated Computer Vision client.

System.out.println("\nAzure Cognitive Services Computer Vision - Java Quickstart Sample");

// Analyze local and remote images
AnalyzeLocalImage(compVisClient);

// Recognize printed text with OCR for a local and remote (URL) image
RecognizeTextOCRLocal(compVisClient);

备注

如果在启动应用程序后创建了环境变量,则需要关闭再重新打开运行该应用程序的编辑器、IDE 或 shell 才能访问该变量。If you created the environment variable after you launched the application, you'll need to close and reopen the editor, IDE, or shell running it to access the variable.

分析图像Analyze an image

以下代码定义方法 AnalyzeLocalImage,该方法使用客户端对象分析本地图像并输出结果。The following code defines a method, AnalyzeLocalImage, which uses the client object to analyze a local image and print the results. 该方法返回文本说明、分类、标记列表、检测到的人脸、成人内容标志、主颜色和图像类型。The method returns a text description, categorization, list of tags, detected faces, adult content flags, main colors, and image type.

设置测试图像Set up test image

首先,在项目的 src/main/ 文件夹中创建 resources/ 文件夹,并添加要分析的图像。First, create a resources/ folder in the src/main/ folder of your project, and add an image you'd like to analyze. 然后,将以下方法定义添加到 ComputerVisionQuickstarts 类。Then add the following method definition to your ComputerVisionQuickstarts class. 如有必要,请更改 pathToLocalImage 的值,使之与图像文件相匹配。If necessary, change the value of the pathToLocalImage to match your image file.

public static void AnalyzeLocalImage(ComputerVisionClient compVisClient) {
    /*
     * Analyze a local image:
     *
     * Set a string variable equal to the path of a local image. The image path
     * below is a relative path.
     */
    String pathToLocalImage = "src\\main\\resources\\myImage.jpg";

备注

还可以使用其 URL 分析远程图像。You can also analyze a remote image using its URL. 请参阅 GitHub上的示例代码以了解涉及远程图像的方案。See the sample code on GitHub for scenarios involving remote images.

指定视觉特性Specify visual features

接下来,指定要在分析中提取的视觉特征。Next, specify which visual features you'd like to extract in your analysis. 有关完整列表,请参阅 VisualFeatureTypes 枚举。See the VisualFeatureTypes enum for a complete list.

// This list defines the features to be extracted from the image.
List<VisualFeatureTypes> featuresToExtractFromLocalImage = new ArrayList<>();
featuresToExtractFromLocalImage.add(VisualFeatureTypes.DESCRIPTION);
featuresToExtractFromLocalImage.add(VisualFeatureTypes.CATEGORIES);
featuresToExtractFromLocalImage.add(VisualFeatureTypes.TAGS);
featuresToExtractFromLocalImage.add(VisualFeatureTypes.FACES);
featuresToExtractFromLocalImage.add(VisualFeatureTypes.ADULT);
featuresToExtractFromLocalImage.add(VisualFeatureTypes.COLOR);
featuresToExtractFromLocalImage.add(VisualFeatureTypes.IMAGE_TYPE);

分析Analyze

此方法根据每个图像分析范围将详细结果输出到控制台。This method prints detailed results to the console for each scope of image analysis. 建议将此方法调用包含在 Try/Catch 块中。We recommend you surround this method call in a Try/Catch block. analyzeImageInStream 方法将返回包含所有提取信息的 ImageAnalysis 对象。The analyzeImageInStream method returns an ImageAnalysis object that contains all of extracted information.

// Need a byte array for analyzing a local image.
File rawImage = new File(pathToLocalImage);
byte[] imageByteArray = Files.readAllBytes(rawImage.toPath());

// Call the Computer Vision service and tell it to analyze the loaded image.
ImageAnalysis analysis = compVisClient.computerVision().analyzeImageInStream().withImage(imageByteArray)
        .withVisualFeatures(featuresToExtractFromLocalImage).execute();

以下部分说明如何详细分析此信息。The following sections show how to parse this information in detail.

获取图像说明Get image description

下面的代码获取为图像生成的描述文字列表。The following code gets the list of generated captions for the image. 有关详细信息,请参阅描述图像For more information, see Describe images.

// Display image captions and confidence values.
System.out.println("\nCaptions: ");
for (ImageCaption caption : analysis.description().captions()) {
    System.out.printf("\'%s\' with confidence %f\n", caption.text(), caption.confidence());
}

获取图像类别Get image category

下面的代码获取所检测到的图像类别。The following code gets the detected category of the image. 有关详细信息,请参阅对图像进行分类For more information, see Categorize images.

// Display image category names and confidence values.
System.out.println("\nCategories: ");
for (Category category : analysis.categories()) {
    System.out.printf("\'%s\' with confidence %f\n", category.name(), category.score());
}

获取图像标记Get image tags

以下代码获取图像中检测到的标记集。The following code gets the set of detected tags in the image. 有关详细信息,请参阅内容标记For more information, see Content tags.

// Display image tags and confidence values.
System.out.println("\nTags: ");
for (ImageTag tag : analysis.tags()) {
    System.out.printf("\'%s\' with confidence %f\n", tag.name(), tag.confidence());
}

检测人脸Detect faces

下面的代码返回图像中检测到的人脸及其矩形坐标,并选择人脸属性。The following code returns the detected faces in the image with their rectangle coordinates and selects face attributes. 有关详细信息,请参阅人脸检测For more information, see Face detection.

// Display any faces found in the image and their location.
System.out.println("\nFaces: ");
for (FaceDescription face : analysis.faces()) {
    System.out.printf("\'%s\' of age %d at location (%d, %d), (%d, %d)\n", face.gender(), face.age(),
            face.faceRectangle().left(), face.faceRectangle().top(),
            face.faceRectangle().left() + face.faceRectangle().width(),
            face.faceRectangle().top() + face.faceRectangle().height());
}

检测成人、色情或血腥内容Detect adult, racy, or gory content

以下代码输出图像中检测到的成人内容。The following code prints the detected presence of adult content in the image. 有关详细信息,请参阅成人、色情或血腥内容For more information, see Adult, racy, gory content.

// Display whether any adult or racy content was detected and the confidence
// values.
System.out.println("\nAdult: ");
System.out.printf("Is adult content: %b with confidence %f\n", analysis.adult().isAdultContent(),
        analysis.adult().adultScore());
System.out.printf("Has racy content: %b with confidence %f\n", analysis.adult().isRacyContent(),
        analysis.adult().racyScore());

获取图像配色方案Get image color scheme

以下代码输出图像中检测到的颜色属性,如主色和主题色。The following code prints the detected color attributes in the image, like the dominant colors and accent color. 有关详细信息,请参阅配色方案For more information, see Color schemes.

// Display the image color scheme.
System.out.println("\nColor scheme: ");
System.out.println("Is black and white: " + analysis.color().isBWImg());
System.out.println("Accent color: " + analysis.color().accentColor());
System.out.println("Dominant background color: " + analysis.color().dominantColorBackground());
System.out.println("Dominant foreground color: " + analysis.color().dominantColorForeground());
System.out.println("Dominant colors: " + String.join(", ", analysis.color().dominantColors()));

获取特定于域的内容Get domain-specific content

计算机视觉可以使用专用模型对图像进行进一步分析。Computer Vision can use specialized model to do further analysis on images. 有关详细信息,请参阅特定于域的内容For more information, see Domain-specific content.

以下代码分析了图像中检测到的名人的相关数据。The following code parses data about detected celebrities in the image.

// Display any celebrities detected in the image and their locations.
System.out.println("\nCelebrities: ");
for (Category category : analysis.categories()) {
    if (category.detail() != null && category.detail().celebrities() != null) {
        for (CelebritiesModel celeb : category.detail().celebrities()) {
            System.out.printf("\'%s\' with confidence %f at location (%d, %d), (%d, %d)\n", celeb.name(),
                    celeb.confidence(), celeb.faceRectangle().left(), celeb.faceRectangle().top(),
                    celeb.faceRectangle().left() + celeb.faceRectangle().width(),
                    celeb.faceRectangle().top() + celeb.faceRectangle().height());
        }
    }
}

以下代码分析了图像中检测到的地标的相关数据。The following code parses data about detected landmarks in the image.

// Display any landmarks detected in the image and their locations.
System.out.println("\nLandmarks: ");
for (Category category : analysis.categories()) {
    if (category.detail() != null && category.detail().landmarks() != null) {
        for (LandmarksModel landmark : category.detail().landmarks()) {
            System.out.printf("\'%s\' with confidence %f\n", landmark.name(), landmark.confidence());
        }
    }
}

获取图像类型Get the image type

以下代码输出有关图像类型的信息—无论它是剪贴画还是线条图。The following code prints information about the type of image—whether it is clip art or line drawing.

// Display what type of clip art or line drawing the image is.
System.out.println("\nImage type:");
System.out.println("Clip art type: " + analysis.imageType().clipArtType());
System.out.println("Line drawing type: " + analysis.imageType().lineDrawingType());

读取印刷体文本和手写文本Read printed and handwritten text

计算机视觉可以读取图像中的可见文本,并将其转换为字符流。Computer Vision can read visible text in an image and convert it to a character stream.

备注

还可以使用其 URL 读取远程图像中的文本。You can also read text in a remote image using its URL. 请参阅 GitHub上的示例代码以了解涉及远程图像的方案。See the sample code on GitHub for scenarios involving remote images.

调用识别 APICall the Recognize API

首先,使用以下代码对给定图像调用 recognizePrintedTextInStream 方法。First, use the following code to call the recognizePrintedTextInStream method for the given image. 将此代码添加到项目中时,需要将 localTextImagePath 的值替换为本地图像的路径。When you add this code to your project, you need to replace the value of localTextImagePath with the path to your local image.

/**
 * RECOGNIZE PRINTED TEXT: Displays text found in image with angle and orientation of
 * the block of text.
 */
private static void RecognizeTextOCRLocal(ComputerVisionClient client) {
    System.out.println("-----------------------------------------------");
    System.out.println("RECOGNIZE PRINTED TEXT");
    
    // Replace this string with the path to your own image.
    String localTextImagePath = "<local image path>";
    
    try {
        File rawImage = new File(localTextImagePath);
        byte[] localImageBytes = Files.readAllBytes(rawImage.toPath());

        // Recognize printed text in local image
        OcrResult ocrResultLocal = client.computerVision().recognizePrintedTextInStream()
                .withDetectOrientation(true).withImage(localImageBytes).withLanguage(OcrLanguages.EN).execute();

以下代码块处理返回的文本并对其进行分析,以输出每行的第一个单词。The following block of code processes the returned text and parses it to print out the first word in each line. 可以使用此代码快速了解 OcrResult 实例的结构。You can use this code to quickly understand the structure of an OcrResult instance.

// Print results of local image
System.out.println();
System.out.println("Recognizing printed text from a local image with OCR ...");
System.out.println("\nLanguage: " + ocrResultLocal.language());
System.out.printf("Text angle: %1.3f\n", ocrResultLocal.textAngle());
System.out.println("Orientation: " + ocrResultLocal.orientation());

boolean firstWord = true;
// Gets entire region of text block
for (OcrRegion reg : ocrResultLocal.regions()) {
    // Get one line in the text block
    for (OcrLine line : reg.lines()) {
        for (OcrWord word : line.words()) {
            // get bounding box of first word recognized (just to demo)
            if (firstWord) {
                System.out.println("\nFirst word in first line is \"" + word.text()
                        + "\" with  bounding box: " + word.boundingBox());
                firstWord = false;
                System.out.println();
            }
            System.out.print(word.text() + " ");
        }
        System.out.println();
    }
}

最后,结束 try/catch 块和方法定义。Finally, close out the try/catch block and the method definition.

} catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}

运行应用程序Run the application

可使用以下命令生成应用:You can build the app with:

gradle build

使用 gradle run 命令运行应用程序:Run the application with the gradle run command:

gradle run

清理资源Clean up resources

如果想要清理并删除认知服务订阅,可以删除资源或资源组。If you want to clean up and remove a Cognitive Services subscription, you can delete the resource or resource group. 删除资源组同时也会删除与之相关联的任何其他资源。Deleting the resource group also deletes any other resources associated with it.

后续步骤Next steps

本快速入门已介绍如何使用计算机视觉 Java 库执行基本任务。In this quickstart, you learned how to use the Computer Vision Java library to do basis tasks. 接下来,请在参考文档中详细了解该库。Next, explore the reference documentation to learn more about the library.

参考文档 | 库源代码 | 包 (npm) | 示例Reference documentation | Library source code | Package (npm) | Samples

先决条件Prerequisites

设置Setting up

创建计算机视觉 Azure 资源Create a Computer Vision Azure resource

Azure 认知服务由你订阅的 Azure 资源表示。Azure Cognitive Services are represented by Azure resources that you subscribe to. 在本地计算机上使用 Azure 门户Azure CLI 创建计算机视觉的资源。Create a resource for Computer Vision using the Azure portal or Azure CLI on your local machine. 也可执行以下操作:You can also:

获取试用订阅或资源的密钥后,请为该密钥和终结点 URL 创建环境变量,分别名为 COMPUTER_VISION_SUBSCRIPTION_KEYCOMPUTER_VISION_ENDPOINTAfter you get a key from your trial subscription or resource, create environment variables for the key and endpoint URL, named COMPUTER_VISION_SUBSCRIPTION_KEY and COMPUTER_VISION_ENDPOINT respectively.

创建新的 Node.js 应用程序Create a new Node.js application

在控制台窗口(例如 cmd、PowerShell 或 Bash)中,为应用创建一个新目录并导航到该目录。In a console window (such as cmd, PowerShell, or Bash), create a new directory for your app, and navigate to it.

mkdir myapp && cd myapp

运行 npm init 命令以使用 package.json 文件创建一个 node 应用程序。Run the npm init command to create a node application with a package.json file.

npm init

安装客户端库Install the client library

安装 ms-rest-azure@azure/cognitiveservices-computervision NPM 包:Install the ms-rest-azure and @azure/cognitiveservices-computervision NPM packages:

npm install @azure/cognitiveservices-computervision

应用的 package.json 文件将使用依赖项进行更新。Your app's package.json file will be updated with the dependencies.

准备 Node.js 脚本Prepare the Node.js script

创建新文件 index.js,将其在文本编辑器中打开。Create a new file, index.js, and open it in a text editor. 添加以下 import 语句。Add the following import statements.

'use strict';

const async = require('async');
const fs = require('fs');
const path = require("path");
const createReadStream = require('fs').createReadStream
const sleep = require('util').promisify(setTimeout);
const ComputerVisionClient = require('@azure/cognitiveservices-computervision').ComputerVisionClient;
const ApiKeyCredentials = require('@azure/ms-rest-js').ApiKeyCredentials;

然后定义函数 computerVision,并声明一个包含主函数和回调函数的异步系列。Then, define a function computerVision and declare an async series with primary function and callback function. 我们会将快速入门代码添加到主函数中,并调用脚本底部的 computerVisionYou will add your quickstart code into the primary function, and call computerVision at the bottom of the script.

function computerVision() {
  async.series([
    async function () {},
    function () {
      return new Promise((resolve) => {
        resolve();
      })
    }
  ], (err) => {
    throw (err);
  });
}

computerVision();

对象模型Object model

以下类和接口将处理计算机视觉 Node.js SDK 的某些主要功能。The following classes and interfaces handle some of the major features of the Computer Vision Node.js SDK.

名称Name 说明Description
ComputerVisionClientComputerVisionClient 所有计算机视觉功能都需要此类。This class is needed for all Computer Vision functionality. 可以使用订阅信息实例化此类,然后使用它来执行大多数图像操作。You instantiate it with your subscription information, and you use it to do most image operations.
VisualFeatureTypesVisualFeatureTypes 此枚举定义可在标准分析操作中执行的不同类型的图像分析。This enum defines the different types of image analysis that can be done in a standard Analyze operation. 请根据需求指定一组 VisualFeatureTypes 值。You specify a set of VisualFeatureTypes values depending on your needs.

代码示例Code examples

这些代码片段演示如何使用适用于 Node.js 的计算机视觉客户端库执行以下任务:These code snippets show you how to do the following tasks with the Computer Vision client library for Node.js:

验证客户端Authenticate the client

为资源的 Azure 终结点和密钥创建变量。Create variables for your resource's Azure endpoint and key. 如果在启动应用程序后创建了环境变量,则需要关闭再重新打开运行该应用程序的编辑器、IDE 或 shell 才能访问该变量。If you created the environment variable after you launched the application, you will need to close and reopen the editor, IDE, or shell running it to access the variable.

/**
 * AUTHENTICATE
 * This single client is used for all examples.
 */
let key = process.env['COMPUTER_VISION_SUBSCRIPTION_KEY'];
let endpoint = process.env['COMPUTER_VISION_ENDPOINT']
if (!key) { throw new Error('Set your environment variables for your subscription key and endpoint.'); }

使用终结点和密钥实例化某个客户端。Instantiate a client with your endpoint and key. 使用密钥和终结点创建 ApiKeyCredentials 对象,然后使用它创建 ComputerVisionClient 对象。Create a ApiKeyCredentials object with your key and endpoint, and use it to create a ComputerVisionClient object.

let computerVisionClient = new ComputerVisionClient(
    new ApiKeyCredentials({inHeader: {'Ocp-Apim-Subscription-Key': key}}), endpoint);

分析图像Analyze an image

此部分的代码通过分析远程图像来提取各种视觉特征。The code in this section analyzes remote images to extract various visual features. 可以在客户端对象的 analyzeImage 方法中执行这些操作,也可以使用单个方法来调用它们。You can do these operations as part of the analyzeImage method of the client object, or you can call them using individual methods. 有关详细信息,请参阅参考文档See the reference documentation for details.

备注

还可以分析本地图像。You can also analyze a local image. 请参阅 GitHub 上的示例代码以了解涉及本地图像的方案。See the sample code on GitHub for scenarios involving local images.

获取图像说明Get image description

下面的代码获取为图像生成的描述文字列表。The following code gets the list of generated captions for the image. 有关更多详细信息,请参阅描述图像See Describe images for more details.

首先,定义要分析的图像的 URL:First, define the URL of an image to analyze:

var describeURL = 'https://moderatorsampleimages.blob.core.windows.net/samples/sample1.jpg';

然后添加以下代码,用于获取图像详细信息并将其输出到控制台。Then add the following code to get the image description and print it to the console.

// Analyze URL image
console.log('Analyzing URL image to describe...', describeURL.split('/').pop());
var caption = (await computerVisionClient.describeImage(describeURL)).captions[0];
console.log(`This may be ${caption.text} (${caption.confidence.toFixed(2)} confidence)`);

获取图像类别Get image category

下面的代码获取所检测到的图像类别。The following code gets the detected category of the image. 有关更多详细信息,请参阅对图像进行分类See Categorize images for more details.

const categoryURLImage = 'https://moderatorsampleimages.blob.core.windows.net/samples/sample16.png';

// Analyze URL image
console.log('Analyzing category in image...', categoryURLImage.split('/').pop());
let categories = (await computerVisionClient.analyzeImage(categoryURLImage)).categories;
console.log(`Categories: ${formatCategories(categories)}`);

定义帮助程序函数 formatCategoriesDefine the helper function formatCategories:

// Formats the image categories
function formatCategories(categories) {
  categories.sort((a, b) => b.score - a.score);
  return categories.map(cat => `${cat.name} (${cat.score.toFixed(2)})`).join(', ');
}

获取图像标记Get image tags

以下代码获取图像中检测到的标记集。The following code gets the set of detected tags in the image. 有关更多详细信息,请参阅内容标记See Content tags for more details.

console.log('-------------------------------------------------');
console.log('DETECT TAGS');
console.log();

// Image of different kind of dog.
const tagsURL = 'https://moderatorsampleimages.blob.core.windows.net/samples/sample16.png';

// Analyze URL image
console.log('Analyzing tags in image...', tagsURL.split('/').pop());
let tags = (await computerVisionClient.analyzeImage(tagsURL, {visualFeatures: ['Tags']})).tags;
console.log(`Tags: ${formatTags(tags)}`);

定义帮助程序函数 formatTagsDefine the helper function formatTags:

// Format tags for display
function formatTags(tags) {
  return tags.map(tag => (`${tag.name} (${tag.confidence.toFixed(2)})`)).join(', ');
}

检测物体Detect objects

以下代码检测图像中的常见物体并将其输出到控制台。The following code detects common objects in the image and prints them to the console. 有关更多详细信息,请参阅物体检测See Object detection for more details.

// Image of a dog
const objectURL = 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-node-sdk-samples/master/Data/image.jpg';

// Analyze a URL image
console.log('Analyzing objects in image...', objectURL.split('/').pop());
let objects = (await computerVisionClient.analyzeImage(objectURL, {visualFeatures: ['Objects']})).objects;
console.log();

// Print objects bounding box and confidence
if (objects.length) {
    console.log(`${objects.length} object${objects.length == 1 ? '' : 's'} found:`);
    for (let obj of objects) { console.log(`    ${obj.object} (${obj.confidence.toFixed(2)}) at ${formatRectObjects(obj.rectangle)}`); }
} else { console.log('No objects found.'); }

定义帮助程序函数 formatRectObjectsDefine the helper function formatRectObjects:

// Formats the bounding box
function formatRectObjects(rect) {
  return `top=${rect.y}`.padEnd(10) + `left=${rect.x}`.padEnd(10) + `bottom=${rect.y + rect.h}`.padEnd(12) 
  + `right=${rect.x + rect.w}`.padEnd(10) + `(${rect.w}x${rect.h})`;
}

检测品牌Detect brands

以下代码检测图像中的公司品牌和徽标,并将其输出到控制台。The following code detects corporate brands and logos in the image and prints them to the console. 有关更多详细信息,请参阅品牌检测See Brand detection for more details.

const brandURLImage = 'https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/images/red-shirt-logo.jpg';

// Analyze URL image
console.log('Analyzing brands in image...', brandURLImage.split('/').pop());
let brands = (await computerVisionClient.analyzeImage(brandURLImage, {visualFeatures: ['Brands']})).brands;

// Print the brands found
if (brands.length) {
    console.log(`${brands.length} brand${brands.length != 1 ? 's' : ''} found:`);
    for (let brand of brands) {
        console.log(`    ${brand.name} (${brand.confidence.toFixed(2)} confidence)`);
    }
} else { console.log(`No brands found.`); }

检测人脸Detect faces

下面的代码返回图像中检测到的人脸及其矩形坐标,以及选择面属性。The following code returns the detected faces in the image with their rectangle coordinates and select face attributes. 有关更多详细信息,请参阅人脸检测See Face detection for more details.

const facesImageURL = 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/faces.jpg';

// Analyze URL image.
console.log('Analyzing faces in image...', facesImageURL.split('/').pop());
// Get the visual feature for 'Faces' only.
let faces = (await computerVisionClient.analyzeImage(facesImageURL, {visualFeatures: ['Faces']})).faces;

// Print the bounding box, gender, and age from the faces.
if (faces.length) {
  console.log(`${faces.length} face${faces.length == 1 ? '' : 's'} found:`);
  for (let face of faces) { console.log(`    Gender: ${face.gender}`.padEnd(20) 
    + ` Age: ${face.age}`.padEnd(10) + `at ${formatRectFaces(face.faceRectangle)}`); }
} else { console.log('No faces found.'); }

定义帮助程序函数 formatRectFacesDefine the helper function formatRectFaces:

// Formats the bounding box
function formatRectFaces(rect) {
  return `top=${rect.top}`.padEnd(10) + `left=${rect.left}`.padEnd(10) + `bottom=${rect.top + rect.height}`.padEnd(12) 
    + `right=${rect.left + rect.width}`.padEnd(10) + `(${rect.width}x${rect.height})`;
}

检测成人、色情或血腥内容Detect adult, racy, or gory content

以下代码输出图像中检测到的成人内容。The following code prints the detected presence of adult content in the image. 有关更多详细信息,请参阅成人、色情或血腥内容See Adult, racy, gory content for more details.

定义要使用的图像的 URL:Define the URL of the image to use:

// The URL image and local images are not racy/adult. 
// Try your own racy/adult images for a more effective result.
const adultURLImage = 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/celebrities.jpg';

然后添加以下代码来检测成人内容,并将结果输出到控制台。Then add the following code to detect adult content and print the results to the console.

// Function to confirm racy or not
const isIt = flag => flag ? 'is' : "isn't";

// Analyze URL image
console.log('Analyzing image for racy/adult content...', adultURLImage.split('/').pop());
var adult = (await computerVisionClient.analyzeImage(adultURLImage, {
  visualFeatures: ['Adult']
})).adult;
console.log(`This probably ${isIt(adult.isAdultContent)} adult content (${adult.adultScore.toFixed(4)} score)`);
console.log(`This probably ${isIt(adult.isRacyContent)} racy content (${adult.racyScore.toFixed(4)} score)`);

获取图像配色方案Get image color scheme

以下代码输出图像中检测到的颜色属性,如主色和主题色。The following code prints the detected color attributes in the image, like the dominant colors and accent color. 有关更多详细信息,请参阅配色方案See Color schemes for more details.

const colorURLImage = 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/celebrities.jpg';

// Analyze URL image
console.log('Analyzing image for color scheme...', colorURLImage.split('/').pop());
console.log();
let color = (await computerVisionClient.analyzeImage(colorURLImage, {visualFeatures: ['Color']})).color;
printColorScheme(color);

定义帮助程序函数 printColorScheme,将颜色方案的详细信息输出到控制台。Define the helper function printColorScheme to print the details of the color scheme to the console.

// Print a detected color scheme
function printColorScheme(colors){
  console.log(`Image is in ${colors.isBwImg ? 'black and white' : 'color'}`);
  console.log(`Dominant colors: ${colors.dominantColors.join(', ')}`);
  console.log(`Dominant foreground color: ${colors.dominantColorForeground}`);
  console.log(`Dominant background color: ${colors.dominantColorBackground}`);
  console.log(`Suggested accent color: #${colors.accentColor}`);
}

获取特定于域的内容Get domain-specific content

计算机视觉可以使用专用模型对图像进行进一步分析。Computer Vision can use specialized model to do further analysis on images. 有关更多详细信息,请参阅特定于域的内容See Domain-specific content for more details.

首先,定义要分析的图像的 URL:First, define the URL of an image to analyze:

const domainURLImage = 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/landmark.jpg';

以下代码分析了图像中检测到的地标的相关数据。The following code parses data about detected landmarks in the image.

// Analyze URL image
console.log('Analyzing image for landmarks...', domainURLImage.split('/').pop());
let domain = (await computerVisionClient.analyzeImageByDomain('landmarks', domainURLImage)).result.landmarks;

// Prints domain-specific, recognized objects
if (domain.length) {
  console.log(`${domain.length} ${domain.length == 1 ? 'landmark' : 'landmarks'} found:`);
  for (let obj of domain) {
    console.log(`    ${obj.name}`.padEnd(20) + `(${obj.confidence.toFixed(2)} confidence)`.padEnd(20) + `${formatRectDomain(obj.faceRectangle)}`);
  }
} else {
  console.log('No landmarks found.');
}

定义帮助程序函数 formatRectDomain,分析有关检测到的地标的位置数据。Define the helper function formatRectDomain to parse the location data about detected landmarks.

// Formats bounding box
function formatRectDomain(rect) {
  if (!rect) return '';
  return `top=${rect.top}`.padEnd(10) + `left=${rect.left}`.padEnd(10) + `bottom=${rect.top + rect.height}`.padEnd(12) +
    `right=${rect.left + rect.width}`.padEnd(10) + `(${rect.width}x${rect.height})`;
}

获取图像类型Get the image type

以下代码输出有关图像类型的信息—无论它是剪贴画还是线条图。The following code prints information about the type of image—whether it is clip art or line drawing.

const typeURLImage = 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-python-sdk-samples/master/samples/vision/images/make_things_happen.jpg';

 // Analyze URL image
console.log('Analyzing type in image...', typeURLImage.split('/').pop());
let types = (await computerVisionClient.analyzeImage(typeURLImage, {visualFeatures: ['ImageType']})).imageType;
console.log(`Image appears to be ${describeType(types)}`);

定义帮助程序函数 describeTypeDefine the helper function describeType:

function describeType(imageType) {
  if (imageType.clipArtType && imageType.clipArtType > imageType.lineDrawingType) return 'clip art';
  if (imageType.lineDrawingType && imageType.clipArtType < imageType.lineDrawingType) return 'a line drawing';
  return 'a photograph';
}

读取印刷体文本和手写文本Read printed and handwritten text

计算机视觉可以读取图像中的可见文本,并将其转换为字符流。Computer Vision can read visible text in an image and convert it to a character stream.

备注

还可以从本地图像读取文本。You can also read text from a local image. 请参阅 GitHub 上的示例代码以了解涉及本地图像的方案。See the sample code on GitHub for scenarios involving local images.

设置测试图像Set up test images

保存要从中提取文本的图像的 URL 的引用。Save a reference of the URL of the images you want to extract text from.

// URL images containing printed and handwritten text
 const printedText     = 'https://moderatorsampleimages.blob.core.windows.net/samples/sample2.jpg';
 const handwrittenText = 'https://raw.githubusercontent.com/MicrosoftDocs/azure-docs/master/articles/cognitive-services/Computer-vision/Images/readsample.jpg';

调用识别 APICall the Recognize API

添加以下代码,该代码针对给定图像调用 recognizeText 函数。Add the code below, which calls the recognizeText function for the given images.

// Recognize text in printed image
console.log('Recognizing printed text...', printedText.split('/').pop());
var printed = await recognizeText(computerVisionClient, 'Printed', printedText);
printRecText(printed);

// Recognize text in handwritten image
console.log('\nRecognizing handwritten text...', handwrittenText.split('/').pop());
var handwriting = await recognizeText(computerVisionClient, 'Handwritten', handwrittenText);
printRecText(handwriting);

定义 recognizeText 函数。Define the recognizeText function. 这会在客户端对象上调用 recognizeText 方法,该方法返回一个操作 ID 并启动异步进程来读取图像的内容。This calls the recognizeText method on the client object, which returns an operation ID and starts an asynchronous process to read the content of the image. 然后,它会使用操作 ID 每隔一秒钟检查一次操作,直到返回结果。Then it uses the operation ID to check the operation at one-second intervals until the results are returned. 然后它会返回提取的结果。It then returns the extracted results.

// Perform text recognition and await the result
async function recognizeText(client, mode, url) {
  // To recognize text in a local image, replace client.recognizeText() with recognizeTextInStream() as shown:
  // result = await client.recognizeTextInStream(mode, () => createReadStream(localImagePath));
  let result = await client.recognizeText(mode, url);
  // Operation ID is last path segment of operationLocation (a URL)
  let operation = result.operationLocation.split('/').slice(-1)[0];

  // Wait for text recognition to complete
  // result.status is initially undefined, since it's the result of recognizeText
  while (result.status !== 'Succeeded') { await sleep(1000); result = await client.getTextOperationResult(operation); }
  return result.recognitionResult;
}

然后定义帮助程序函数 printRecText,该函数将某项识别操作的结果输出到控制台。Then, define the helper function printRecText, which prints the results of a Recognize operation to the console.

// Prints all text from OCR result
function printRecText(ocr) {
  if (ocr.lines.length) {
      console.log('Recognized text:');
      for (let line of ocr.lines) {
          console.log(line.words.map(w => w.text).join(' '));
      }
  }
  else { console.log('No recognized text.'); }
}

运行应用程序Run the application

在快速入门文件中使用 node 命令运行应用程序。Run the application with the node command on your quickstart file.

node index.js

清理资源Clean up resources

如果想要清理并删除认知服务订阅,可以删除资源或资源组。If you want to clean up and remove a Cognitive Services subscription, you can delete the resource or resource group. 删除资源组同时也会删除与之相关联的任何其他资源。Deleting the resource group also deletes any other resources associated with it.

后续步骤Next steps

参考文档 | 库源代码 | 包 (PiPy) | 示例Reference documentation | Library source code | Package (PiPy) | Samples

先决条件Prerequisites

设置Setting up

创建计算机视觉 Azure 资源Create a Computer Vision Azure resource

Azure 认知服务由你订阅的 Azure 资源表示。Azure Cognitive Services are represented by Azure resources that you subscribe to. 在本地计算机上使用 Azure 门户Azure CLI 创建计算机视觉的资源。Create a resource for Computer Vision using the Azure portal or Azure CLI on your local machine. 也可执行以下操作:You can also:

获取试用订阅或资源的密钥后,请为该密钥和终结点 URL 创建环境变量,分别名为 COMPUTER_VISION_SUBSCRIPTION_KEYCOMPUTER_VISION_ENDPOINTAfter you get a key from your trial subscription or resource, create environment variables for the key and endpoint URL, named COMPUTER_VISION_SUBSCRIPTION_KEY and COMPUTER_VISION_ENDPOINT, respectively.

创建新的 Python 应用程序Create a new Python application

创建新的 Python 脚本 — 例如 quickstart-file.pyCreate a new Python script—quickstart-file.py, for example. 在喜好的编辑器或 IDE 中打开该文件,并导入以下库。Then open it in your preferred editor or IDE and import the following libraries.

from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.cognitiveservices.vision.computervision.models import TextOperationStatusCodes
from azure.cognitiveservices.vision.computervision.models import TextRecognitionMode
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
from msrest.authentication import CognitiveServicesCredentials

from array import array
import os
from PIL import Image
import sys
import time

然后,为该资源的 Azure 终结点和密钥创建变量。Then, create variables for your resource's Azure endpoint and key.

# Add your Computer Vision subscription key to your environment variables.
if 'COMPUTER_VISION_SUBSCRIPTION_KEY' in os.environ:
    subscription_key = os.environ['COMPUTER_VISION_SUBSCRIPTION_KEY']
else:
    print("\nSet the COMPUTER_VISION_SUBSCRIPTION_KEY environment variable.\n**Restart your shell or IDE for changes to take effect.**")
    sys.exit()
# Add your Computer Vision endpoint to your environment variables.
if 'COMPUTER_VISION_ENDPOINT' in os.environ:
    endpoint = os.environ['COMPUTER_VISION_ENDPOINT']
else:
    print("\nSet the COMPUTER_VISION_ENDPOINT environment variable.\n**Restart your shell or IDE for changes to take effect.**")
    sys.exit()

备注

如果在启动应用程序后创建了环境变量,则需要关闭再重新打开运行该应用程序的编辑器、IDE 或 shell 才能访问该变量。If you created the environment variable after you launched the application, you will need to close and reopen the editor, IDE, or shell running it to access the variable.

安装客户端库Install the client library

可使用以下方式安装客户端库:You can install the client library with:

pip install --upgrade azure-cognitiveservices-vision-computervision

对象模型Object model

以下类和接口将处理计算机视觉 Python SDK 的某些主要功能。The following classes and interfaces handle some of the major features of the Computer Vision Python SDK.

名称Name 说明Description
ComputerVisionClientOperationsMixinComputerVisionClientOperationsMixin 此类直接处理所有图像操作,例如图像分析、文本检测和缩略图生成。This class directly handles all of the image operations, such as image analysis, text detection, and thumbnail generation.
ComputerVisionClientComputerVisionClient 所有计算机视觉功能都需要此类。This class is needed for all Computer Vision functionality. 请使用你的订阅信息实例化此类,然后使用它来生成其他类的实例。You instantiate it with your subscription information, and you use it to produce instances of other classes. 它实现了 ComputerVisionClientOperationsMixinIt implements ComputerVisionClientOperationsMixin.
VisualFeatureTypesVisualFeatureTypes 此枚举定义可在标准分析操作中执行的不同类型的图像分析。This enum defines the different types of image analysis that can be done in a standard Analyze operation. 请根据需求指定一组 VisualFeatureTypes 值。You specify a set of VisualFeatureTypes values depending on your needs.

代码示例Code examples

这些代码片段演示如何使用适用于 Python 的计算机视觉客户端库执行以下任务:These code snippets show you how to do the following tasks with the Computer Vision client library for Python:

验证客户端Authenticate the client

备注

本快速入门假设你已为计算机视觉密钥创建了名为 COMPUTER_VISION_SUBSCRIPTION_KEY环境变量This quickstart assumes you've created an environment variable for your Computer Vision key, named COMPUTER_VISION_SUBSCRIPTION_KEY.

使用终结点和密钥实例化某个客户端。Instantiate a client with your endpoint and key. 使用密钥创建 CognitiveServicesCredentials 对象,然后在终结点上使用该对象创建 ComputerVisionClient 对象。Create a CognitiveServicesCredentials object with your key, and use it with your endpoint to create a ComputerVisionClient object.

computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))

分析图像Analyze an image

保存对要分析的图像的 URL 的引用。Save a reference to the URL of an image you want to analyze.

remote_image_url = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/landmark.jpg"

获取图像说明Get image description

下面的代码获取为图像生成的描述文字列表。The following code gets the list of generated captions for the image. 有关更多详细信息,请参阅描述图像See Describe images for more details.

remote_image_url = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/landmark.jpg"

获取图像类别Get image category

下面的代码获取所检测到的图像类别。The following code gets the detected category of the image. 有关更多详细信息,请参阅对图像进行分类See Categorize images for more details.

'''
Describe an Image - remote
This example describes the contents of an image with the confidence score.
'''
print("===== Describe an image - remote =====")
# Call API
description_results = computervision_client.describe_image(remote_image_url )

# Get the captions (descriptions) from the response, with confidence level
print("Description of remote image: ")
if (len(description_results.captions) == 0):
    print("No description detected.")
else:
    for caption in description_results.captions:
        print("'{}' with confidence {:.2f}%".format(caption.text, caption.confidence * 100))

获取图像标记Get image tags

以下代码获取图像中检测到的标记集。The following code gets the set of detected tags in the image. 有关更多详细信息,请参阅内容标记See Content tags for more details.

'''
Tag an Image - remote
This example returns a tag (key word) for each thing in the image.
'''
print("===== Tag an image - remote =====")
# Call API with remote image
tags_result_remote = computervision_client.tag_image(remote_image_url )

# Print results with confidence score
print("Tags in the remote image: ")
if (len(tags_result_remote.tags) == 0):
    print("No tags detected.")
else:
    for tag in tags_result_remote.tags:
        print("'{}' with confidence {:.2f}%".format(tag.name, tag.confidence * 100))
``

### Detect objects

The following code detects common objects in the image and prints them to the console. See [Object detection](../../concept-object-detection.md) for more details.

```python
'''
Detect Objects - remote
This example detects different kinds of objects with bounding boxes in a remote image.
'''
print("===== Detect Objects - remote =====")
# Get URL image with different objects
remote_image_url_objects = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/objects.jpg"
# Call API with URL
detect_objects_results_remote = computervision_client.detect_objects(remote_image_url_objects)

# Print detected objects results with bounding boxes
print("Detecting objects in remote image:")
if len(detect_objects_results_remote.objects) == 0:
    print("No objects detected.")
else:
    for object in detect_objects_results_remote.objects:
        print("object at location {}, {}, {}, {}".format( \
        object.rectangle.x, object.rectangle.x + object.rectangle.w, \
        object.rectangle.y, object.rectangle.y + object.rectangle.h))

检测品牌Detect brands

以下代码检测图像中的公司品牌和徽标,并将其输出到控制台。The following code detects corporate brands and logos in the image and prints them to the console. 有关更多详细信息,请参阅品牌检测See Brand detection for more details.

'''
Detect Brands - remote
This example detects common brands like logos and puts a bounding box around them.
'''
print("===== Detect Brands - remote =====")
# Get a URL with a brand logo
remote_image_url = "https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/images/gray-shirt-logo.jpg"
# Select the visual feature(s) you want
remote_image_features = ["brands"]
# Call API with URL and features
detect_brands_results_remote = computervision_client.analyze_image(remote_image_url, remote_image_features)

print("Detecting brands in remote image: ")
if len(detect_brands_results_remote.brands) == 0:
    print("No brands detected.")
else:
    for brand in detect_brands_results_remote.brands:
        print("'{}' brand detected with confidence {:.1f}% at location {}, {}, {}, {}".format( \
        brand.name, brand.confidence * 100, brand.rectangle.x, brand.rectangle.x + brand.rectangle.w, \
        brand.rectangle.y, brand.rectangle.y + brand.rectangle.h))

检测人脸Detect faces

下面的代码返回图像中检测到的人脸及其矩形坐标,以及选择面属性。The following code returns the detected faces in the image with their rectangle coordinates and select face attributes. 有关更多详细信息,请参阅人脸检测See Face detection for more details.

'''
Detect Faces - remote
This example detects faces in a remote image, gets their gender and age, 
and marks them with a bounding box.
'''
print("===== Detect Faces - remote =====")
# Get an image with faces
remote_image_url_faces = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/faces.jpg"
# Select the visual feature(s) you want.
remote_image_features = ["faces"]
# Call the API with remote URL and features
detect_faces_results_remote = computervision_client.analyze_image(remote_image_url_faces, remote_image_features)

# Print the results with gender, age, and bounding box
print("Faces in the remote image: ")
if (len(detect_faces_results_remote.faces) == 0):
    print("No faces detected.")
else:
    for face in detect_faces_results_remote.faces:
        print("'{}' of age {} at location {}, {}, {}, {}".format(face.gender, face.age, \
        face.face_rectangle.left, face.face_rectangle.top, \
        face.face_rectangle.left + face.face_rectangle.width, \
        face.face_rectangle.top + face.face_rectangle.height))

检测成人、色情或血腥内容Detect adult, racy, or gory content

以下代码输出图像中检测到的成人内容。The following code prints the detected presence of adult content in the image. 有关更多详细信息,请参阅成人、色情或血腥内容See Adult, racy, gory content for more details.

'''
Detect Adult or Racy Content - remote
This example detects adult or racy content in a remote image, then prints the adult/racy score.
The score is ranged 0.0 - 1.0 with smaller numbers indicating negative results.
'''
print("===== Detect Adult or Racy Content - remote =====")
# Select the visual feature(s) you want
remote_image_features = ["adult"]
# Call API with URL and features
detect_adult_results_remote = computervision_client.analyze_image(remote_image_url, remote_image_features)

# Print results with adult/racy score
print("Analyzing remote image for adult or racy content ... ")
print("Is adult content: {} with confidence {:.2f}".format(detect_adult_results_remote.adult.is_adult_content, detect_adult_results_remote.adult.adult_score * 100))
print("Has racy content: {} with confidence {:.2f}".format(detect_adult_results_remote.adult.is_racy_content, detect_adult_results_remote.adult.racy_score * 100))

获取图像配色方案Get image color scheme

以下代码输出图像中检测到的颜色属性,如主色和主题色。The following code prints the detected color attributes in the image, like the dominant colors and accent color. 有关更多详细信息,请参阅配色方案See Color schemes for more details.

'''
Detect Color - remote
This example detects the different aspects of its color scheme in a remote image.
'''
print("===== Detect Color - remote =====")
# Select the feature(s) you want
remote_image_features = ["color"]
# Call API with URL and features
detect_color_results_remote = computervision_client.analyze_image(remote_image_url, remote_image_features)

# Print results of color scheme
print("Getting color scheme of the remote image: ")
print("Is black and white: {}".format(detect_color_results_remote.color.is_bw_img))
print("Accent color: {}".format(detect_color_results_remote.color.accent_color))
print("Dominant background color: {}".format(detect_color_results_remote.color.dominant_color_background))
print("Dominant foreground color: {}".format(detect_color_results_remote.color.dominant_color_foreground))
print("Dominant colors: {}".format(detect_color_results_remote.color.dominant_colors))

获取特定于域的内容Get domain-specific content

计算机视觉可以使用专用模型对图像进行进一步分析。Computer Vision can use specialized model to do further analysis on images. 有关更多详细信息,请参阅特定于域的内容See Domain-specific content for more details.

以下代码分析了图像中检测到的名人的相关数据。The following code parses data about detected celebrities in the image.

'''
Detect Domain-specific Content - remote
This example detects celebrites and landmarks in remote images.
'''
print("===== Detect Domain-specific Content - remote =====")
# URL of one or more celebrities
remote_image_url_celebs = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/faces.jpg"
# Call API with content type (celebrities) and URL
detect_domain_results_celebs_remote = computervision_client.analyze_image_by_domain("celebrities", remote_image_url_celebs)

# Print detection results with name
print("Celebrities in the remote image:")
if len(detect_domain_results_celebs_remote.result["celebrities"]) == 0:
    print("No celebrities detected.")
else:
    for celeb in detect_domain_results_celebs_remote.result["celebrities"]:
        print(celeb["name"])

以下代码分析了图像中检测到的地标的相关数据。The following code parses data about detected landmarks in the image.

# Call API with content type (landmarks) and URL
detect_domain_results_landmarks = computervision_client.analyze_image_by_domain("landmarks", remote_image_url)
print()

print("Landmarks in the remote image:")
if len(detect_domain_results_landmarks.result["landmarks"]) == 0:
    print("No landmarks detected.")
else:
    for landmark in detect_domain_results_landmarks.result["landmarks"]:
        print(landmark["name"])

获取图像类型Get the image type

以下代码输出有关图像类型的信息—无论它是剪贴画还是线条图。The following code prints information about the type of image—whether it is clip art or line drawing.

'''
Detect Image Types - remote
This example detects an image's type (clip art/line drawing).
'''
print("===== Detect Image Types - remote =====")
# Get URL of an image with a type
remote_image_url_type = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/type-image.jpg"
# Select visual feature(s) you want
remote_image_features = VisualFeatureTypes.image_type
# Call API with URL and features
detect_type_results_remote = computervision_client.analyze_image(remote_image_url_type, remote_image_features)

# Prints type results with degree of accuracy
print("Type of remote image:")
if detect_type_results_remote.image_type.clip_art_type == 0:
    print("Image is not clip art.")
elif detect_type_results_remote.image_type.line_drawing_type == 1:
    print("Image is ambiguously clip art.")
elif detect_type_results_remote.image_type.line_drawing_type == 2:
    print("Image is normal clip art.")
else:
    print("Image is good clip art.")

if detect_type_results_remote.image_type.line_drawing_type == 0:
    print("Image is not a line drawing.")
else:
    print("Image is a line drawing")

读取印刷体文本和手写文本Read printed and handwritten text

计算机视觉可以读取图像中的可见文本,并将其转换为字符流。Computer Vision can read visible text in an image and convert it to a character stream. 分两部分来执行此操作。You do this in two parts.

调用读取 APICall the Read API

首先,使用以下代码对给定图像调用 batch_read_file 方法。First, use the following code to call the batch_read_file method for the given image. 这会返回一个操作 ID 并启动异步进程来读取图像的内容。This returns an operation ID and starts an asynchronous process to read the content of the image.

'''
Batch Read File, recognize handwritten text - remote
This example will extract handwritten text in an image, then print results, line by line.
This API call can also recognize handwriting (not shown).
'''
print("===== Batch Read File - remote =====")
# Get an image with handwritten text
remote_image_handw_text_url = "https://raw.githubusercontent.com/MicrosoftDocs/azure-docs/master/articles/cognitive-services/Computer-vision/Images/readsample.jpg"

# Call API with URL and raw response (allows you to get the operation location)
recognize_handw_results = computervision_client.read(remote_image_handw_text_url,  raw=True)

获取读取结果Get Read results

接下来,获取从 batch_read_file 调用返回的操作 ID,并使用它查询服务以获取操作结果。Next, get the operation ID returned from the batch_read_file call, and use it to query the service for operation results. 下面的代码每隔一秒钟检查一次操作,直到返回结果。The following code checks the operation at one-second intervals until the results are returned. 然后,它将提取的文本数据输出到控制台。It then prints the extracted text data to the console.

# Get the operation location (URL with an ID at the end) from the response
operation_location_remote = recognize_handw_results.headers["Operation-Location"]
# Grab the ID from the URL
operation_id = operation_location_remote.split("/")[-1]

# Call the "GET" API and wait for it to retrieve the results 
while True:
    get_handw_text_results = computervision_client.get_read_result(operation_id)
    if get_handw_text_results.status not in ['NotStarted', 'Running']:
        break
    time.sleep(1)

# Print the detected text, line by line
if get_handw_text_results.status == TextOperationStatusCodes.succeeded:
    for text_result in get_handw_text_results.recognition_results:
        for line in text_result.lines:
            print(line.text)
            print(line.bounding_box)
print()

运行应用程序Run the application

在快速入门文件中使用 python 命令运行应用程序。Run the application with the python command on your quickstart file.

python quickstart-file.py

清理资源Clean up resources

如果想要清理并删除认知服务订阅,可以删除资源或资源组。If you want to clean up and remove a Cognitive Services subscription, you can delete the resource or resource group. 删除资源组同时也会删除与之相关联的任何其他资源。Deleting the resource group also deletes any other resources associated with it.

后续步骤Next steps

本快速入门介绍了如何使用适用于 Python 的计算机视觉库执行基本任务。In this quickstart, you learned how to use the Computer Vision library for Python to do basis tasks. 接下来,请在参考文档中详细了解该库。Next, explore the reference documentation to learn more about the library.

参考文档 | 库源代码 | Reference documentation | Library source code | Package

先决条件Prerequisites

设置Setting up

创建计算机视觉 Azure 资源Create a Computer Vision Azure resource

Azure 认知服务由你订阅的 Azure 资源表示。Azure Cognitive Services are represented by Azure resources that you subscribe to. 在本地计算机上使用 Azure 门户Azure CLI 创建计算机视觉的资源。Create a resource for Computer Vision using the Azure portal or Azure CLI on your local machine. 也可执行以下操作:You can also:

获取试用订阅或资源的密钥后,请为该密钥和终结点 URL 创建环境变量,分别名为 COMPUTER_VISION_SUBSCRIPTION_KEYCOMPUTER_VISION_ENDPOINTAfter you get a key from your trial subscription or resource, create environment variables for the key and endpoint URL, named COMPUTER_VISION_SUBSCRIPTION_KEY and COMPUTER_VISION_ENDPOINT, respectively.

创建 Go 项目目录Create a Go project directory

在控制台窗口(cmd、PowerShell、终端、Bash)中,为 Go 项目创建一个名为 my-app 的新工作区并导航到该工作区。In a console window (cmd, PowerShell, Terminal, Bash), create a new workspace for your Go project, named my-app, and navigate to it.

mkdir -p my-app/{src, bin, pkg}  
cd my-app

工作区包含三个文件夹:Your workspace will contain three folders:

  • src - 此目录包含源代码和包。src - This directory will contain source code and packages. 使用 go get 命令安装的任何包都将在此目录中。Any packages installed with the go get command will go in this directory.
  • pkg - 此目录包含编译的 Go 包对象。pkg - This directory will contain the compiled Go package objects. 这些文件使用 .a 扩展名。These files all have an .a extension.
  • bin - 此目录包含运行 go install 时创建的二进制可执行文件。bin - This directory will contain the binary executable files that are created when you run go install.

安装适用于 Go 的客户端库Install the client library for Go

接下来,安装适用于 Go 的客户端库:Next, install the client library for Go:

go get -u https://github.com/Azure/azure-sdk-for-go/tree/master/services/cognitiveservices/v2.1/computervision

或者,如果使用 dep,则在存储库中运行:or if you use dep, within your repo run:

dep ensure -add https://github.com/Azure/azure-sdk-for-go/tree/master/services/cognitiveservices/v2.1/computervision

创建 Go 应用程序Create a Go application

接下来,在 src 目录中创建名为 sample-app.go 的文件****:Next, create a file in the src directory named sample-app.go:

cd src
touch sample-app.go

在首选 IDE 或文本编辑器中打开 sample-app.goOpen sample-app.go in your preferred IDE or text editor. 然后添加包名称并导入以下库:Then add the package name and import the following libraries:

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v2.0/computervision"
    "github.com/Azure/go-autorest/autorest"
    "io"
    "log"
    "os"
    "strings"
    "time"
)

此外,还可以在脚本的根目录中声明一个上下文。Also, declare a context at the root of your script. 你将需要此对象来执行大多数计算机视觉函数调用:You'll need this object to execute most Computer Vision function calls:

// Declare global so don't have to pass it to all of the tasks.
var computerVisionContext context.Context

接下来,将开始添加代码以执行不同的计算机视觉操作。Next, you'll begin adding code to carry out different Computer Vision operations.

对象模型Object model

以下类和接口将处理计算机视觉 Go SDK 的某些主要功能。The following classes and interfaces handle some of the major features of the Computer Vision Go SDK.

名称Name 说明Description
BaseClientBaseClient 所有计算机视觉功能(如图像分析和文本阅读)都需要此类。This class is needed for all Computer Vision functionality, such as image analysis and text reading. 可以使用订阅信息实例化此类,然后使用它来执行大多数图像操作。You instantiate it with your subscription information, and you use it to do most image operations.
ImageAnalysisImageAnalysis 此类型包含 AnalyzeImage 函数调用的结果****。This type contains the results of an AnalyzeImage function call. 每个特定于类别的函数都具有相似的类型。There are similar types for each of the category-specific functions.
ReadOperationResultReadOperationResult 此类型包含批读取操作的结果。This type contains the results of a Batch Read operation.
VisualFeatureTypesVisualFeatureTypes 此类型定义了可在标准分析操作中完成的不同类型的图像分析。This type defines the different kinds of image analysis that can be done in a standard Analyze operation. 请根据需求指定一组 VisualFeatureTypes 值。You specify a set of VisualFeatureTypes values depending on your needs.

代码示例Code examples

这些代码片段演示如何使用适用于 Go 的计算机视觉客户端库执行以下任务:These code snippets show you how to do the following tasks with the Computer Vision client library for Go:

验证客户端Authenticate the client

备注

此步骤假设已经为计算机视觉密钥和终结点(分别名为 COMPUTER_VISION_SUBSCRIPTION_KEYCOMPUTER_VISION_ENDPOINT创建了环境变量This step assumes you've created environment variables for your Computer Vision key and endpoint, named COMPUTER_VISION_SUBSCRIPTION_KEY and COMPUTER_VISION_ENDPOINT respectively.

创建 main 函数,并向其添加以下代码,以使用终结点和密钥实例化客户端。Create a main function and add the following code to it to instantiate a client with your endpoint and key.

/*  
 * Configure the Computer Vision client
 * Set environment variables for COMPUTER_VISION_SUBSCRIPTION_KEY and COMPUTER_VISION_ENDPOINT,
 * then restart your command shell or your IDE for changes to take effect.
 */
  computerVisionKey := os.Getenv("COMPUTER_VISION_SUBSCRIPTION_KEY")

if (computerVisionKey == "") {
    log.Fatal("\n\nPlease set a COMPUTER_VISION_SUBSCRIPTION_KEY environment variable.\n" +
                          "**You may need to restart your shell or IDE after it's set.**\n")
}

endpointURL := os.Getenv("COMPUTER_VISION_ENDPOINT")
if (endpointURL == "") {
    log.Fatal("\n\nPlease set a COMPUTER_VISION_ENDPOINT environment variable.\n" +
                          "**You may need to restart your shell or IDE after it's set.**")
}

computerVisionClient := computervision.New(endpointURL);
computerVisionClient.Authorizer = autorest.NewCognitiveServicesAuthorizer(computerVisionKey)

computerVisionContext = context.Background()
/*
 * END - Configure the Computer Vision client
 */

分析图像Analyze an image

以下代码使用客户端对象分析远程映像,并将结果打印到控制台。The following code uses the client object to analyze a remote image and print the results to the console. 可获取文本说明、分类、标记列表、检测到的物体、检测到的品牌、检测到的人脸、成人内容标志、主要颜色和图像类型。You can get a text description, categorization, list of tags, detected objects, detected brands, detected faces, adult content flags, main colors, and image type.

设置测试图像Set up test image

首先,保存对要分析的图像的 URL 的引用。First save a reference to the URL of the image you want to analyze. 将其放入 main 函数。Put this inside your main function.

landmarkImageURL := "https://github.com/Azure-Samples/cognitive-services-sample-data-files/raw/master/ComputerVision/Images/landmark.jpg"

备注

还可以分析本地图像。You can also analyze a local image. 请参阅 GitHub 上的示例代码以了解涉及本地图像的方案。See the sample code on GitHub for scenarios involving local images.

指定视觉特性Specify visual features

以下函数调用从示例图像中提取不同的视觉特征。The following function calls extract different visual features from the sample image. 将在以下各节中定义这些函数。You'll define these functions in the following sections.

// Analyze features of an image, remote
DescribeRemoteImage(computerVisionClient, landmarkImageURL)
CategorizeRemoteImage(computerVisionClient, landmarkImageURL)
TagRemoteImage(computerVisionClient, landmarkImageURL)
DetectFacesRemoteImage(computerVisionClient, facesImageURL)
DetectObjectsRemoteImage(computerVisionClient, objectsImageURL)
DetectBrandsRemoteImage(computerVisionClient, brandsImageURL)
DetectAdultOrRacyContentRemoteImage(computerVisionClient, adultRacyImageURL)
DetectColorSchemeRemoteImage(computerVisionClient, brandsImageURL)
DetectDomainSpecificContentRemoteImage(computerVisionClient, landmarkImageURL)
DetectImageTypesRemoteImage(computerVisionClient, detectTypeImageURL)
GenerateThumbnailRemoteImage(computerVisionClient, adultRacyImageURL)

获取图像说明Get image description

以下函数获取为图像生成的描述文字列表。The following function gets the list of generated captions for the image. 有关图像描述的详细信息,请参阅描述图像For more information about image description, see Describe images.

func DescribeRemoteImage(client computervision.BaseClient, remoteImageURL string) {
    fmt.Println("-----------------------------------------")
    fmt.Println("DESCRIBE IMAGE - remote")
    fmt.Println()
    var remoteImage computervision.ImageURL
    remoteImage.URL = &remoteImageURL

    maxNumberDescriptionCandidates := new(int32)
    *maxNumberDescriptionCandidates = 1

    remoteImageDescription, err := client.DescribeImage(
            computerVisionContext,
            remoteImage,
            maxNumberDescriptionCandidates,
            "") // language
        if err != nil { log.Fatal(err) }

    fmt.Println("Captions from remote image: ")
    if len(*remoteImageDescription.Captions) == 0 {
        fmt.Println("No captions detected.")
    } else {
        for _, caption := range *remoteImageDescription.Captions {
            fmt.Printf("'%v' with confidence %.2f%%\n", *caption.Text, *caption.Confidence * 100)
        }
    }
    fmt.Println()
}

获取图像类别Get image category

以下函数获取已检测到的映像类别。The following function gets the detected category of the image. 有关详细信息,请参阅对图像进行分类For more information, see Categorize images.

func CategorizeRemoteImage(client computervision.BaseClient, remoteImageURL string) {
    fmt.Println("-----------------------------------------")
    fmt.Println("CATEGORIZE IMAGE - remote")
    fmt.Println()
    var remoteImage computervision.ImageURL
    remoteImage.URL = &remoteImageURL

    features := []computervision.VisualFeatureTypes{computervision.VisualFeatureTypesCategories}
    imageAnalysis, err := client.AnalyzeImage(
            computerVisionContext,
            remoteImage,
            features,
            []computervision.Details{},
            "")
    if err != nil { log.Fatal(err) }

    fmt.Println("Categories from remote image: ")
    if len(*imageAnalysis.Categories) == 0 {
        fmt.Println("No categories detected.")
    } else {
        for _, category := range *imageAnalysis.Categories {
            fmt.Printf("'%v' with confidence %.2f%%\n", *category.Name, *category.Score * 100)
        }
    }
    fmt.Println()
}

获取图像标记Get image tags

以下函数获取映像中检测到的标记集。The following function gets the set of detected tags in the image. 有关详细信息,请参阅内容标记For more information, see Content tags.

func TagRemoteImage(client computervision.BaseClient, remoteImageURL string) {
    fmt.Println("-----------------------------------------")
    fmt.Println("TAG IMAGE - remote")
    fmt.Println()
    var remoteImage computervision.ImageURL
    remoteImage.URL = &remoteImageURL

    remoteImageTags, err := client.TagImage(
            computerVisionContext,
            remoteImage,
            "")
    if err != nil { log.Fatal(err) }

    fmt.Println("Tags in the remote image: ")
    if len(*remoteImageTags.Tags) == 0 {
        fmt.Println("No tags detected.")
    } else {
        for _, tag := range *remoteImageTags.Tags {
            fmt.Printf("'%v' with confidence %.2f%%\n", *tag.Name, *tag.Confidence * 100)
        }
    }
    fmt.Println()
}

检测物体Detect objects

以下函数检测图像中的常见物体并将其打印到控制台。The following function detects common objects in the image and prints them to the console. 有关详细信息,请参阅物体检测For more information, see Object detection.

func DetectObjectsRemoteImage(client computervision.BaseClient, remoteImageURL string) {
    fmt.Println("-----------------------------------------")
    fmt.Println("DETECT OBJECTS - remote")
    fmt.Println()
    var remoteImage computervision.ImageURL
    remoteImage.URL = &remoteImageURL

    imageAnalysis, err := client.DetectObjects(
            computerVisionContext,
            remoteImage,
    )
    if err != nil { log.Fatal(err) }

    fmt.Println("Detecting objects in remote image: ")
    if len(*imageAnalysis.Objects) == 0 {
        fmt.Println("No objects detected.")
    } else {
        // Print the objects found with confidence level and bounding box locations.
        for _, object := range *imageAnalysis.Objects {
            fmt.Printf("'%v' with confidence %.2f%% at location (%v, %v), (%v, %v)\n",
                *object.Object, *object.Confidence * 100,
                *object.Rectangle.X, *object.Rectangle.X + *object.Rectangle.W,
                *object.Rectangle.Y, *object.Rectangle.Y + *object.Rectangle.H)
        }
    }
    fmt.Println()
}

检测品牌Detect brands

以下代码检测图像中的公司品牌和徽标,并将其输出到控制台。The following code detects corporate brands and logos in the image and prints them to the console. 有关详细信息,请参阅品牌检测For more information, Brand detection.

首先,在 main 函数内声明对新图像的引用。First, declare a reference to a new image within your main function.

brandsImageURL := "https://docs.microsoft.com/azure/cognitive-services/computer-vision/images/gray-shirt-logo.jpg"

以下代码定义品牌检测函数。The following code defines the brand detection function.

func DetectBrandsRemoteImage(client computervision.BaseClient, remoteImageURL string) {
    fmt.Println("-----------------------------------------")
    fmt.Println("DETECT BRANDS - remote")
    fmt.Println()
    var remoteImage computervision.ImageURL
    remoteImage.URL = &remoteImageURL

    // Define the kinds of features you want returned.
    features := []computervision.VisualFeatureTypes{computervision.VisualFeatureTypesBrands}

    imageAnalysis, err := client.AnalyzeImage(
        computerVisionContext,
        remoteImage,
        features,
        []computervision.Details{},
        "en")
    if err != nil { log.Fatal(err) }

    fmt.Println("Detecting brands in remote image: ")
    if len(*imageAnalysis.Brands) == 0 {
        fmt.Println("No brands detected.")
    } else {
        // Get bounding box around the brand and confidence level it's correctly identified.
        for _, brand := range *imageAnalysis.Brands {
            fmt.Printf("'%v' with confidence %.2f%% at location (%v, %v), (%v, %v)\n",
                *brand.Name, *brand.Confidence * 100,
                *brand.Rectangle.X, *brand.Rectangle.X + *brand.Rectangle.W,
                *brand.Rectangle.Y, *brand.Rectangle.Y + *brand.Rectangle.H)
        }
    }
    fmt.Println()
}

检测人脸Detect faces

以下函数返回图像中检测到的人脸及其矩形坐标,以及某些人脸属性。The following function returns the detected faces in the image with their rectangle coordinates and certain face attributes. 有关详细信息,请参阅人脸检测For more information, see Face detection.

func DetectFacesRemoteImage(client computervision.BaseClient, remoteImageURL string) {
    fmt.Println("-----------------------------------------")
    fmt.Println("DETECT FACES - remote")
    fmt.Println()
    var remoteImage computervision.ImageURL
    remoteImage.URL = &remoteImageURL

    // Define the features you want returned with the API call.
    features := []computervision.VisualFeatureTypes{computervision.VisualFeatureTypesFaces}
    imageAnalysis, err := client.AnalyzeImage(
            computerVisionContext,
            remoteImage,
            features,
            []computervision.Details{},
            "")
        if err != nil { log.Fatal(err) }

    fmt.Println("Detecting faces in a remote image ...")
    if len(*imageAnalysis.Faces) == 0 {
        fmt.Println("No faces detected.")
    } else {
        // Print the bounding box locations of the found faces.
        for _, face := range *imageAnalysis.Faces {
            fmt.Printf("'%v' of age %v at location (%v, %v), (%v, %v)\n",
                face.Gender, *face.Age,
                *face.FaceRectangle.Left, *face.FaceRectangle.Top,
                *face.FaceRectangle.Left + *face.FaceRectangle.Width,
                *face.FaceRectangle.Top + *face.FaceRectangle.Height)
        }
    }
    fmt.Println()
}

检测成人、色情或血腥内容Detect adult, racy, or gory content

以下函数打印图像中检测到的成人内容。The following function prints the detected presence of adult content in the image. 有关详细信息,请参阅成人、色情或血腥内容For more information, see Adult, racy, gory content.

func DetectAdultOrRacyContentRemoteImage(client computervision.BaseClient, remoteImageURL string) {
    fmt.Println("-----------------------------------------")
    fmt.Println("DETECT ADULT OR RACY CONTENT - remote")
    fmt.Println()
    var remoteImage computervision.ImageURL
    remoteImage.URL = &remoteImageURL

    // Define the features you want returned from the API call.
    features := []computervision.VisualFeatureTypes{computervision.VisualFeatureTypesAdult}
    imageAnalysis, err := client.AnalyzeImage(
            computerVisionContext,
            remoteImage,
            features,
            []computervision.Details{},
            "") // language, English is default
    if err != nil { log.Fatal(err) }

    // Print whether or not there is questionable content.
    // Confidence levels: low means content is OK, high means it's not.
    fmt.Println("Analyzing remote image for adult or racy content: ");
    fmt.Printf("Is adult content: %v with confidence %.2f%%\n", *imageAnalysis.Adult.IsAdultContent, *imageAnalysis.Adult.AdultScore * 100)
    fmt.Printf("Has racy content: %v with confidence %.2f%%\n", *imageAnalysis.Adult.IsRacyContent, *imageAnalysis.Adult.RacyScore * 100)
    fmt.Println()
}

获取图像配色方案Get image color scheme

以下函数打印图像中检测到的颜色属性,如主色和主题色。The following function prints the detected color attributes in the image, like the dominant colors and accent color. 有关详细信息,请参阅配色方案For more information, see Color schemes.

func DetectColorSchemeRemoteImage(client computervision.BaseClient, remoteImageURL string) {
    fmt.Println("-----------------------------------------")
    fmt.Println("DETECT COLOR SCHEME - remote")
    fmt.Println()
    var remoteImage computervision.ImageURL
    remoteImage.URL = &remoteImageURL

    // Define the features you'd like returned with the result.
    features := []computervision.VisualFeatureTypes{computervision.VisualFeatureTypesColor}
    imageAnalysis, err := client.AnalyzeImage(
            computerVisionContext,
            remoteImage,
            features,
            []computervision.Details{},
            "") // language, English is default
    if err != nil { log.Fatal(err) }

    fmt.Println("Color scheme of the remote image: ");
    fmt.Printf("Is black and white: %v\n", *imageAnalysis.Color.IsBWImg)
    fmt.Printf("Accent color: 0x%v\n", *imageAnalysis.Color.AccentColor)
    fmt.Printf("Dominant background color: %v\n", *imageAnalysis.Color.DominantColorBackground)
    fmt.Printf("Dominant foreground color: %v\n", *imageAnalysis.Color.DominantColorForeground)
    fmt.Printf("Dominant colors: %v\n", strings.Join(*imageAnalysis.Color.DominantColors, ", "))
    fmt.Println()
}

获取特定于域的内容Get domain-specific content

计算机视觉可以使用专用模型对图像进行进一步分析。Computer Vision can use specialized models to do further analysis on images. 有关详细信息,请参阅特定于域的内容For more information, see Domain-specific content.

以下代码分析了图像中检测到的名人的相关数据。The following code parses data about detected celebrities in the image.

func DetectDomainSpecificContentRemoteImage(client computervision.BaseClient, remoteImageURL string) {
    fmt.Println("-----------------------------------------")
    fmt.Println("DETECT DOMAIN-SPECIFIC CONTENT - remote")
    fmt.Println()
    var remoteImage computervision.ImageURL
    remoteImage.URL = &remoteImageURL

    fmt.Println("Detecting domain-specific content in the local image ...")

    // Check if there are any celebrities in the image.
    celebrities, err := client.AnalyzeImageByDomain(
            computerVisionContext,
            "celebrities",
            remoteImage,
            "") // language, English is default
    if err != nil { log.Fatal(err) }

    fmt.Println("\nCelebrities: ")

    // Marshal the output from AnalyzeImageByDomain into JSON.
    data, err := json.MarshalIndent(celebrities.Result, "", "\t")

    // Define structs for which to unmarshal the JSON.
    type Celebrities struct {
        Name string `json:"name"`
    }

    type CelebrityResult struct {
        Celebrities []Celebrities `json:"celebrities"`
    }

    var celebrityResult CelebrityResult

    // Unmarshal the data.
    err = json.Unmarshal(data, &celebrityResult)
    if err != nil { log.Fatal(err) }

    //  Check if any celebrities detected.
    if len(celebrityResult.Celebrities) == 0 {
        fmt.Println("No celebrities detected.")
    }   else {
        for _, celebrity := range celebrityResult.Celebrities {
            fmt.Printf("name: %v\n", celebrity.Name)
        }
    }

以下代码分析了图像中检测到的地标的相关数据。The following code parses data about detected landmarks in the image.

fmt.Println("\nLandmarks: ")

    // Check if there are any landmarks in the image.
    landmarks, err := client.AnalyzeImageByDomain(
            computerVisionContext,
            "landmarks",
            remoteImage,
            "")
    if err != nil { log.Fatal(err) }

    // Marshal the output from AnalyzeImageByDomain into JSON.
    data, err = json.MarshalIndent(landmarks.Result, "", "\t")

    // Define structs for which to unmarshal the JSON.
    type Landmarks struct {
        Name string `json:"name"`
    }

    type LandmarkResult struct {
        Landmarks   []Landmarks `json:"landmarks"`
    }

    var landmarkResult LandmarkResult

    // Unmarshal the data.
    err = json.Unmarshal(data, &landmarkResult)
    if err != nil { log.Fatal(err) }

    // Check if any celebrities detected.
    if len(landmarkResult.Landmarks) == 0 {
        fmt.Println("No landmarks detected.")
    }   else {
        for _, landmark := range landmarkResult.Landmarks {
            fmt.Printf("name: %v\n", landmark.Name)
        }
    }
    fmt.Println()
}

获取图像类型Get the image type

以下函数将打印有关图像类型的信息,即表明它是剪贴画还是素描。The following function prints information about the type of image—whether it's clip art or a line drawing.

func DetectImageTypesRemoteImage(client computervision.BaseClient, remoteImageURL string) {
    fmt.Println("-----------------------------------------")
    fmt.Println("DETECT IMAGE TYPES - remote")
    fmt.Println()
    var remoteImage computervision.ImageURL
    remoteImage.URL = &remoteImageURL

    features := []computervision.VisualFeatureTypes{computervision.VisualFeatureTypesImageType}

    imageAnalysis, err := client.AnalyzeImage(
            computerVisionContext,
            remoteImage,
            features,
            []computervision.Details{},
            "")
    if err != nil { log.Fatal(err) }

    fmt.Println("Image type of remote image:")

    fmt.Println("\nClip art type: ")
    switch *imageAnalysis.ImageType.ClipArtType {
    case 0:
        fmt.Println("Image is not clip art.")
    case 1:
        fmt.Println("Image is ambiguously clip art.")
    case 2:
        fmt.Println("Image is normal clip art.")
    case 3:
        fmt.Println("Image is good clip art.")
    }

    fmt.Println("\nLine drawing type: ")
    if *imageAnalysis.ImageType.LineDrawingType == 1 {
        fmt.Println("Image is a line drawing.")
    }   else {
        fmt.Println("Image is not a line drawing.")
    }
    fmt.Println()
}

读取印刷体文本和手写文本Read printed and handwritten text

计算机视觉可以读取图像中的可见文本,并将其转换为字符流。Computer Vision can read visible text in an image and convert it to a character stream. 本部分的代码定义了函数 RecognizeTextReadAPIRemoteImage,该函数使用客户端对象检测并提取图像中的印刷体文本或手写文本。The code in this section defines a function, RecognizeTextReadAPIRemoteImage, which uses the client object to detect and extract printed or handwritten text in the image.

main 函数中添加示例图像引用和函数调用。Add the sample image reference and function call in your main function.

// Analyze text in an image, remote
BatchReadFileRemoteImage(computerVisionClient, printedImageURL)

备注

还可以从本地图像提取文本。You can also extract text from a local image. 请参阅 GitHub 上的示例代码以了解涉及本地图像的方案。See the sample code on GitHub for scenarios involving local images.

调用读取 APICall the Read API

定义用于读取文本的新函数 RecognizeTextReadAPIRemoteImageDefine the new function for reading text, RecognizeTextReadAPIRemoteImage. 添加以下代码,该代码对给定图像调用 BatchReadFile 方法****。Add the code below, which calls the BatchReadFile method for the given image. 此方法返回一个操作 ID 并启动异步进程来读取图像的内容。This method returns an operation ID and starts an asynchronous process to read the content of the image.

func BatchReadFileRemoteImage(client computervision.BaseClient, remoteImageURL string) {
    fmt.Println("-----------------------------------------")
    fmt.Println("BATCH READ FILE - remote")
    fmt.Println()
    var remoteImage computervision.ImageURL
    remoteImage.URL = &remoteImageURL

    // The response contains a field called "Operation-Location", 
    // which is a URL with an ID that you'll use for GetReadOperationResult to access OCR results.
    textHeaders, err := client.BatchReadFile(computerVisionContext, remoteImage)
    if err != nil { log.Fatal(err) }

    // Use ExtractHeader from the autorest library to get the Operation-Location URL
    operationLocation := autorest.ExtractHeaderValue("Operation-Location", textHeaders.Response)

    numberOfCharsInOperationId := 36
    operationId := string(operationLocation[len(operationLocation)-numberOfCharsInOperationId : len(operationLocation)])

获取读取结果Get Read results

接下来,获取 BatchReadFile 调用返回的操作 ID,并将其用于 GetReadOperationResult 方法,向服务查询操作结果**** ****。Next, get the operation ID returned from the BatchReadFile call, and use it with the GetReadOperationResult method to query the service for operation results. 下面的代码每隔一秒钟检查一次操作,直到返回结果。The following code checks the operation at one-second intervals until the results are returned. 然后,它将提取的文本数据输出到控制台。It then prints the extracted text data to the console.

readOperationResult, err := client.GetReadOperationResult(computerVisionContext, operationId)
if err != nil { log.Fatal(err) }

// Wait for the operation to complete.
i := 0
maxRetries := 10

fmt.Println("Recognizing text in a remote image with the batch Read API ...")
for readOperationResult.Status != computervision.Failed &&
        readOperationResult.Status != computervision.Succeeded {
    if i >= maxRetries {
        break
    }
    i++

    fmt.Printf("Server status: %v, waiting %v seconds...\n", readOperationResult.Status, i)
    time.Sleep(1 * time.Second)

    readOperationResult, err = client.GetReadOperationResult(computerVisionContext, operationId)
    if err != nil { log.Fatal(err) }
}

显示读取结果Display Read results

添加以下代码来分析和显示检索到的文本数据,并完成函数定义。Add the following code to parse and display the retrieved text data, and finish the function definition.

// Display the results.
fmt.Println()
for _, recResult := range *(readOperationResult.RecognitionResults) {
    for _, line := range *recResult.Lines {
        fmt.Println(*line.Text)
    }
}

运行应用程序Run the application

从应用程序目录使用 go run 命令运行应用程序。Run the application from your application directory with the go run command.

go run sample-app.go

清理资源Clean up resources

如果想要清理并删除认知服务订阅,可以删除资源或资源组。If you want to clean up and remove a Cognitive Services subscription, you can delete the resource or resource group. 删除资源组同时也会删除与之相关联的任何其他资源。Deleting the resource group also deletes any other resources associated with it.

后续步骤Next steps