-
Notifications
You must be signed in to change notification settings - Fork 0
/
faceswap.js
143 lines (120 loc) · 3.69 KB
/
faceswap.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict';
const aws = require('aws-sdk');
const fs = require('fs');
const gm = require('gm').subClass({
imageMagick: true
});
const rekognition = new aws.Rekognition();
const s3 = new aws.S3();
function getEmojiBasedOnSentiment(emotion) {
if (emotion) {
return 'emoji/' + emotion[0].Type.toLowerCase() + '.png';
} else {
return 'emoji/happy.png';
}
}
const detectFaces = function(bucket, filename) {
return new Promise((resolve, reject) => {
var params = {
Image: {
S3Object: {
Bucket: bucket,
Name: filename
}
},
Attributes: ['ALL']
};
rekognition.detectFaces(params, function(err, data) {
if (err) {
reject(err);
}
else {
resolve(data);
}
})
});
}
const saveFileToSystem = function(bucket, key, facedata) {
var file = fs.createWriteStream(process.env.TEMP_FOLDER + key);
return new Promise((resolve, reject) => {
var stream = s3.getObject({Bucket: bucket, Key: key})
.createReadStream()
.pipe(file);
stream.on('error', function(error){
reject(error);
});
stream.on('close', function(data){
resolve();
});
});
};
const analyseImage = function(key) {
return new Promise((resolve, reject) => {
var image = gm(process.env.TEMP_FOLDER + key);
image.size(function(err, size) {
if (err) {
reject(err);
}
else {
resolve({image: image, size: size});
}
});
});
}
const processFaces = function(key, imgdata, facedata) {
const image = imgdata.image;
const size = imgdata.size;
return new Promise((resolve, reject) => {
for (var i = 0; i < facedata.FaceDetails.length; i++) {
var box = facedata.FaceDetails[i].BoundingBox;
const left = parseInt(box.Left * size.width, 10);
const top = parseInt(box.Top * size.height, 10);
const width = parseInt(size.width * box.Width, 10);
const height = parseInt(size.height * box.Height, 10);
var dimensions = `${left}` + ',' + `${top}` + ' ' + `${width}` + ',' + `${height}`;
// var emoji = getEmojiBasedOnSentiment(facedata.FaceDetails[i].Emotions);
image.draw('image Over ' + dimensions + ' ' + 'emoji/happy.png');
}
resolve(image);
});
}
const saveNewImageToSystem = function(image, key) {
return new Promise((resolve, reject) => {
image.write(process.env.TEMP_FOLDER + process.env.OUTPUT_PREFIX + key, function (error){
if (error) {
reject(error);
} else {
resolve();
}
});
});
}
const uploadToBucket = function(key) {
var bodystream = fs.createReadStream(process.env.TEMP_FOLDER + process.env.OUTPUT_PREFIX + key);
return new Promise((resolve, reject) => {
s3.putObject({
Bucket: process.env.TRANSFORM_BUCKET,
Key: key,
Body: bodystream
}, function(error, data){
if (error){
reject(error);
} else {
resolve();
}
});
});
};
module.exports.execute = (event, context, callback) => {
const bucket = event.Records[0].s3.bucket.name;
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
var fd = null;
detectFaces(bucket, key)
.then((facedata) => {fd = facedata; return saveFileToSystem(bucket, key, facedata)})
.then(() => analyseImage(key))
.then((imagedata) => processFaces(key, imagedata, fd))
.then((image) => saveNewImageToSystem(image, key))
.then(() => uploadToBucket(key))
.then(() => callback(null, 'Success'))
.catch((err) => callback(err))
};