This repository has been archived by the owner on Sep 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext_action.js
166 lines (159 loc) · 4.57 KB
/
next_action.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';
const AWS = require('aws-sdk');
const axios = require('axios');
require('dotenv').config();
const { bucket } = require('./config')
// const getPerson = (personId) => {
// let s3 = new AWS.S3();
// return new Promise((resolve, reject) => {
// s3.getObject({
// Bucket: 'affective-computing',
// Key: `emotions/${personId}.json`
// }, (error, response) => {
// if (error) {
// reject(error);
// } else {
// var person = JSON.parse(response.Body.toString());
// resolve(person);
// }
// });
// });
// };
const calculateHappiness = (faceDetails) => {
var happiness = 0.5 * 100;
if (faceDetails.Smile.Value) {
happiness += 0.3 * faceDetails.Smile.Confidence;
}
faceDetails.Emotions.forEach(emotion => {
switch (emotion.Type) {
case 'HAPPY':
happiness += 0.5 * emotion.Confidence;
break;
case 'CALM':
case 'SURPRISED':
happiness += 0.1 * emotion.Confidence;
break;
case 'ANGRY':
case 'DISGUSTED':
happiness -= 0.8 * emotion.Confidence;
break;
case 'CONFUSED':
case 'SAD':
happiness -= 0.5 * emotion.Confidence;
break;
default:
break;
}
});
return Math.min(Math.max(happiness / 100, 0), 1);
};
const getPerson = (personId) => {
return new Promise((resolve, reject) => {
var rekognition = new AWS.Rekognition();
var params = {
Image: {
S3Object: {
Bucket: bucket,
Name: 'photos/' + personId + '.png'
}
},
Attributes: [ 'ALL' ]
}
return rekognition.detectFaces(params, (error, response) => {
if (error) {
return reject(error);
}
console.log('Rekognition', JSON.stringify(response));
var faceDetails = response.FaceDetails[0];
if (!faceDetails) {
return resolve({
age: 40,
female: 0,
has_sunglasses: 0,
happiness: 0.5
});
}
resolve({
age: Math.round(faceDetails.AgeRange.Low + 0.5 * (faceDetails.AgeRange.High - faceDetails.AgeRange.Low)),
female: (faceDetails.Gender.Value == 'female') ? 1 : 0,
has_sunglasses: (faceDetails.Sunglasses) ? 0 : 1,
happiness: calculateHappiness(faceDetails)
});
});
});
};
const getAction = (env) => {
return axios.request({
method: 'get',
url: `${process.env.AGENT_URL}/next_action`,
params: env
}).then(response => response.data);
};
const carAction = (action) => {
axios.request({
method: 'get',
url: `https://bmw-api.hackathons.de/vehicles/${process.env.VEHICLE_ID}/services/${action}/`,
headers: {
'x-api-key': process.env.CAR_API_KEY
}
}).then(_ => console.log('CarAction', action)).catch(console.error)
}
module.exports.handle = (event, context, callback) => {
if (!event.queryStringParameters || !event.queryStringParameters.music) {
return callback(null, {
statusCode: 400,
headers: { 'Access-Control-Allow-Origin': '*' },
body: "Param music missing"
})
}
if (!event.queryStringParameters.route) {
return callback(null, {
statusCode: 400,
headers: { 'Access-Control-Allow-Origin': '*' },
body: "Param route missing"
})
}
if (!event.queryStringParameters.step) {
return callback(null, {
statusCode: 400,
headers: { 'Access-Control-Allow-Origin': '*' },
body: "Param step missing"
})
}
if (!event.queryStringParameters.personId) {
return callback(null, {
statusCode: 400,
headers: { 'Access-Control-Allow-Origin': '*' },
body: "Param personId missing"
})
}
console.log('PersonID', event.queryStringParameters.personId);
return getPerson(event.queryStringParameters.personId).then(person => {
var env = {
music: parseInt(event.queryStringParameters.music),
route: parseInt(event.queryStringParameters.route),
step: parseInt(event.queryStringParameters.step)
};
Object.assign(env, person);
console.log('Env', env);
return getAction(env);
}).then(response => {
console.log('Response', response);
if (response.action == 9) {
carAction('horn_blow');
carAction('door_unlock');
}
callback(null, {
statusCode: 200,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify(response)
});
}).catch(error => {
console.error(error);
callback(null, {
statusCode: 500,
headers: { 'Access-Control-Allow-Origin': '*' },
body: error.toString() // TODO: Do not tell error message in real world scenario
});
});
};