Node-red:使用 Analyze Image API 将本地图像发送到 Azure 的功能节点

Node-red: function node to send a local image to Azure with Analyze Image API

我想使用 Analyze Image API 将本地图像文件发送到 Azure 认知服务,以便在 node-red 中进行图像识别。这是我的节点:

函数节点中的代码是:

msg.payload = {"data" : "D:\TEMP2\tsaie.jpg"};
msg.headers = {
    "Ocp-Apim-Subscription-Key" : "439aa9b420e34cXXXXXXXXXXXX",
    "Content-Type" : "multipart/form-data"
}
return msg;

HTTP 请求节点是 POST 和 :

https://comvisonapi.cognitiveservices.azure.com/vision/v3.2/analyze?visualFeatures=Description,Faces

发送此请求后,出现错误:

msg.payload : string[168]
"{"error":{"code":"InvalidRequest","innererror":{"code":"InvalidImageFormat","message":"Input data is not a valid image."},"message":"Input data is not a valid image."}}"

你能帮我吗?功能节点中应该有什么正确的代码?非常感谢!!

您不能只将文件名作为 data 字段传递。 HTTP 请求节点将只发送该字符串,它不会从磁盘加载文件的内容。

您将需要使用文件节点来加载图像的内容,然后构建格式正确的负载对象。这是一个例子:

var fileData = msg.payload;
msg.headers = {
  "Ocp-Apim-Subscription-Key" : "439aa9b420e34cXXXXXXXXXXXX",
  'Content-Type': 'multipart/form-data'
};

msg.payload = {
    'image' : {
        'value': fileData,
        'options': {
            'filename': 'image.png'
        }
    }
};

return msg;

https://discourse.nodered.org/t/add-support-for-multipart-form-data-in-request-node/8111