-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathapp.js
154 lines (123 loc) · 4.22 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
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
const express = require('express');
const tf = require('@tensorflow/tfjs-node');
const helmet = require('helmet');
const compression = require('compression');
const formidable = require('formidable');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const fs = require('fs');
const app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(compression());
app.use(helmet({hsts: false}));
// app.get('/', function(req, res, next) {
// res.render('index', { title: 'Express' });
// });
let mirNetModel;
let modelInfo;
const imageSize = 512;
async function loadModel() {
try {
// Warm up the model
if (!mirNetModel) {
modelInfo = await tf.node.getMetaGraphsFromSavedModel('./model');
console.log(await modelInfo);
mirNetModel = await tf.node.loadSavedModel(
'./model'
);
return await mirNetModel;
}
} catch (error) {
console.log(error);
}
};
const predict = async (imgPath, responseImagePath) => {
try {
mirNetModel = await loadModel();
console.log(mirNetModel);
console.log("Inside predict");
let image = fs.readFileSync(imgPath);
// image = new Uint8Array(image);
// Decode the image into a tensor.
let imageTensor = await tf.node.decodePng(image, 3);
imageTensor = tf.image.resizeBilinear(imageTensor, size = [imageSize, imageSize]);
imageTensor = tf.cast(imageTensor, "float32");
imageTensor = tf.div(imageTensor, tf.scalar(255.0));
let input = imageTensor.expandDims(0);
// Feed the image tensor into the model for inference.
const startTime = tf.util.now();
let outputTensor = mirNetModel.predict(input);
const endTime = tf.util.now();
console.log(endTime - startTime);
console.log("After Predict");
outputTensor = tf.reshape(outputTensor, [512, 512, 3]);
// outputTensor = outputTensor.squeeze();
// outputTensor = new Uint8Array(outputTensor);
// let factor = tf.onesLike(outputTensor);
// factor = tf.mul(factor, tf.min(outputTensor));
// outputTensor = tf.add(outputTensor, factor);
// const mulFactor = tf.max(outputTensor) / 255.0;
// outputTensor = tf.mul(outputTensor, mulFactor);
// outputTensor = tf.mul(outputTensor, factor);
outputTensor = tf.mul(outputTensor, tf.scalar(255.0));
outputTensor = tf.clipByValue(outputTensor, 0, 255);
outputTensor = await tf.node.encodePng(outputTensor);
fs.writeFileSync(responseImagePath, outputTensor);
return true;
} catch (error) {
console.log(error);
return false;
}
};
const formOptions = {
// uploadDir: path.join(__dirname, "uploads"),
encoding: 'utf-8',
keepExtensions: true,
maxFileSize: 5 * 1024 * 1024,
multiples: false,
};
// const form = new formidable.IncomingForm(formOptions);
app.post('/submit', (req, res) => {
let form = new formidable.IncomingForm(formOptions);
form.parse(req, async (err, fields, files) => {
if (err) {
res.send("Incorrect File Format");
console.log('\n' + err + '\n');
} else {
console.log("sending File: " + files.image.name);
// res.sendFile(files.image.path);
try {
let toSend = await predict(files.image.path, path.join(__dirname, "public", "responseImages") + files.image.name);
if (toSend === true) {
res.sendFile(path.join(__dirname, "public", "responseImages") + files.image.name);
} else {
res.status(501).send(toSend);
}
} catch (err) {
res.status(500).send(err);
}
}
});
const allowedFiles = ["image/jpg", "image/jpeg", "image/png"];
try {
form.on('fileBegin', function(name, file) {
if (!allowedFiles.includes(file.type)) {
// throw new Error("Incorrect File Type");
form._error(new Error("Incorrect File Type"));
return new Error("Incorrect File Type");
} else {
file.path = path.join(__dirname, "uploads") + "/" + Date.now() + "-" + file.name;
}
});
} catch (err) {
form._error(err);
console.log(err);
res.send("Incorrect File Type");
}
});
module.exports = app;