Nota
O acesso a esta página requer autorização. Pode tentar iniciar sessão ou alterar os diretórios.
O acesso a esta página requer autorização. Pode tentar alterar os diretórios.
Caution
Azure Vision 中的图像分析 4.0 服务已弃用,将于 2028 年 9 月 25 日停用,之后对服务的调用将失败。 建议切换到 迁移指南中概述的可用替代方法之一。
本文介绍如何使用图像分析 REST API 或客户端库设置基本图像标记脚本。 分析图像服务提供用于处理图像并返回有关其视觉特征的信息的 AI 算法。 请按照以下步骤将包安装到应用程序中并试用示例代码。
使用适用于 C# 的图像分析客户端库分析内容标记的图像。 本快速入门定义了一个方法, AnalyzeImageUrl该方法使用客户端对象分析远程图像并打印结果。
Tip
还可以分析本地图像。 请参阅 ComputerVisionClient 方法,例如 AnalyzeImageInStreamAsync。 或者,请参阅 GitHub 上的示例代码,了解涉及本地图像的方案。
Tip
除了生成图像标记之外,分析图像 API 还可执行许多不同的操作。 有关展示所有可用功能的示例,请参阅 图像分析操作指南 。
Prerequisites
- 一份 Azure 订阅。 可以创建一个试用帐户
- Visual Studio IDE 或最新版本的 .NET Core。
- 拥有 Azure 订阅后,请在 Azure 门户中创建计算机视觉资源 ,以获取密钥和终结点。 部署后,选择转到资源。
- 需要创建的资源中的密钥和终结点才能将应用程序连接到 Azure Vision。
- 可以使用免费定价层 (
F0) 试用该服务,然后再升级到付费层进行生产。
创建环境变量
在此示例中,将凭据写入运行应用程序的本地计算机上的环境变量。
转到 Azure 门户。 如果您在先决条件部分创建的资源已成功部署,请在后续步骤下选择转到资源。 可以在“密钥和终结点”页上的资源管理下找到密钥和终结点。 你的资源密钥与你的 Azure 订阅 ID 不同。
若要为密钥和终结点设置环境变量,请打开控制台窗口,并按照操作系统和开发环境的说明进行操作。
- 若要设置
VISION_KEY环境变量,请将<your_key>替换为资源的其中一个密钥。 - 若要设置
VISION_ENDPOINT环境变量,请将<your_endpoint>替换为资源的终结点。
Important
我们建议使用 Microsoft Entra ID 和 Azure 资源的管理标识进行身份验证,以避免将凭据存储在运行于云中的应用程序中。
请谨慎使用 API 密钥。 请不要直接在代码中包含 API 密钥,并且切勿公开发布该密钥。 如果使用 API 密钥,请将其安全地存储在 Azure 密钥保管库 中,定期轮换密钥,并使用基于角色的访问控制和网络访问限制来限制对 Azure 密钥保管库 的访问。
有关 AI 服务安全性的详细信息,请参阅 对 Azure AI 服务请求进行身份验证。
setx VISION_KEY <your_key>
setx VISION_ENDPOINT <your_endpoint>
添加环境变量后,可能需要重启任何正在运行的、将读取环境变量的程序(包括控制台窗口)。
分析图像
创建一个新的 C# 应用程序。
使用 Visual Studio 创建新的 .NET Core 应用程序。
安装客户端库
创建新项目后,右键单击 解决方案资源管理器 中的项目解决方案并选择 Manage NuGet 包来安装客户端库。 在打开的包管理器中,选择浏览,勾选包括预发行版,然后搜索
Microsoft.Azure.CognitiveServices.Vision.ComputerVision。 选择版本7.0.0,然后选择 “安装”。在首选的编辑器或 IDE 中,从项目目录打开 Program.cs 文件。 粘贴以下代码。
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;
using System.Threading;
using System.Linq;
namespace ComputerVisionQuickstart
{
class Program
{
// Add your Computer Vision key and endpoint
static string key = Environment.GetEnvironmentVariable("VISION_KEY");
static string endpoint = Environment.GetEnvironmentVariable("VISION_ENDPOINT");
// URL image used for analyzing an image (image of puppy)
private const string ANALYZE_URL_IMAGE = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/landmark.jpg";
static void Main(string[] args)
{
Console.WriteLine("Azure Cognitive Services Computer Vision - .NET quickstart example");
Console.WriteLine();
// Create a client
ComputerVisionClient client = Authenticate(endpoint, key);
// Analyze an image to get features and other properties.
AnalyzeImageUrl(client, ANALYZE_URL_IMAGE).Wait();
}
/*
* 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;
}
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.Tags
};
Console.WriteLine($"Analyzing the image {Path.GetFileName(imageUrl)}...");
Console.WriteLine();
// Analyze the URL image
ImageAnalysis results = await client.AnalyzeImageAsync(imageUrl, visualFeatures: features);
// Image tags and their confidence score
Console.WriteLine("Tags:");
foreach (var tag in results.Tags)
{
Console.WriteLine($"{tag.Name} {tag.Confidence}");
}
Console.WriteLine();
}
}
}
Important
我们建议使用 Microsoft Entra ID 和 Azure 资源的管理标识进行身份验证,以避免将凭据存储在运行于云中的应用程序中。
请谨慎使用 API 密钥。 请不要直接在代码中包含 API 密钥,并且切勿公开发布该密钥。 如果使用 API 密钥,请将其安全地存储在 Azure 密钥保管库 中,定期轮换密钥,并使用基于角色的访问控制和网络访问限制来限制对 Azure 密钥保管库 的访问。
有关 AI 服务安全性的详细信息,请参阅 对 Azure AI 服务请求进行身份验证。
运行应用程序
单击 IDE 窗口顶部的 “调试 ”按钮运行应用程序。
Output
操作的输出应类似于以下示例。
----------------------------------------------------------
ANALYZE IMAGE - URL
Analyzing the image sample16.png...
Tags:
grass 0.9957543611526489
dog 0.9939157962799072
mammal 0.9928356409072876
animal 0.9918001890182495
dog breed 0.9890419244766235
pet 0.974603533744812
outdoor 0.969241738319397
companion dog 0.906731367111206
small greek domestic dog 0.8965123891830444
golden retriever 0.8877675533294678
labrador retriever 0.8746421337127686
puppy 0.872604250907898
ancient dog breeds 0.8508287668228149
field 0.8017748594284058
retriever 0.6837497353553772
brown 0.6581960916519165
清理资源
如果想要清理并移除 Azure AI 服务订阅,可以删除资源或资源组。 删除资源组同时也会删除与之相关联的任何其他资源。
- Azure 门户
- Azure CLI
相关内容
在本快速入门中了解了安装图像分析客户端库和进行基本的图像分析调用的方法。 接下来,详细了解图像分析 API 功能。
使用Python图像分析客户端库分析远程图像以获得内容标签。
Tip
还可以分析本地图像。 请参阅 ComputerVisionClientOperationsMixin 方法,例如 analyze_image_in_stream。 或者,请参阅 GitHub 上的 示例代码,了解有关涉及本地图像的方案。
Tip
除了生成图像标记之外,分析图像 API 还可执行许多不同的操作。 有关展示所有可用功能的示例,请参阅 图像分析操作指南 。
Prerequisites
- 一份 Azure 订阅。 可以创建一个试用帐户
-
Python 3.x。
- 你的 Python 安装应包含 pip。 可以通过在命令行上运行
pip --version来检查是否安装了 pip。 要获取 pip,请安装最新版本的 Python。
- 你的 Python 安装应包含 pip。 可以通过在命令行上运行
- 拥有 Azure 订阅后,请在 Azure 门户中创建计算机视觉资源 ,以获取密钥和终结点。 部署后,选择转到资源。
- 需要创建的资源中的密钥和终结点才能将应用程序连接到 Azure Vision。
- 可以使用免费定价层 (
F0) 试用该服务,然后再升级到付费层进行生产。
创建环境变量
在此示例中,将凭据写入运行应用程序的本地计算机上的环境变量。
转到 Azure 门户。 如果您在先决条件部分创建的资源已成功部署,请在后续步骤下选择转到资源。 可以在“密钥和终结点”页上的资源管理下找到密钥和终结点。 你的资源密钥与你的 Azure 订阅 ID 不同。
若要为密钥和终结点设置环境变量,请打开控制台窗口,并按照操作系统和开发环境的说明进行操作。
- 若要设置
VISION_KEY环境变量,请将<your_key>替换为资源的其中一个密钥。 - 若要设置
VISION_ENDPOINT环境变量,请将<your_endpoint>替换为资源的终结点。
Important
我们建议使用 Microsoft Entra ID 和 Azure 资源的管理标识进行身份验证,以避免将凭据存储在运行于云中的应用程序中。
请谨慎使用 API 密钥。 请不要直接在代码中包含 API 密钥,并且切勿公开发布该密钥。 如果使用 API 密钥,请将其安全地存储在 Azure 密钥保管库 中,定期轮换密钥,并使用基于角色的访问控制和网络访问限制来限制对 Azure 密钥保管库 的访问。
有关 AI 服务安全性的详细信息,请参阅 对 Azure AI 服务请求进行身份验证。
setx VISION_KEY <your_key>
setx VISION_ENDPOINT <your_endpoint>
添加环境变量后,可能需要重启任何正在运行的、将读取环境变量的程序(包括控制台窗口)。
分析图像
安装客户端库。
可使用以下方式安装客户端库:
pip install --upgrade azure-cognitiveservices-vision-computervision同时,安装 Pillow 库。
pip install pillow创建一个新的 Python 应用程序。
创建新的 Python 文件。 例如,可以将其命名为 quickstart-file.py。
在文本编辑器或 IDE 中打开 quickstart-file.py 并粘贴以下代码。
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes
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
'''
Authenticate
Authenticates your credentials and creates a client.
'''
subscription_key = os.environ["VISION_KEY"]
endpoint = os.environ["VISION_ENDPOINT"]
computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))
'''
END - Authenticate
'''
'''
Quickstart variables
These variables are shared by several examples
'''
# Images used for the examples: Describe an image, Categorize an image, Tag an image,
# Detect faces, Detect adult or racy content, Detect the color scheme,
# Detect domain-specific content, Detect image types, Detect objects
images_folder = os.path.join (os.path.dirname(os.path.abspath(__file__)), "images")
remote_image_url = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/landmark.jpg"
'''
END - Quickstart variables
'''
'''
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))
print()
'''
END - Tag an Image - remote
'''
print("End of Computer Vision quickstart.")
使用快速入门文件中的
python命令运行应用程序。python quickstart-file.py
Output
操作的输出应类似于以下示例。
===== Tag an image - remote =====
Tags in the remote image:
'outdoor' with confidence 99.00%
'building' with confidence 98.81%
'sky' with confidence 98.21%
'stadium' with confidence 98.17%
'ancient rome' with confidence 96.16%
'ruins' with confidence 95.04%
'amphitheatre' with confidence 93.99%
'ancient roman architecture' with confidence 92.65%
'historic site' with confidence 89.55%
'ancient history' with confidence 89.54%
'history' with confidence 86.72%
'archaeological site' with confidence 84.41%
'travel' with confidence 65.85%
'large' with confidence 61.02%
'city' with confidence 56.57%
End of Azure Vision quickstart.
清理资源
如果想要清理并移除 Azure AI 服务订阅,可以删除资源或资源组。 删除资源组同时也会删除与之相关联的任何其他资源。
- Azure 门户
- Azure CLI
后续步骤
在本快速入门中了解了安装图像分析客户端库和进行基本的图像分析调用的方法。 接下来,详细了解分析图像 API 功能。
使用 Java 的图像分析客户端库分析远程图像的标记、文本说明、人脸、成人内容等。
Tip
还可以分析本地图像。 请参阅 ComputerVision 方法,例如 AnalyzeImage。 或者,请参阅 GitHub 上的 示例代码,了解有关涉及本地图像的方案。
Tip
除了生成图像标记之外,分析图像 API 还可执行许多不同的操作。 有关展示所有可用功能的示例,请参阅 图像分析操作指南 。
参考文档 | 库源代码 |Artifact(Maven) | 示例
Prerequisites
- 一份 Azure 订阅。 可以创建一个试用帐户
- Java开发工具包(JDK)的当前版本。
- Gradle 生成工具或其他依赖项管理器。
- 拥有 Azure 订阅后,请在 Azure 门户中创建计算机视觉资源 ,以获取密钥和终结点。 部署后,选择转到资源。
- 需要创建的资源中的密钥和终结点才能将应用程序连接到 Azure Vision。
- 可以使用免费定价层 (
F0) 试用该服务,然后再升级到付费层进行生产。
创建环境变量
在此示例中,将凭据写入运行应用程序的本地计算机上的环境变量。
转到 Azure 门户。 如果您在先决条件部分创建的资源已成功部署,请在后续步骤下选择转到资源。 可以在“密钥和终结点”页上的资源管理下找到密钥和终结点。 你的资源密钥与你的 Azure 订阅 ID 不同。
若要为密钥和终结点设置环境变量,请打开控制台窗口,并按照操作系统和开发环境的说明进行操作。
- 若要设置
VISION_KEY环境变量,请将<your_key>替换为资源的其中一个密钥。 - 若要设置
VISION_ENDPOINT环境变量,请将<your_endpoint>替换为资源的终结点。
Important
我们建议使用 Microsoft Entra ID 和 Azure 资源的管理标识进行身份验证,以避免将凭据存储在运行于云中的应用程序中。
请谨慎使用 API 密钥。 请不要直接在代码中包含 API 密钥,并且切勿公开发布该密钥。 如果使用 API 密钥,请将其安全地存储在 Azure 密钥保管库 中,定期轮换密钥,并使用基于角色的访问控制和网络访问限制来限制对 Azure 密钥保管库 的访问。
有关 AI 服务安全性的详细信息,请参阅 对 Azure AI 服务请求进行身份验证。
setx VISION_KEY <your_key>
setx VISION_ENDPOINT <your_endpoint>
添加环境变量后,可能需要重启任何正在运行的、将读取环境变量的程序(包括控制台窗口)。
分析图像
创建一个新的 Gradle 项目。
在控制台窗口(例如 cmd、PowerShell 或 Bash)中,为应用创建一个新目录并导航到该目录。
mkdir myapp && cd myapp在工作目录中运行
gradle init命令。 此命令将创建Gradle的重要构建文件,包括build.gradle.kts,该文件将在运行时用于创建和配置您的应用程序。gradle init --type basic当系统提示选择 DSL 时,请选择 Kotlin。
安装客户端库。
本快速入门使用 Gradle 依赖项管理器。 可以在 Maven 中央存储库中找到其他依赖项管理器的客户端库和信息。
找到 build.gradle.kts ,并使用首选的 IDE 或文本编辑器将其打开。 然后,将以下生成配置复制粘贴到文件中。 此配置将项目定义为一个 Java 应用程序,其入口点为
ImageAnalysisQuickstart类。 它导入 Azure 视觉库。plugins { java application } application { mainClass.set("ImageAnalysisQuickstart") } repositories { mavenCentral() } dependencies { implementation(group = "com.microsoft.azure.cognitiveservices", name = "azure-cognitiveservices-computervision", version = "1.0.9-beta") }创建 Java 文件。
从工作目录运行以下命令,以创建项目源文件夹:
mkdir -p src/main/java导航到新文件夹并创建名为 ImageAnalysisQuickstart.java的文件。
在首选编辑器或 IDE 中打开 ImageAnalysisQuickstart.java 并粘贴以下代码。
import com.microsoft.azure.cognitiveservices.vision.computervision.*;
import com.microsoft.azure.cognitiveservices.vision.computervision.implementation.ComputerVisionImpl;
import com.microsoft.azure.cognitiveservices.vision.computervision.models.*;
import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class ImageAnalysisQuickstart {
// Use environment variables
static String key = System.getenv("VISION_KEY");
static String endpoint = System.getenv("VISION_ENDPOINT");
public static void main(String[] args) {
System.out.println("\nAzure Cognitive Services Computer Vision - Java Quickstart Sample");
// Create an authenticated Computer Vision client.
ComputerVisionClient compVisClient = Authenticate(key, endpoint);
// Analyze local and remote images
AnalyzeRemoteImage(compVisClient);
}
public static ComputerVisionClient Authenticate(String key, String endpoint){
return ComputerVisionManager.authenticate(key).withEndpoint(endpoint);
}
public static void AnalyzeRemoteImage(ComputerVisionClient compVisClient) {
/*
* Analyze an image from a URL:
*
* Set a string variable equal to the path of a remote image.
*/
String pathToRemoteImage = "https://github.com/Azure-Samples/cognitive-services-sample-data-files/raw/master/ComputerVision/Images/faces.jpg";
// This list defines the features to be extracted from the image.
List<VisualFeatureTypes> featuresToExtractFromRemoteImage = new ArrayList<>();
featuresToExtractFromRemoteImage.add(VisualFeatureTypes.TAGS);
System.out.println("\n\nAnalyzing an image from a URL ...");
try {
// Call the Computer Vision service and tell it to analyze the loaded image.
ImageAnalysis analysis = compVisClient.computerVision().analyzeImage().withUrl(pathToRemoteImage)
.withVisualFeatures(featuresToExtractFromRemoteImage).execute();
// 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());
}
}
catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
// END - Analyze an image from a URL.
}
导航回项目根文件夹,然后使用以下命令生成应用:
gradle build使用以下命令运行它:
gradle run
Output
操作的输出应类似于以下示例。
Azure Vision - Java Quickstart Sample
Analyzing an image from a URL ...
Tags:
'person' with confidence 0.998895
'human face' with confidence 0.997437
'smile' with confidence 0.991973
'outdoor' with confidence 0.985962
'happy' with confidence 0.969785
'clothing' with confidence 0.961570
'friendship' with confidence 0.946441
'tree' with confidence 0.917331
'female person' with confidence 0.890976
'girl' with confidence 0.888741
'social group' with confidence 0.872044
'posing' with confidence 0.865493
'adolescent' with confidence 0.857371
'love' with confidence 0.852553
'laugh' with confidence 0.850097
'people' with confidence 0.849922
'lady' with confidence 0.844540
'woman' with confidence 0.818172
'group' with confidence 0.792975
'wedding' with confidence 0.615252
'dress' with confidence 0.517169
清理资源
如果想要清理并移除 Azure AI 服务订阅,可以删除资源或资源组。 删除资源组同时也会删除与之相关联的任何其他资源。
- Azure 门户
- Azure CLI
后续步骤
在本快速入门中了解了安装图像分析客户端库和进行基本的图像分析调用的方法。 接下来,详细了解分析图像 API 功能。
使用适用于 JavaScript 的图像分析客户端库来分析内容标记的远程图像。
Tip
还可以分析本地图像。 请参阅 ComputerVisionClient 方法,例如 describeImageInStream。 或者,请参阅 GitHub 上的 示例代码,了解有关涉及本地图像的方案。
Tip
除了生成图像标记之外,分析图像 API 还可执行许多不同的操作。 有关展示所有可用功能的示例,请参阅 图像分析操作指南 。
Prerequisites
- 一份 Azure 订阅。 可以创建一个试用帐户
- Node.js 的当前 版本。
- 拥有 Azure 订阅后,请在 Azure 门户中创建计算机视觉资源 ,以获取密钥和终结点。 部署后,选择转到资源。
- 需要创建的资源中的密钥和终结点才能将应用程序连接到 Azure Vision。
- 可以使用免费定价层 (
F0) 试用该服务,然后再升级到付费层进行生产。
创建环境变量
在此示例中,将凭据写入运行应用程序的本地计算机上的环境变量。
转到 Azure 门户。 如果您在先决条件部分创建的资源已成功部署,请在后续步骤下选择转到资源。 可以在“密钥和终结点”页上的资源管理下找到密钥和终结点。 你的资源密钥与你的 Azure 订阅 ID 不同。
若要为密钥和终结点设置环境变量,请打开控制台窗口,并按照操作系统和开发环境的说明进行操作。
- 若要设置
VISION_KEY环境变量,请将<your_key>替换为资源的其中一个密钥。 - 若要设置
VISION_ENDPOINT环境变量,请将<your_endpoint>替换为资源的终结点。
Important
我们建议使用 Microsoft Entra ID 和 Azure 资源的管理标识进行身份验证,以避免将凭据存储在运行于云中的应用程序中。
请谨慎使用 API 密钥。 请不要直接在代码中包含 API 密钥,并且切勿公开发布该密钥。 如果使用 API 密钥,请将其安全地存储在 Azure 密钥保管库 中,定期轮换密钥,并使用基于角色的访问控制和网络访问限制来限制对 Azure 密钥保管库 的访问。
有关 AI 服务安全性的详细信息,请参阅 对 Azure AI 服务请求进行身份验证。
setx VISION_KEY <your_key>
setx VISION_ENDPOINT <your_endpoint>
添加环境变量后,可能需要重启任何正在运行的、将读取环境变量的程序(包括控制台窗口)。
分析图像
创建新的 Node.js 应用程序
在控制台窗口(例如 cmd、PowerShell 或 Bash)中,为应用创建一个新目录并导航到该目录。
mkdir myapp && cd myapp运行
npm init命令以使用 package.json 文件创建一个 node 应用程序。npm init安装客户端库
安装
ms-rest-azure和@azure/cognitiveservices-computervisionnpm 包:npm install @azure/cognitiveservices-computervision同时安装异步模块:
npm install async您应用的
package.json文件已更新,包含这些依赖项。创建新文件 ,index.js。
在文本编辑器中打开 index.js 并粘贴以下代码。
'use strict';
const async = require('async');
const fs = require('fs');
const https = require('https');
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;
/**
* AUTHENTICATE
* This single client is used for all examples.
*/
const key = process.env.VISION_KEY;
const endpoint = process.env.VISION_ENDPOINT;
const computerVisionClient = new ComputerVisionClient(
new ApiKeyCredentials({ inHeader: { 'Ocp-Apim-Subscription-Key': key } }), endpoint);
/**
* END - Authenticate
*/
function computerVision() {
async.series([
async function () {
/**
* DETECT TAGS
* Detects tags for an image, which returns:
* all objects in image and confidence score.
*/
console.log('-------------------------------------------------');
console.log('DETECT TAGS');
console.log();
// Image of different kind of dog.
const tagsURL = 'https://github.com/Azure-Samples/cognitive-services-sample-data-files/blob/master/ComputerVision/Images/house.jpg';
// Analyze URL image
console.log('Analyzing tags in image...', tagsURL.split('/').pop());
const tags = (await computerVisionClient.analyzeImage(tagsURL, { visualFeatures: ['Tags'] })).tags;
console.log(`Tags: ${formatTags(tags)}`);
// Format tags for display
function formatTags(tags) {
return tags.map(tag => (`${tag.name} (${tag.confidence.toFixed(2)})`)).join(', ');
}
/**
* END - Detect Tags
*/
console.log();
console.log('-------------------------------------------------');
console.log('End of quickstart.');
},
function () {
return new Promise((resolve) => {
resolve();
})
}
], (err) => {
throw (err);
});
}
computerVision();
使用快速入门文件中的
node命令运行应用程序。node index.js
Output
操作的输出应类似于以下示例。
-------------------------------------------------
DETECT TAGS
Analyzing tags in image... sample16.png
Tags: grass (1.00), dog (0.99), mammal (0.99), animal (0.99), dog breed (0.99), pet (0.97), outdoor (0.97), companion dog (0.91), small greek domestic dog (0.90), golden retriever (0.89), labrador retriever (0.87), puppy (0.87), ancient dog breeds (0.85), field (0.80), retriever (0.68), brown (0.66)
-------------------------------------------------
End of quickstart.
清理资源
如果想要清理并移除 Azure AI 服务订阅,可以删除资源或资源组。 删除资源组同时也会删除与之相关联的任何其他资源。
- Azure 门户
- Azure CLI
后续步骤
在本快速入门中了解了安装图像分析客户端库和进行基本的图像分析调用的方法。 接下来,详细了解分析图像 API 功能。
使用图像分析 REST API 来分析图像的标记。
Tip
除了生成图像标记之外,分析图像 API 还可执行许多不同的操作。 有关展示所有可用功能的示例,请参阅 图像分析操作指南 。
Note
此快速入门使用 cURL 命令来调用 REST API。 也可以使用编程语言调用 REST API。 请参阅 GitHub 示例,查看 C#、Python、Java 和 JavaScript 的相关示例。
Prerequisites
- 一份 Azure 订阅。 可以创建一个试用帐户
- 拥有 Azure 订阅后,请在 Azure 门户中创建计算机视觉资源 ,以获取密钥和终结点。 部署后,选择转到资源。
- 需要创建的资源中的密钥和终结点才能将应用程序连接到 Azure Vision。
- 可以使用免费定价层 (
F0) 试用该服务,然后再升级到付费层进行生产。
- 已安装 cURL。
分析图像
若要分析图像的各种视觉特征,请执行以下步骤:
将以下命令复制到文本编辑器中。
curl.exe -H "Ocp-Apim-Subscription-Key: <yourKey>" -H "Content-Type: application/json" "https://chinaeast2.api.cognitive.azure.cn/vision/v3.2/analyze?visualFeatures=Tags" -d "{'url':'https://learn.microsoft.com/azure/ai-services/computer-vision/media/quickstarts/presentation.png'}"根据需要在命令中进行以下更改:
- 将
<yourKey>的值替换为计算机视觉资源密钥。 - 将请求 URL (
api.cognitive.azure.cn) 的第一部分替换为自己的终结点 URL。Note
2019 年 7 月 1 日之后创建的新资源将使用自定义子域名。 有关详细信息和区域终结点的完整列表,请参阅 Azure AI 服务的自定义子域名。
- (可选)将请求正文中的图像 URL (
https://learn.microsoft.com/azure/ai-services/computer-vision/media/quickstarts/presentation.png) 更改为要分析的其他图像的 URL。
- 将
打开命令提示符窗口。
将文本编辑器中编辑的
curl命令粘贴到命令提示符窗口,然后运行命令。
检查响应
成功的响应以 JSON 格式返回。 示例应用程序会在命令提示符窗口中分析和显示成功响应,如下例所示:
{
"tags":[
{
"name":"text",
"confidence":0.9992657899856567
},
{
"name":"post-it note",
"confidence":0.9879657626152039
},
{
"name":"handwriting",
"confidence":0.9730165004730225
},
{
"name":"rectangle",
"confidence":0.8658561706542969
},
{
"name":"paper product",
"confidence":0.8561884760856628
},
{
"name":"purple",
"confidence":0.5961999297142029
}
],
"requestId":"2788adfc-8cfb-43a5-8fd6-b3a9ced35db2",
"metadata":{
"height":945,
"width":1000,
"format":"Jpeg"
},
"modelVersion":"2021-05-01"
}
后续步骤
在本快速入门中,你学习了如何使用 REST API 进行基本的图像分析调用。 接下来,详细了解分析图像 API 功能。