Juan L
Juan L

Reputation: 1731

AWS compareFaces() Rekognition Javascript SDK Error: Unable to get object metadata from S3. Check object key, region and/or access permissions

I'm trying to use compareFaces() function from aws Rekognition API by referencing two files in the same S3 bucket "reconfaces" that is in the same Region as Rekognition(I set the S3 bucket to us-east-1, and so Rekognition). I set the bucket to public for simplicity and I'm also using a user that has Full Permisions over Rekognition and S3(which wasn't necessary for this case but just to clarify it):

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;

and the index.js where I do a simple test to compare the two images in my bucket:

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;
}

As you can see the files exist in the bucket and it's on the same region as the one specified in the rekognition config: enter image description here

And the user credentials I'm using has more permissions than it needs to for this task:

enter image description here

I also have to mention that I uploaded the files via api as well using the npm package multer-s3:

    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));
        }
      })
    });

and then it's applied as a middleware:

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"});
});

I don't know if maybe the metadata is messed up by multer-s3. But I also tried to upload both of the files from the aws console in the browser, I made both files and the bucket public and I get the same error, so I doubt it has to do with multer-s3 package. The files are not corrupted or anything since I can download them and view them without any problem...

I also tried using the cli and I get the same error:

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"}}'

The guy in this video couldn't do the same as I wanted either:

https://www.youtube.com/watch?v=GtknPjdlOfg

and this guy could using the same privileges I have

https://www.youtube.com/watch?v=FhFs0zwCvg4

If I throw this other operation it works:

aws rekognition detect-labels --image '{"S3Object":{"Bucket":"reconfaces","Name":"11112-face1.jpg"}}' 

it returns:

{
    "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"
}

so it must be something with the compare-faces endpoint.

What could be the problem?. I saw a lot of people having problems with this particular API, but most of the answers that I found here and in github issues were about both resources being operating in different regions, which is not my case.

Thank you very much!

Upvotes: 2

Views: 1664

Answers (1)

Juan L
Juan L

Reputation: 1731

The problem was really silly. In the bucket I had face1.jpg and a face.png and I was calling both with jpg like

var params = {
  SimilarityThreshold: 90, 
  SourceImage: {
  S3Object: {
    Bucket: "reconfaces", 
    Name: "face1.jpg"
  }
  }, 
  TargetImage: {
  S3Object: {
    Bucket: "reconfaces", 
    Name: "face2.jpg"   }
  }
};

after correcting the face2.jpg with face2.png I got the proper response:

  var params = {
      SimilarityThreshold: 90, 
      SourceImage: {
      S3Object: {
        Bucket: "reconfaces", 
        Name: "face1.jpg"
      }
      }, 
      TargetImage: {
      S3Object: {
        Bucket: "reconfaces", 
        Name: "face2.png"   }
      }
    };

Response:

{
    "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"
}

So to wrap up, CHECK THAT THE NAMES OF THE FILES MATCHES THE ONES IN THE BUCKET YOU ARE TARGETING. I guess I have to get some sleep xD

Upvotes: 2

Related Questions