计算机视觉读取 api azure

时间:2021-05-13 17:25:54

标签: node.js azure ocr

我尝试了 Read api of azure 从图像/pdf (https://eastus.dev.cognitive.microsoft.com/docs/services/computer-vision-v3-2/operations/5d986960601faab4bf452005/console) 中读取文本并且它工作正常然后我尝试使用代码

var request = require('request');
var options = {
  'method': 'POST',
  'url': 'https://eastus.api.cognitive.microsoft.com/vision/v3.2/read/analyze?language=en&readingOrder=basic&model-version=latest',
  'headers': {
    'Host': 'eastus.api.cognitive.microsoft.com',
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': 'key'
  },
  body: JSON.stringify({"url":"url"})

};
request(options, function (error, response) {
  if (error) throw new Error(error);
  console.log("response",response.body);
});

response.body 没有返回任何值。有人可以帮我看看是什么问题吗?

1 个答案:

答案 0 :(得分:0)

它是按照 the doc 所示设计的:

<块引用>

当你调用 Read 操作时,调用返回一个响应 标题称为“操作位置”。 “操作位置”标题 包含要在第二步中使用的带有操作 ID 的 URL。在 第二步,你使用Get Read Result操作来获取 检测到文本行和单词作为 JSON 响应的一部分。

响应正文为空,您可以在响应头中Operation-Location

只需尝试以下代码即可获得 Operation-Location 和最终结果:

var request = require('request');

var region = ''
var key = ''
var imageUrl = ""

var options = {
  'method': 'POST',
  'url': `https://${region}.api.cognitive.microsoft.com/vision/v3.2/read/analyze?language=en&readingOrder=basic&model-version=latest`,
  'headers': {
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': key
  },
  body: JSON.stringify({"url":imageUrl})

};


request(options, function (error, response) {
    if (error) throw new Error(error);

    resultURL = response.headers['operation-location'];
    //print result URL
    console.log(resultURL)

    options.url= resultURL
    options.method='GET'

    //wait 5s to allow Azure process the image
    wait(5000).then(function(){
            request.get(options,function(error, result){
                console.log(result.body)
            });
        })
});

function wait(ms) {
    return new Promise(resolve => setTimeout(() => resolve(), ms));
};

结果:

enter image description here