forked from rachelnicole/whatcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
79 lines (74 loc) · 2.54 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
71
72
73
74
75
76
77
78
79
Array.prototype.pick = function() {
return this[Math.floor(Math.random()*this.length)];
};
var express = require('express');
var app = module.exports.app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var port = process.env.PORT || 3000;
var oxford = require('project-oxford');
var client = new oxford.Client(process.env.OXFORD);
var angerList = require('./anger.js');
var contemptList = require('./contempt.js');
var disgustList = require('./disgust.js');
var fearList = require('./fear.js');
var happinessList = require('./happiness.js');
var neutralList = require('./neutral.js');
var sadessList = require('./sadness.js');
var surpriseList = require('./surprise.js');
var selfieUrl;
server.listen(port);
app.use('/public', express.static('public'));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket) {
socket.on('selfiePath', function (selfiePath) {
selfieUrl = selfiePath;
console.log('face url: ' + selfiePath);
client.emotion.analyzeEmotion({
url: selfiePath,
}).then(function (response) {
if (response.length == 0) {
socket.emit('nohface');
return;
}
var emotionScores = response[0].scores;
var highScore = 0;
var highEmotion;
for (var i in emotionScores) {
if (emotionScores[i] > highScore) {
highScore = emotionScores[i];
highEmotion = i;
}
}
console.log(highScore);
console.log(highEmotion);
if (highEmotion == 'anger') {
socket.emit('catify', highEmotion, selfiePath, angerList.pick());
}
if (highEmotion == 'contempt') {
socket.emit('catify', highEmotion, selfiePath, contemptList.pick());
}
if (highEmotion == 'disgust') {
socket.emit('catify', highEmotion, selfiePath, disgustList.pick());
}
if (highEmotion == 'fear') {
socket.emit('catify', highEmotion, selfiePath, fearList.pick());
}
if (highEmotion == 'happiness') {
socket.emit('catify', highEmotion, selfiePath, happinessList.pick());
}
if (highEmotion == 'neutral') {
console.log('emitting for neutral');
socket.emit('catify', highEmotion, selfiePath, neutralList.pick());
}
if (highEmotion == 'sadness') {
socket.emit('catify', highEmotion, selfiePath, sadnessList.pick());
}
if (highEmotion == 'surprise') {
socket.emit('catify', highEmotion, selfiePath, surpriseList.pick());
}
});
});
});