如何将图像传递到AWS SageMaker终端节点

时间:2020-03-30 11:35:25

标签: node.js amazon-web-services amazon-sagemaker

我需要使用AWS Marketplace中的WireframeToCode模型,我使用Nodejs读取文件数据并将其发送到该模型,如下所示:

var sageMakerRuntime = new AWS.SageMakerRuntime();

var bitmap = fs.readFileSync("sample.jpeg", "utf8");
var buffer = new Buffer.from(bitmap, "base64");

var params = {
  Body: buffer.toJSON(),
  EndpointName: "wireframe-to-code",
  Accept: "image/jpeg",
  ContentType: "application/json"
};

sageMakerRuntime.invokeEndpoint(params, function(err, data) {
  if (err) console.log(err, err.stack);
  else console.log(data);
});

但是我得到这个错误:

message:'预期参数。正文为字符串,Buffer,Stream,Blob, 或类型化数组对象”,代码:“ InvalidParameterType”,时间: 2020-03-30T11:06:27.535Z

根据文档,输入所支持的内容类型为image/jpeg,输出为application/json

当我尝试将Body转换为这样的字符串时:JSON.stringify(buffer.toJSON())我收到此错误:

从模型收到消息“此预测变量”的客户端错误(415) 仅支持JSON格式的数据”

2 个答案:

答案 0 :(得分:1)

所以我认为您需要修改传递给invokeEndpoint的有效负载

文档建议,尽管有效载荷需要为JSON,但在这种情况下,主体需要为缓冲区,并且将免费编码为base64。

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SageMakerRuntime.html#invokeEndpoint-property

const fs = require('fs')

const sageMakerRuntime = new AWS.SageMakerRuntime()

const bitmap = fs.readFileSync('sample.jpeg', 'utf8')
const buffer = new Buffer.from(bitmap)

const params = {
    Body: buffer, //Buffer, Typed Array, Blob, String
    EndpointName: 'wireframe-to-code',
    Accept: 'image/jpeg', //The desired MIME type of the inference in the response
    ContentType: 'image/jpeg', //MIME type of body
}

sageMakerRuntime.invokeEndpoint(params, function(err, data) {
    if (err) console.log(err, err.stack)
    else console.log(data)
})

答案 1 :(得分:1)

我必须传递位图并将ContentType更改为"image/jpeg"

const AWS = require("aws-sdk");
const fs = require("fs");

const sageMakerRuntime = new AWS.SageMakerRuntime({
  region: "us-east-1",
  accessKeyId: "XXXXXXXXXXXX",
  secretAccessKey: "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
});

const bitmap = fs.readFileSync("sample.jpeg");

var params = {
  Body: bitmap,
  EndpointName: "wireframe-to-code",
  ContentType: "image/jpeg"
};

sageMakerRuntime.invokeEndpoint(params, function(err, data) {
  if (err) {
    console.log(err, err.stack);
  } else {
    responseData = JSON.parse(Buffer.from(data.Body).toString());
    console.log(responseData);
  }
});