-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
65 lines (59 loc) · 1.58 KB
/
app.js
File metadata and controls
65 lines (59 loc) · 1.58 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
require("dotenv").config();
const fs = require("fs");
const express = require("express");
const app = express();
const vision = require("@google-cloud/vision");
const multer = require("multer");
const upload = multer({ dest: ".cache/" });
const port = process.env.PORT || 3000;
function convertToInt(likelihood) {
switch (likelihood) {
case "VERY_UNLIKELY":
return 0;
case "UNLIKELY":
return 1;
case "LIKELY":
return 2;
case "VERY_LIKELY":
return 3;
default:
return 0;
}
}
async function getEmotion(imageUrl) {
const client = new vision.ImageAnnotatorClient();
const [result] = await client.faceDetection(imageUrl);
const faces = result.faceAnnotations;
return faces;
}
app.post("/getMood", upload.single("image"), (req, res) => {
getEmotion(req.file.path).then((faces) => {
const face = faces[0];
const likelihood = {
joy: convertToInt(face.joyLikelihood),
angry: convertToInt(face.angerLikelihood),
sad: convertToInt(face.sorrowLikelihood),
surprised: convertToInt(face.surpriseLikelihood),
};
const highestProbability = Math.max(
likelihood.joy,
likelihood.angry,
likelihood.sad,
likelihood.surprised
);
const mostLikely = (function () {
for (let [key, value] of Object.entries(likelihood)) {
if (value === highestProbability) return key;
}
})();
const data = {
likelihood,
mostLikely,
};
fs.unlinkSync(req.file.path);
res.json(data);
});
});
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});