Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

node.js - aws Rekognition.detectFaces request bytes 'encoded base64' not working

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;
     }); 
};
question from:https://stackoverflow.com/questions/65649673/aws-rekognition-detectfaces-request-bytes-encoded-base64-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...