gpt4 book ai didi

node.js - Amazon Rekognition 图像 : error InvalidImageFormatException: Request has invalid image format

转载 作者:行者123 更新时间:2023-12-03 12:17:13 24 4
gpt4 key购买 nike

我正在尝试比较从 Node.Js 应用程序调用 AWS Rekognition 的面孔。当比较 S3 存储桶上的两个图像时,一切正常,但是当我尝试从客户端(React Native/Expo 应用程序)上传本地镜像以与存储在此存储桶中的另一个图像进行比较时,我收到错误 InvalidImageFormatException: Request has invalid image format .
此图像是一个 jpeg 250px 正方形,并作为有效的 base64 字符串发送(经过测试)。显然,它满足这里提出的要求:https://docs.aws.amazon.com/rekognition/latest/dg/limits.html .
下面是一些代码片段:
捕获图像:

const takeImgHandler = async () => {
const img = await ImagePicker.launchCameraAsync(getImgProps);
editImg(img);
};
编辑图像:
const editImg = async img => {
...
const actions = [
{ resize: { 250, 250 } },
];

const saveOptions = {
base64: true,
};

const edited = await ImageManipulator.manipulateAsync(img.uri, actions, saveOptions);
setState({ ...state, img: edited });
};
将 detectFaces 调用设置为我的服务器:
// sourceImg is appState.img.base64
const compareImagesHandler = async sourceImg => {
const targetImage = {
S3Object: {
Bucket: 'my-bucket-name',
Name: 'image-name.jpg',
},
};

const sourceImage = {
Bytes: sourceImg,
};

const comparison = await ajax({ method: 'POST', url: `url-to-server-route`, data: { sourceImage, targetImage }});
console.log('comparison: >>>>>> ', comparison);
return comparison;
};
服务器 Controller 运行此功能:
const awsConfig = () => {
const config = new AWS.Config({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: process.env.AWS_DEFAULT_REGION,
});
AWS.config.update(config);
};

const compareImages = async (SourceImage, TargetImage, cb) => {
const client = new AWS.Rekognition();

// Logging the base64 string to validate it, externally, just to make
sure that it´s valid!
console.log('sourceImag.Bytes: >>>>>> ', SourceImage.Bytes);

const params = {
SourceImage,
TargetImage,
SimilarityThreshold: 50,
};

client.compareFaces(params, (err, response) => {
if (err) {
console.log('err: >>>>>> ', err);
return cb({ err });
}

if (!response.FaceMatches.length) {
return cb({ err: 'Face not recongized' });
}

response.FaceMatches.forEach(data => {
const position = data.Face.BoundingBox;
const similarity = data.Similarity;
console.log(`The face at: ${position.Left}, ${position.Top} matches with ${similarity} % confidence`);
return cb({ success: data.Similarity });
});
});
};

最佳答案

解决了!
需要进行两次调整。首先,对 sourceImg 进行编码文件使用 encodeURIComponent :const sourceImage = encodeURIComponent(sourceImg);在服务器上,我应该创建一个缓冲区,而不是发送 base64 字符串:const imageBuffer = Buffer.from(decodeURIComponent(SourceImage), 'base64');因此,发送到 AWS 的正文应该是:

const params = {    
SourceImage: {
Bytes: imageBuffer,
}
TargetImage,
SimilarityThreshold: 50,
};

关于node.js - Amazon Rekognition 图像 : error InvalidImageFormatException: Request has invalid image format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64789544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com