-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
51 lines (39 loc) · 1.15 KB
/
server.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
const express = require("express");
const app = express();
const multer = require('multer');
const fs = require("fs");
const { loadModel, predict } = require("./src/ml-process");
(async () => {
const model = await loadModel();
// Middleware for json response
app.use(express.json())
// Middleware for root
app.get("/", (req,res) => {
res.json({result: "Server is running and connected"})
});
//Middleware for upload image using memorystorage (equal to we don't want to store anywhere)
const storage = multer.memoryStorage();
const imageUpload = multer({ storage: storage });
// Middleware for prediction ML
app.post("/predicts", imageUpload.single("image"), (req, res) => {
const imageBuffer = req.file.buffer;
(async () => {
//Get Prediction
const prediction = await predict(model, imageBuffer);
const [paper, rock, scissors] = prediction;
if (paper) {
res.json({result: "Paper"});
}
if (rock) {
res.json({result: "Rock"})
}
if (scissors) {
res.json({result: "Scissors"})
}
})();
});
const port = 3000;
const server = app.listen(port, () => {
console.log(`server is running on ${port} port`)
});
})();