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

快速入门:使用计算机视觉中的 REST API 和 Node.js 分析远程图像Quickstart: Analyze a remote image using the REST API with Node.js in Computer Vision

在本快速入门中,你将使用计算机视觉的 REST API 分析远程存储的图像以提取视觉特征。In this quickstart, you analyze a remotely stored image to extract visual features by using Computer Vision's REST API. 使用分析图像方法,可以根据图像内容提取视觉特征。With the Analyze Image method, you can extract visual features based on image content.

如果没有 Azure 订阅,请在开始之前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

先决条件Prerequisites

创建并运行示例Create and run the sample

要创建和运行示例,请执行以下步骤:To create and run the sample, do the following steps:

  1. 安装 npm request 包。Install the npm request package.

    1. 以管理员身份打开命令提示符窗口。Open a command prompt window as an administrator.

    2. 运行以下命令:Run the following command:

      npm install request
      
    3. 包安装成功后,关闭命令提示符窗口。After the package is successfully installed, close the command prompt window.

  2. 将以下代码复制到文本编辑器中。Copy the following code into a text editor.

  3. 必要时在代码中进行如下更改:Make the following changes in code where needed:

    1. subscriptionKey 的值替换为你的订阅密钥。Replace the value of subscriptionKey with your subscription key.
    2. 如有必要,请将 uriBase 的值替换为获取的订阅密钥所在的 Azure 区域中的分析图像方法的终结点 URL。Replace the value of uriBase with the endpoint URL for the Analyze Image method from the Azure region where you obtained your subscription keys, if necessary.
    3. (可选)将 imageUrl 的值替换为要分析的其他图像的 URL。Optionally, replace the value of imageUrl with the URL of a different image that you want to analyze.
    4. (可选)将 language 请求参数的值替换为其他语言。Optionally, replace the value of the language request parameter with a different language.
  4. 将代码保存为以 .js 为扩展名的文件。Save the code as a file with a .js extension. 例如,analyze-image.jsFor example, analyze-image.js.

  5. 打开命令提示符窗口。Open a command prompt window.

  6. 在提示符处,使用 node 命令运行文件。At the prompt, use the node command to run the file. 例如,node analyze-image.jsFor example, node analyze-image.js.

'use strict';

const request = require('request');

// Replace <Subscription Key> with your valid subscription key.
const subscriptionKey = '<Subscription Key>';

// You must use the same location in your REST call as you used to get your
// subscription keys. For example, if you got your subscription keys from
// westus, replace "westcentralus" in the URL below with "westus".
const uriBase =
    'https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze';

const imageUrl =
    'https://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg';

// Request parameters.
const params = {
    'visualFeatures': 'Categories,Description,Color',
    'details': '',
    'language': 'en'
};

const options = {
    uri: uriBase,
    qs: params,
    body: '{"url": ' + '"' + imageUrl + '"}',
    headers: {
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key' : subscriptionKey
    }
};

request.post(options, (error, response, body) => {
  if (error) {
    console.log('Error: ', error);
    return;
  }
  let jsonResponse = JSON.stringify(JSON.parse(body), null, '  ');
  console.log('JSON Response\n');
  console.log(jsonResponse);
});

检查响应Examine the response

成功的响应以 JSON 格式返回。A successful response is returned in JSON. 示例会在命令提示符窗口中分析和显示成功响应,如下例所示:The sample parses and displays a successful response in the command prompt window, similar to the following example:

{
  "categories": [
    {
      "name": "outdoor_water",
      "score": 0.9921875,
      "detail": {
        "landmarks": []
      }
    }
  ],
  "description": {
    "tags": [
      "nature",
      "water",
      "waterfall",
      "outdoor",
      "rock",
      "mountain",
      "rocky",
      "grass",
      "hill",
      "covered",
      "hillside",
      "standing",
      "side",
      "group",
      "walking",
      "white",
      "man",
      "large",
      "snow",
      "grazing",
      "forest",
      "slope",
      "herd",
      "river",
      "giraffe",
      "field"
    ],
    "captions": [
      {
        "text": "a large waterfall over a rocky cliff",
        "confidence": 0.916458423253597
      }
    ]
  },
  "color": {
    "dominantColorForeground": "Grey",
    "dominantColorBackground": "Green",
    "dominantColors": [
      "Grey",
      "Green"
    ],
    "accentColor": "4D5E2F",
    "isBwImg": false
  },
  "requestId": "81b4e400-e3c1-41f1-9020-e6871ad9f0ed",
  "metadata": {
    "height": 959,
    "width": 1280,
    "format": "Jpeg"
  }
}

清理资源Clean up resources

不再需要文件时,请将其删除,然后卸载 npm request 包。When no longer needed, delete the file, and then uninstall the npm request package. 要卸载包,请执行以下步骤:To uninstall the package, do the following steps:

  1. 以管理员身份打开命令提示符窗口。Open a command prompt window as an administrator.

  2. 运行以下命令:Run the following command:

    npm uninstall request
    
  3. 成功卸载包后,关闭命令提示符窗口。After the package is successfully uninstalled, close the command prompt window.

后续步骤Next steps

探索用于分析图像、检测名人和地标、创建缩略图以及提取印刷体文本和手写文本的计算机视觉 API。Explore the Computer Vision APIs used to analyze an image, detect celebrities and landmarks, create a thumbnail, and extract printed and handwritten text. 要快速体验计算机视觉 API,请尝试使用 Open API 测试控制台To rapidly experiment with the Computer Vision API, try the Open API testing console.