我正在尝试比较从Node.Js应用程序调用AWS Rekognition的面孔。比较S3存储桶上的两个图像时,一切都很好,但是当我尝试从客户端(React Native / Expo应用程序)上载本地图像以与存储在此存储桶中的另一个图像进行比较时,出现了错误InvalidImageFormatException: Request has invalid image format
。
这张图片是jpeg 250像素的正方形,已作为有效的base64字符串发送(已测试了atob)。显然,它符合此处提出的条件: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;
};
服务器控制程序运行以下功能:
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);
在服务器上,我应该创建一个Buffer,而不是发送base64字符串:
const imageBuffer = Buffer.from(decodeURIComponent(SourceImage), 'base64');
因此,发送给AWS的正文应为:
const params = {
SourceImage: {
Bytes: imageBuffer,
}
TargetImage,
SimilarityThreshold: 50,
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句