节点 Js 微软认知服务

Node Js Microsoft Cognitive Services

我在从 NodeJs 调用认知服务时遇到问题。我是 Node 的新手,所以从直接 JavaScript 到 Node 的转换一直是一个挑战。当我使用互联网上的图像时它可以工作,但我需要使用本地服务器上的图像并尝试了很多东西来满足它作为原始二进制文件的要求。这是我一直在尝试的...

    const cognitiveServices = require('cognitive-services');
    var fs = require('fs');
    var apiKey = "myapikey";
    var apiUrl="https://westus.api.cognitive.microsoft.com/vision/v1.0/analyze"
    const FRIENDS_IMAGE_URL ='http://az616578.vo.msecnd.net/files/2016/10/09/636115830685164048-686058602_friends.jpg';

    const parameters ={ "visualFeatures": "Tags","details": "Celebrities","language": "en"}
    //var body = { "url": FRIENDS_IMAGE_URL };

   var body = { "url":  getlocalimage()};
   //var body = getlocalimage();

   function getlocalimage()
    {
      var fn = "test.jpg"
      var base64 = fs.readFileSync(fn).toString('base64');
      //  var myimage = "data:image/jpeg:base64," + base64;
      var myimage =  base64;
      return new Buffer(myimage, 'base64').toString('binary');
     }

    function cogtest()
      {
        const chkImage = new cognitiveServices.computerVision({
                     apiKey: apiKey,
                     endpoint: "westus.api.cognitive.microsoft.com"
      })

    chkImage.analyzeImage({  parameters,  body  })
      .then(response => { console.log(response); })
      .catch((err) => { console.log(err);});

      }

       cogtest();

由于使用来自网络的图像可以成功调用,而我不断收到的错误是:

   { StatusCodeError: 400 - {"code":"InvalidImageUrl","requestId":"96432967-59d8-4851-a039-1e5b281b4cb7","message":"Can't fetch the image."}
    at new StatusCodeError (/Users/Tim/Downloads/0PiNode/node_modules/request-promise-core/lib/errors.js:32:15)
    at /Users/Tim/Downloads/0PiNode/node_modules/request-promise-core/lib/plumbing.js:97:41
    at tryCatcher (/Users/Tim/Downloads/0PiNode/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/Tim/Downloads/0PiNode/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/Users/Tim/Downloads/0PiNode/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromiseCtx (/Users/Tim/Downloads/0PiNode/node_modules/bluebird/js/release/promise.js:606:10)
    at Async._drainQueue (/Users/Tim/Downloads/0PiNode/node_modules/bluebird/js/release/async.js:138:12)
    at Async._drainQueues (/Users/Tim/Downloads/0PiNode/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues [as _onImmediate] (/Users/Tim/Downloads/0PiNode/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:800:20)
    at tryOnImmediate (timers.js:762:5)
    at processImmediate [as _immediateCallback] (timers.js:733:5)
  name: 'StatusCodeError',
  statusCode: 400,
  message: '400 - {"code":"InvalidImageUrl","requestId":"96432967-59d8-4851-a039-1e5b281b4cb7","message":"Can\'t fetch the image."}',
  error: 
   { code: 'InvalidImageUrl',
     requestId: '96432967-59d8-4851-a039-1e5b281b4cb7',
     message: 'Can\'t fetch the image.' },
  options: 
   { uri: 'https://westus.api.cognitive.microsoft.com/vision/v1.0/analyze',
     method: 'POST',
     headers: 
      { 'Ocp-Apim-Subscription-Key': 'KEY' },
     qs: 
      { visualFeatures: 'Tags',
        details: 'Celebrities',
        language: 'en' },
     json: true,

有什么想法吗?我相信这与在体内传递原始二进制图像有关。 每个模块 https://github.com/joshbalfour/node-cognitive-services

https://github.com/joshbalfour/node-cognitive-services/tree/v0.2.0#computer-vision---analyze-image

感谢您的帮助

您不需要将图像转换为 base64 表示形式。你可以简单地这样做:

var parameters = {
    "visualFeatures": "Tags",
    "details": "Celebrities",
    "language": "en"
}

var body = fs.readFileSync("test.jpg");

var chkImage = new cognitiveServices.computerVision({
    apiKey: "myapikey",
    endpoint: "westus.api.cognitive.microsoft.com"
})

chkImage.analyzeImage({
    parameters,
    body
})
    .then(response => {
        console.log(response);
    })
    .catch((err) => {
        console.log(err);
    });

即,将fs.readFileSync的结果作为请求的正文发送过来。