快速入门:使用计算机视觉 REST API 和 Node.js 提取印刷体文本 (OCR)Quickstart: Extract printed text (OCR) using the Computer Vision REST API and Node.js

本快速入门将使用计算机视觉 REST API 通过光学字符识别 (OCR) 从图像中提取印刷体文本。In this quickstart, you'll extract printed text with optical character recognition (OCR) from an image using the Computer Vision REST API. 借助 OCR 方法,可检测图像中的印刷体文本,并将识别的字符提取到计算机可用的字符流中。With the OCR method, you can detect printed text in an image and extract recognized characters into a machine-usable character stream.

先决条件Prerequisites

  • Azure 订阅 - 创建试用订阅An Azure subscription - Create one for trial
  • Node.js 4.x 或更高版本Node.js 4.x or later
  • npmnpm
  • 拥有 Azure 订阅后,在 Azure 门户中创建计算机视觉资源 ,获取密钥和终结点。Once you have your Azure subscription, create a Computer Vision resource in the Azure portal to get your key and endpoint. 部署后,单击“转到资源”。After it deploys, click Go to resource.
    • 需要从创建的资源获取密钥和终结点,以便将应用程序连接到计算机视觉服务。You will need the key and endpoint from the resource you create to connect your application to the Computer Vision service. 你稍后会在快速入门中将密钥和终结点粘贴到下方的代码中。You'll paste your key and endpoint into the code below later in the quickstart.
    • 可以使用免费定价层 (F0) 试用该服务,然后再升级到付费层进行生产。You can use the free pricing tier (F0) to try the service, and upgrade later to a paid tier for production.
  • 为密钥和终结点 URL 创建环境变量,分别将其命名为 COMPUTER_VISION_SUBSCRIPTION_KEYCOMPUTER_VISION_ENDPOINTCreate environment variables for the key and endpoint URL, named COMPUTER_VISION_SUBSCRIPTION_KEY and COMPUTER_VISION_ENDPOINT, respectively.

创建并运行示例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. (可选)将 imageUrl 的值替换为要从中提取印刷体文本的另一图像的 URL。Optionally, replace the value of imageUrl with the URL of a different image from which you want to extract printed text.

  4. 将代码保存为以 .js 为扩展名的文件。Save the code as a file with a .js extension. 例如,get-printed-text.jsFor example, get-printed-text.js.

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

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

'use strict';

const request = require('request');

let subscriptionKey = process.env['COMPUTER_VISION_SUBSCRIPTION_KEY'];
let endpoint = process.env['COMPUTER_VISION_ENDPOINT']
if (!subscriptionKey) { throw new Error('Set your environment variables for your subscription key and endpoint.'); }

var uriBase = endpoint + 'vision/v3.0/ocr';

const imageUrl = 'https://upload.wikimedia.org/wikipedia/commons/thumb/a/af/' +
    'Atomist_quote_from_Democritus.png/338px-Atomist_quote_from_Democritus.png';

// Request parameters.
const params = {
    'language': 'unk',
    'detectOrientation': 'true',
};

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:

{
  "language": "en",
  "orientation": "Up",
  "textAngle": 0,
  "regions": [
    {
      "boundingBox": "21,16,304,451",
      "lines": [
        {
          "boundingBox": "28,16,288,41",
          "words": [
            {
              "boundingBox": "28,16,288,41",
              "text": "NOTHING"
            }
          ]
        },
        {
          "boundingBox": "27,66,283,52",
          "words": [
            {
              "boundingBox": "27,66,283,52",
              "text": "EXISTS"
            }
          ]
        },
        {
          "boundingBox": "27,128,292,49",
          "words": [
            {
              "boundingBox": "27,128,292,49",
              "text": "EXCEPT"
            }
          ]
        },
        {
          "boundingBox": "24,188,292,54",
          "words": [
            {
              "boundingBox": "24,188,292,54",
              "text": "ATOMS"
            }
          ]
        },
        {
          "boundingBox": "22,253,297,32",
          "words": [
            {
              "boundingBox": "22,253,105,32",
              "text": "AND"
            },
            {
              "boundingBox": "144,253,175,32",
              "text": "EMPTY"
            }
          ]
        },
        {
          "boundingBox": "21,298,304,60",
          "words": [
            {
              "boundingBox": "21,298,304,60",
              "text": "SPACE."
            }
          ]
        },
        {
          "boundingBox": "26,387,294,37",
          "words": [
            {
              "boundingBox": "26,387,210,37",
              "text": "Everything"
            },
            {
              "boundingBox": "249,389,71,27",
              "text": "else"
            }
          ]
        },
        {
          "boundingBox": "127,431,198,36",
          "words": [
            {
              "boundingBox": "127,431,31,29",
              "text": "is"
            },
            {
              "boundingBox": "172,431,153,36",
              "text": "opinion."
            }
          ]
        }
      ]
    }
  ]
}

清理资源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。Next, explore the Computer Vision APIs used to analyze an image, detect celebrities and landmarks, create a thumbnail, and extract printed and handwritten text.