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

快速入门:适用于 Node.js 的计算机视觉客户端库Quickstart: Computer Vision client library for Node.js

适用于 Node.js 的计算机视觉客户端库入门。Get started with the Computer Vision client library for Node.js. 请按照以下步骤安装程序包并试用基本任务的示例代码。Follow these steps to install the package and try out the example code for basic tasks.

使用适用于 Node.js 的计算机视觉客户端库可以:Use the Computer Vision client library for Node.js to:

参考文档 | 库源代码 | 包 (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-azureazure-cognitiveservices-computervision NPM 包:Install the ms-rest-azure and azure-cognitiveservices-computervision NPM packages:

npm install azure-cognitiveservices-computervision ms-rest-azure

应用的 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 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.

NameName 说明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());
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/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/handwritten_text.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