-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfaceBlur.js
More file actions
97 lines (82 loc) · 2.28 KB
/
Copy pathfaceBlur.js
File metadata and controls
97 lines (82 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const async = require('async'),
AWS = require('aws-sdk'),
gm = require('gm').subClass({ imageMagick: true });
const s3 = new AWS.S3();
const rekognition = new AWS.Rekognition();
const destDir = 'no-face';
exports.handler = (event, context, callback) => {
const srcBucket = event.Records[0].s3.bucket.name;
const srcKey = event.Records[0].s3.object.key;
async.waterfall([
function download(next) {
s3.getObject({
Bucket: srcBucket,
Key: srcKey
}, next);
},
function detectFaces(response, next) {
var params = {
Image: {
S3Object: {
Bucket: srcBucket,
Name: srcKey
}
},
Attributes: ['DEFAULT']
};
rekognition.detectFaces(params, (err, data) => {
if (err){
next(err);
} else {
next(null, response, data.FaceDetails);
}
});
},
function blur(response, faceDetails, next) {
let img = gm(response.Body);
img.size(function(err, value){
if (err) {
next(err);
} else {
faceDetails.forEach((faceDetail) => {
const box = faceDetail.BoundingBox,
width = box.Width * value.width,
height = box.Height * value.height,
left = box.Left * value.width,
top = box.Top * value.height;
img.region(width, height, left, top).blur(0, 50);
});
img.toBuffer(function(err, buffer) {
if(err) {
next(err);
} else {
next(null, response.ContentType, buffer);
}
});
}
});
},
function putObject(contentType, buffer, next) {
let dest = srcKey.split("/");
dest.shift();
dest.unshift(destDir);
let destKey = dest.join("/");
let obj = { Bucket : srcBucket, Key: destKey, Body : buffer, ContentType : contentType, ACL:'public-read' };
s3.putObject(obj, function(err, result) {
if (err) {
next(err);
} else {
next(null);
}
});
}
],
function (err) {
if (err) {
console.error(err);
callback(err);
} else {
callback(null, 'success');
}
});
};