我试图通过引用与Rekognition位于同一区域的同一S3存储桶“ refaces”中的两个文件来使用aws Rekognition API中的compareFaces()函数(我将S3存储桶设置为us-east-1,等等)。为了简单起见,我将存储桶设置为公开,并且我还使用了对Rekognition和S3具有完全权限的用户(对于这种情况,这不是必需的,只是为了澄清它):
aws-rekognition-config.js
const dotenv = require('dotenv');
dotenv.config();
const AWS = require('aws-sdk');
const rekognition = {
"accessKeyId": process.env.AMAZON_DEV_ACCESS_KEY_ID,
"secretAccessKey": process.env.AMAZON_DEV_SECRET_ACCESS_KEY,
"region": "us-east-1"
};
const Recognition = new AWS.Rekognition(rekognition);
module.exports = Recognition;
和index.js,在这里我做一个简单的测试来比较存储桶中的两个图像:
let express = require('express');
let router = express.Router();
var AWS = require('aws-sdk');
var rekognition = require('../config/aws-rekognition-config');
module.exports = () => {
router.get('/compare/:uid', async(req,res,next) => {
var params = {
SimilarityThreshold: 90,
SourceImage: {
S3Object: {
Bucket: "reconfaces",
Name: "1541079978865.jpg"
}
},
TargetImage: {
S3Object: {
Bucket: "reconfaces",
Name: "1541079982272.png" }
}
};
let faceMatches;
//tried promised version here as well but without any luck
rekognition.compareFaces(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
/*
try{
faceMatches = await rekognition.compareFaces(params);
let a = 1;
}catch(err){
console.log("Error comparing faces",err);
return;
} */
});
/* GET home page. */
router.use('/', (req, res) => {
//list all routes
res.send({"Default":'Backend-'});
});
return router;
}
您会看到文件存在于存储桶中,并且与rekognition配置中指定的文件位于同一区域:
并且我正在使用的用户凭据具有的权限超出了此任务所需的权限:
我还必须提到,我也使用npm包multer-s3通过api上传了文件:
var multer = require('multer');
var multerS3 = require('multer-s3');
var s3 = new aws.S3({
"accessKeyId": process.env.AMAZON_DEV_ACCESS_KEY_ID,
"secretAccessKey": process.env.AMAZON_DEV_SECRET_ACCESS_KEY,
"region": "us-east-1",
"s3BucketEndpoint": false,
"endpoint": "https://s3.amazonaws.com"
});
var upload = multer({
storage: multerS3({
s3,
bucket: 'reconfaces',
metadata: function (req, file, cb) {
cb(null, {fieldName: file.fieldname});
},
key: function (req, file, cb) {
cb(null, Date.now() + path.extname(file.originalname));
}
})
});
然后将其用作中间件:
router.post('/upload', upload.array('image',10), async(req,res,next)=>{
//upload picture to s3
console.log("Files uploaded successfully");
res.json({data:"UPLOAD_SUCCESS"});
});
我不知道multer-s3是否弄乱了元数据。但是我还尝试从浏览器的aws控制台上载这两个文件,我同时公开了这两个文件和存储桶,并且遇到了相同的错误,因此我怀疑这与multer-s3软件包有关。这些文件没有损坏或任何东西,因为我可以下载它们并查看它们而没有任何问题...
我也尝试使用cli并得到相同的错误:
aws rekognition compare-faces --source-image '{"S3Object":{"Bucket":"reconfaces","Name":"11112-face1.jpg"}}' --region us-east-1 --target-image '{"S3Object":{"Bucket":"reconfaces","Name":"11112-face2.jpg"}}'
该视频中的那个人也做不了我想要的事情:
https://www.youtube.com/watch?v=GtknPjdlOfg
这个人可以使用我拥有的相同特权
https://www.youtube.com/watch?v=FhFs0zwCvg4
如果我执行其他操作,它将起作用:
aws rekognition detect-labels --image '{"S3Object":{"Bucket":"reconfaces","Name":"11112-face1.jpg"}}'
它返回:
{
"Labels": [
{
"Name": "Human",
"Confidence": 99.17317962646484
},
{
"Name": "People",
"Confidence": 99.17317962646484
},
{
"Name": "Person",
"Confidence": 99.17317962646484
},
{
"Name": "Face",
"Confidence": 63.1695671081543
},
{
"Name": "Head",
"Confidence": 57.89347839355469
},
{
"Name": "Portrait",
"Confidence": 55.02056884765625
},
{
"Name": "Glasses",
"Confidence": 51.320011138916016
},
{
"Name": "Child",
"Confidence": 50.735557556152344
},
{
"Name": "Kid",
"Confidence": 50.735557556152344
}
],
"OrientationCorrection": "ROTATE_0"
}
所以它一定是带有“比较面”端点的东西。
可能是什么问题?我看到很多人在使用此特定的API时遇到问题,但是我在这里和github问题中发现的大多数答案都是关于两种资源都在不同地区运行的,这不是我的情况。
非常感谢您!
答案 0 :(得分:0)
这个问题真是愚蠢。在存储桶中,我有face1.jpg和face.png,并且都用
之类的jpg调用var params = {
SimilarityThreshold: 90,
SourceImage: {
S3Object: {
Bucket: "reconfaces",
Name: "face1.jpg"
}
},
TargetImage: {
S3Object: {
Bucket: "reconfaces",
Name: "face2.jpg" }
}
};
在使用face2.png修改了face2.jpg之后,我得到了正确的响应:
var params = {
SimilarityThreshold: 90,
SourceImage: {
S3Object: {
Bucket: "reconfaces",
Name: "face1.jpg"
}
},
TargetImage: {
S3Object: {
Bucket: "reconfaces",
Name: "face2.png" }
}
};
响应:
{
"SourceImageFace": {
"BoundingBox": {
"Width": 0.48317307233810425,
"Height": 0.6442307829856873,
"Left": 0.2584134638309479,
"Top": 0.18910256028175354
},
"Confidence": 99.9949722290039
},
"FaceMatches": [
{
"Similarity": 98,
"Face": {
"BoundingBox": {
"Width": 0.2640642821788788,
"Height": 0.2769230902194977,
"Left": 0.16237494349479675,
"Top": 0.29230770468711853
},
"Confidence": 99.84500122070312,
"Landmarks": [
{
"Type": "eyeLeft",
"X": 0.2420874983072281,
"Y": 0.3932344913482666
},
{
"Type": "eyeRight",
"X": 0.3406614661216736,
"Y": 0.3887109160423279
},
{
"Type": "nose",
"X": 0.3142981231212616,
"Y": 0.4448704421520233
},
{
"Type": "mouthLeft",
"X": 0.2604469358921051,
"Y": 0.5106690526008606
},
{
"Type": "mouthRight",
"X": 0.3448459208011627,
"Y": 0.5012921690940857
}
],
"Pose": {
"Roll": -3.831692695617676,
"Yaw": 14.12887954711914,
"Pitch": 3.8166630268096924
},
"Quality": {
"Brightness": 82.77690887451172,
"Sharpness": 89.91268920898438
}
}
}
],
"UnmatchedFaces": [],
"SourceImageOrientationCorrection": "ROTATE_0",
"TargetImageOrientationCorrection": "ROTATE_0"
}
最后,请检查文件名是否与您要定位的存储桶中的信息匹配。我想我得去睡觉xD