-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
70 lines (59 loc) · 1.68 KB
/
app.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
const AWS = require('aws-sdk');
const dotenv = require('dotenv').config();
const fs = require('fs');
//Creación y configuración de instancia de AWS Rekognition
const rekognition = new AWS.Rekognition({
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: process.env.AWS_REGION,
apiVersion: '2016-06-27'
});
//Lee la imagen, la convierte a Buffer y prepara el objeto
const file_name = 'imagen1.jpg';
const bitmap = fs.readFileSync(`./input/${file_name}`);
let image = {
Image: {
Bytes: new Buffer(bitmap)
},
Attributes: ['ALL']
}
//Llama al método detectFaces
rekognition.detectFaces(image, (error, data) => {
if (error) {
throw new Error(error);
}
data.FaceDetails.forEach((response) => {
let maxConfidence = response.Emotions.reduce((a, b) => b.Confidence > b.Confidence ? b : a);
let emotionName = '';
switch (maxConfidence.Type) {
case 'HAPPY':
emotionName = 'ALEGRÍA';
break;
case 'SAD':
emotionName = 'PENA';
break;
case 'ANGRY':
emotionName = 'ENOJO';
break;
case 'CONFUSED':
emotionName = 'CONFUSIÓN';
break;
case 'DISGUSTED':
emotionName = 'DISGUSTO';
break;
case 'SURPRISED':
emotionName = 'SORPRESA';
break;
case 'CALM':
emotionName = 'CALMA';
break;
case 'UNKNOWN':
emotionName = 'DESCONOCIDA';
break;
default:
emotionName = 'DESCONOCIDA';
break;
}
console.log(`La emoción predominante en la imágen es ${emotionName} con un grado de seguridad de ${maxConfidence.Confidence}`);
});
});