1

I try aws rekognition.I want to request and response through apigateway and lambda function.

Up: image down json of result which be trough rekognition.detectFaces.

detectFace method is bytes or S3Object as condition. So I choose bytes which encoded by Buffer.

lambda function code by node.js is under. Result is null(return(data) of rekognition.detectFaces)

It is sure of 'bytes' is.

What does I do now?

2021-01-10T01:23:56.087Z    ab357778-9513-4988-8616-4fda1250a14e    INFO    {  Attributes: [ 'DEFAULT' ],  Image: {    Bytes: <Buffer 2d 2d 2d 2d 2d 2d 57 65 62 4b 69 74 46 6f 72 6d 42 6f 75 6e 64 61 72 79 37 33 51 4b 39 44 51 55 6b 58 79 4c 58 42 59 47 0d 0a 43 6f 6e 74 65 6e 74 2d ... 1861180 more bytes>  }}

const aws = require('aws-sdk');
const rekognition = new aws.Rekognition();

exports.handler = async (event) => {
    //console.log(event.body);
  
    const buf = new Buffer.from(event.body,'base64'); 
    console.log(Buffer.byteLength(buf)); //under 5mbytes
    console.log(Buffer.isBuffer(buf)); //true
    
    //regix for cut 'Buffer<>' not working

    //const re = "/^<Buffer$/";
    //const reLast = "/>$/";
    //const encode1 = buf.toString().replace(re, "");
    //const encode = encode1.replace(reLast, "");
    //console.log(encode);
    //console.log(parseInt(encode,16));
    const params = {
    
        "Attributes": [ "DEFAULT" ],
        "Image": { 
            "Bytes": buf,
        }
    };
    console.log(params);
    rekognition.detectFaces(params, function(err, data) {
      console.log("Is it in this function?");
      if (err) console.log(err, err.stack); 
      else     console.log(data); 
               const faceData = data;
            
      const response = {
        statusCode: 200,
        body: JSON.stringify(faceData),
    };
    return response;
     }); 
};

1 Answers1

1

Here is a working example, Reading an Image and get a Buffer

var fs = require("fs");
const imageBytes1 = fs.readFileSync("/Users/path/to/image/my-image.jpeg");

Converting to buffer to Base64 and back to Buffer.

const base64Image = imageBytes1.toString("base64");
var imageBytes2 = Buffer.from(base64Image, "base64");

Passing either imageBytes1 or imageBytes2 to Bytes is resulting in correct response

const params = {
  Attributes: ["DEFAULT"],
  Image: {
  Bytes: imageBytes,
  },
};

to detectFaces

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

Response

{
 FaceDetails: [
   {
     BoundingBox: [Object],
     Landmarks: [Array],
     Pose: [Object],
     Quality: [Object],
     Confidence: 99.9991455078125
   }
 ]
}

Here is a Lambda example:

const aws = require("aws-sdk");
const rekognition = new aws.Rekognition();

exports.handler = async(event) => {
  var imageBytes = Buffer.from(event.body, "base64");
  const params = {
    Attributes: ["DEFAULT"],
    Image: {
      Bytes: imageBytes,
    },
  };
 const promise = new Promise(function(resolve, reject) {

 rekognition.detectFaces(params, function(err, data) {
  if (err) {
    console.log(err, err);
    reject(err);
  }
  else console.log(data);
  const response = {
    statusCode: 200,
    body: data,
  };
  resolve(response);
  })
 })
return promise;
};
Balu Vyamajala
  • 9,287
  • 1
  • 20
  • 42
  • Thank you. In this case, there is no image's file, lambda function get a encoded.tostring() from localhost form(action="myAwsApigateway") so that `event.body` is a strings(not binnary) like a this--LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5bm5BRkh5a1lUMmxoWWRWQg0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJpbWFnZSI7IGZpbGVuYW1lPSIyMDE5LTAxLTA4XzEwaDQwXzI0L...........I don't think fs module is working......Is anything for?? – ueyamamasashi Jan 10 '21 at 04:02
  • I just tried out with in Lambda console and edited the answer with it! working as expected. – Balu Vyamajala Jan 10 '21 at 05:01
  • Thank you.This is working.But next error coming `InvalidImageFormatException: Request has invalid image format`.As document I use png and jpeg,but same error coming..... I will ask next question on this site.Thank you, anyway!! – ueyamamasashi Jan 10 '21 at 07:03
  • Glad i could help. I converted my profile pic of this site into base64 and passed as lambda input, it detected the face. – Balu Vyamajala Jan 10 '21 at 07:08