计算机视觉 Read api azure

Computer vision Read api azure

我已经尝试使用 Read api of azure 来读取来自 image/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 没有返回任何值。谁能帮我看看可能是什么问题?

它是设计使然,如 the doc 所示:

When you call the Read operation, the call returns with a response header called 'Operation-Location'. The 'Operation-Location' header contains a URL with the Operation Id to be used in the second step. In the second step, you use the Get Read Result operation to fetch the detected text lines and words as part of the JSON response.

响应体为空,可以在响应头中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));
};

结果: