我在从NodeJ调用Cognitive Services时遇到问题。我是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
感谢您的帮助
答案 0 :(得分:0)
您不需要将图像转换为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
作为请求正文的结果。