如何使用Google存储中的图像在Microsoft Azure的计算机视觉中修复InvalidImageFormat?

时间:2019-09-11 10:03:23

标签: javascript node.js azure google-cloud-platform

我正在从Google云存储中设置带有图像URL的Microsoft Azure的计算机视觉,但是每当我从Google云存储中上传照片时,响应都是“ InvalidImageFormat”,但是当我使用除Google云存储之外的图像URL时,它有效

我尝试更改网址并检查上传的图片的尺寸是否大于最大限制大小,但小于最大限制大小

const cognitiveServices = require('cognitive-services');
const fs = require('fs')

const parameters = {
    'visualFeatures': 'Description',
    'language': 'en'
};

const body = {"url" :'<from google storage>'}
const headers = { "content-type": "application/json" };

const checkImage = new cognitiveServices.computerVision({
    apiKey: "<api key>",
    endpoint: "<end point>"
})

checkImage.analyzeImage({
    parameters,
    body,
    headers
}).then(response => {
    console.log(response);
}).catch(error => {
    console.log(error)
})

我希望输出给出200响应,而不给出'InvalidImageFormat'的400状态代码

1 个答案:

答案 0 :(得分:0)

即使您在Google存储空间中设置了具有公共访问权限的内容,您获得的URL也不是文件的直接URL。

您可以先使用URL进行GET请求,然后将获得带有307状态代码的响应,并且将从响应标头中获得重定向URL:

enter image description here

有了新的URL,您可以获取图像,并将其用于您的js代码中:

enter image description here

结果:

enter image description here


建议:

您可以使用Azure存储。通过公共访问,您可以直接使用Blob的URL。这是供您测试的存储Blob文件:https://storagetest789.blob.core.windows.net/pub/p033tw9j.jpg