Skip to content

Instantly share code, notes, and snippets.

@andreigec
Created September 11, 2019 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreigec/bf8ddb01d74f7e63566a33c5c1edbea2 to your computer and use it in GitHub Desktop.
Save andreigec/bf8ddb01d74f7e63566a33c5c1edbea2 to your computer and use it in GitHub Desktop.
const AWS = require('aws-sdk');
function getObject({ Bucket, Key }) {
return new Promise((res, rej) => {
const s3 = new AWS.S3();
const params = {
Bucket,
Key,
};
s3.getObject(params, (err, data) => {
if (err) {
rej(err);
} else {
// @ts-ignore
const ret = data.Body;
res(ret);
}
});
});
}
function detectFace({ Bytes }) {
return new Promise((res, rej) => {
const rekognition = new AWS.Rekognition();
const params = {
Image: {
/* required */
Bytes,
},
Attributes: ['ALL', 'DEFAULT'],
};
rekognition.detectFaces(params, (err, data) => {
if (err) {
rej(err);
} else {
res(data);
}
});
});
}
function detectText({ Bytes }) {
return new Promise((res, rej) => {
const rekognition = new AWS.Rekognition();
const params = {
Image: {
/* required */
Bytes,
},
};
rekognition.detectText(params, (err, data) => {
if (err) {
rej(err);
} else {
res(data);
}
});
});
}
exports.handler = async (a, b, c) => {
const Bucket = a.Records[0].s3.bucket.name;
const Key = a.Records[0].s3.object.key;
const Bytes = await getObject({ Bucket, Key });
console.log('got data, l=', Bytes.length);
const ret = await detectText({ Bytes });
const lines = ret.TextDetections.filter(r => r.Type === 'LINE').map(
r2 => r2.DetectedText,
);
console.log('ret=', JSON.stringify(lines, null, 2));
const response = {
statusCode: 200,
body: JSON.stringify(ret, null, 2),
};
return response;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment