-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·194 lines (160 loc) · 5.72 KB
/
server.js
File metadata and controls
executable file
·194 lines (160 loc) · 5.72 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
'use strict';
const express = require('express'); //express framework
const Clarifai = require('clarifai');
const WebCam = require('node-webcam');
const fs = require('fs');
const querystring = require('querystring');
const request = require('request');
const DatabaseManager = require('./database/database_manager');
let dbm = new DatabaseManager();
const bodyparser = require('body-parser');
const PORT = process.env.PORT || 3000;
const demographic = 'f783f0807c52474c8c6ad20c8cf45fc0';
const subscriptionKey = '4c824752ecbd402a9c24458473473dd1';
const app = express();
const cam = WebCam.create({
width: 1280,
height: 720,
quality: 100,
delay: 0,
saveShots: true,
output: "jpeg",
device: '2',
callbackReturn: "location",
verbose: false
});
const clarifai = new Clarifai.App({
apiKey: 'b53b6c1a796d4351ad943e5308c363f5'
});
app.use(express.static(__dirname + '/public')); //static server
app.get('/', (req, res) => {
res.sendfile('./sample.html');
});
app.get('/redeem', (req, res) => {
res.sendfile('./redeem.html');
});
app.use(bodyparser.urlencoded({extended: true}));
app.post('/redeem-discount', (request, response) => {
dbm.applyDiscount(request.body.discountCode, request.body.amount, [request.body.product]);
response.send('OK');
});
app.post('/startCollecting', (request, response) => {
collectCustomerDemographics().then(result => {
return dbm.recordCustomer(organize(result[0], result[1]), true);
}).then(discount => {
response.send(discount);
});
});
app.listen(PORT, err => {
if (err) console.log(err);
else console.log(`Server listening on port: ${PORT}`);
});
function collectCustomerDemographics() {
return new Promise((resolve, reject) => {
let results = [];
cam.capture("./images/image" + Date.now() + ".jpg", (err, data) => {
console.log(err);
analyzeWithAzure(data).then(result => {
results.push(result);
return analyzeWithClarifai(data);
}).then(result => {
results.push(result);
return resolve(results);
});
});
})
}
function analyzeWithClarifai(image) {
return new Promise((resolve, reject) => {
fs.readFile(image, (err, data) => {
let encodedImage = new Buffer(data, 'binary').toString('base64');
clarifai.models.predict(Clarifai.DEMOGRAPHICS_MODEL, {base64: encodedImage}).then(result => {
return resolve(result);
}).catch(console.log);
});
});
}
function analyzeWithAzure(image) {
return new Promise((resolve, reject) => {
fs.readFile(image, (err, data) => {
if (err) throw err;
let encodedImage = new Buffer(data, 'binary');
let options = {
url: 'https://westus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,gender,emotion',
method: 'POST',
processData: false,
headers: {
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': subscriptionKey
},
body: data
};
request(options, function (err, response) {
let obj = JSON.parse(response.body);
return resolve(obj[0]);
});
});
});
}
function organize(azobj, clarObj) {
//averaging age between Azure and Clarifai
var azAge = parseInt(azobj.faceAttributes.age);
var clarAge = clarObj.outputs[0].data.regions[0].data.face.age_appearance.concepts[0].name;
var age = parseInt((parseInt(azAge) + parseInt(clarAge)) / 2);
//getting gender from Azure
var gender = azobj.faceAttributes.gender;
//getting emotion from Azure
var anger = azobj.faceAttributes.emotion.anger;
var contempt = azobj.faceAttributes.emotion.contempt;
var disgust = azobj.faceAttributes.emotion.disgust;
var fear = azobj.faceAttributes.emotion.fear;
var happiness = azobj.faceAttributes.emotion.happiness;
var neutral = azobj.faceAttributes.emotion.neutral;
var sadness = azobj.faceAttributes.emotion.sadness;
var surprise = azobj.faceAttributes.emotion.surprise;
var emotion = 'undefined';
switch (Math.max(anger, contempt, disgust, fear, happiness, neutral, sadness, surprise)) {
case anger:
emotion = 'angry';
break;
case contempt:
emotion = 'contemptuous';
break;
case disgust:
emotion = 'disgusted';
break;
case fear:
emotion = 'fearful';
break;
case happiness:
emotion = 'happy';
break;
case neutral:
emotion = 'neutral';
break;
case sadness:
emotion = 'sad';
break;
case surprise:
emotion = 'surprised';
break;
default:
emotion = 'unknown';
}
//getting ethnicity from Clarifai
var ethnicity = 'undefined';
var confEthnicity = 0;
for (let i = 0; i < 7; i++) {
//finding Clarifai ethnicity with highest confidence.
if (clarObj.outputs[0].data.regions[0].data.face.multicultural_appearance.concepts[i].value > confEthnicity) {
confEthnicity = clarObj.outputs[0].data.regions[0].data.face.multicultural_appearance.concepts[i].value;
ethnicity = clarObj.outputs[0].data.regions[0].data.face.multicultural_appearance.concepts[i].name;
}
}
var results = {};
results['gender'] = gender;
results['age'] = age;
results['emotion'] = emotion;
results['ethnicity'] = ethnicity;
return results;
}