Created
May 14, 2020 14:35
-
-
Save damiancipolat/17d450b46f42ddbf3864f3ff159132f4 to your computer and use it in GitHub Desktop.
Aws rekognition lambda - read image from encoded image using base64
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk'); | |
const rekognition = new AWS.Rekognition(); | |
//Analyze image. | |
const analyzeImg = (image) => rekognition.detectFaces(image).promise(); | |
//Decode image. | |
const decodeImage = (image64)=>{ | |
return { | |
Image: { | |
Bytes: new Buffer(image64, 'base64') | |
}, | |
Attributes: ['ALL'] | |
}; | |
} | |
const validatRequest = (body)=>{ | |
const { | |
userId, | |
image | |
} = body; | |
if (!(userId&&image)) | |
throw {code:404,error:'Bad request'}; | |
return true; | |
} | |
exports.handler = async (event) => { | |
try { | |
const request = JSON.parse(event.body); | |
validatRequest(request); | |
const { | |
userId, | |
image | |
} = request; | |
const decodedImage = decodeImage(image); | |
const result = await analyzeImg(decodedImage); | |
console.info(result); | |
return { | |
statusCode:200, | |
body:JSON.stringify(result) | |
}; | |
} catch(err){ | |
console.error(err); | |
return { | |
statusCode:500, | |
error:JSON.stringify(err) | |
} | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment