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;
};
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…