-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
431 lines (394 loc) · 15.3 KB
/
main.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
const express = require("express");
const multer = require("multer");
const path = require("path");
const app = express();
const port = 3000;
////////// AI /////////
const fs = require('fs');
const log = require('@vladmandic/pilogger');
const tf = require('@tensorflow/tfjs-node');
const {
Canvas,
loadImage
} = require('canvas'); // eslint-disable-line node/no-unpublished-require
const options = { // options
debug: true,
modelPath: 'file://model/model.json',
minScore: 0.30,
maxResults: 50,
iouThreshold: 0.5,
outputNodes: ['output1', 'output2', 'output3'],
blurNude: true,
blurRadius: 25,
};
const labels = [ // class labels
'exposed anus',
'exposed armpits',
'belly',
'exposed belly',
'buttocks',
'exposed buttocks',
'female face',
'male face',
'feet',
'exposed feet',
'breast',
'exposed breast',
'vagina',
'exposed vagina',
'male breast',
'exposed male breast',
];
const composite = { // composite definitions of what is a person, sexy, nude
person: [6, 7],
sexy: [1, 2, 3, 4, 8, 9, 10, 15],
nude: [0, 5, 11, 12, 13],
};
const models = []; // holds instance of graph model
// draw rect with rounded corners
function rect({
canvas,
x = 0,
y = 0,
width = 0,
height = 0,
radius = 8,
lineWidth = 2,
color = 'white',
title = '',
font = '16px "Segoe UI"'
}) {
if (!canvas) return;
const ctx = canvas.getContext('2d');
if (!ctx) return;
ctx.lineWidth = lineWidth;
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
ctx.strokeStyle = color;
ctx.stroke();
ctx.lineWidth = 2;
ctx.fillStyle = color;
ctx.font = font;
ctx.fillText(title, x + 4, y - 4);
}
// blur par of canvas by redrawing it with smaller resulution
function blur({
canvas,
left = 0,
top = 0,
width = 0,
height = 0
}) {
if (!canvas) return;
const blurCanvas = new Canvas(width / options.blurRadius, height / options.blurRadius);
const blurCtx = blurCanvas.getContext('2d');
if (!blurCtx) return;
blurCtx.imageSmoothingEnabled = true;
blurCtx.drawImage(canvas, left, top, width, height, 0, 0, width / options.blurRadius, height / options.blurRadius);
const canvasCtx = canvas.getContext('2d');
canvasCtx.drawImage(blurCanvas, left, top, width, height);
}
// read image file and prepare tensor for further processing
function getTensorFromImage(imageFile) {
if (!fs.existsSync(imageFile)) {
log.error('Not found:', imageFile);
return null;
}
const data = fs.readFileSync(imageFile);
const bufferT = tf.node.decodeImage(data);
const expandedT = tf.expandDims(bufferT, 0);
const imageT = tf.cast(expandedT, 'float32');
imageT['file'] = imageFile;
tf.dispose([expandedT, bufferT]);
if (options.debug) log.info('loaded image:', imageT['file'], 'width:', imageT.shape[2], 'height:', imageT.shape[1]);
return imageT;
}
// create output jpeg after processing
async function saveProcessedImage_blur_only(inImage, outImage, data) {
if (!data) return false;
return new Promise(async (resolve) => { // eslint-disable-line no-async-promise-executor
const original = await loadImage(inImage); // load original image
const c = new Canvas(original.width, original.height); // create canvas
const ctx = c.getContext('2d');
ctx.drawImage(original, 0, 0, c.width, c.height); // draw original onto output canvas
for (const obj of data.parts) { // draw all detected objects
if (composite.nude.includes(obj.id) && options.blurNude) {
blur({
canvas: c,
left: obj.box[0],
top: obj.box[1],
width: obj.box[2],
height: obj.box[3],
});
}
}
const out = fs.createWriteStream(outImage); // write canvas to jpeg
out.on('finish', () => {
if (options.debug) log.state('created output image:', outImage);
resolve(true);
});
out.on('error', (err) => {
log.error('error creating image:', outImage, err);
resolve(true);
});
const stream = c.createJPEGStream({
quality: 0.6,
progressive: true,
chromaSubsampling: true
});
stream.pipe(out);
});
}
// create output jpeg after processing
async function saveProcessedImage_NO_BLUR(inImage, outImage, data) {
if (!data) return false;
return new Promise(async (resolve) => { // eslint-disable-line no-async-promise-executor
const original = await loadImage(inImage); // load original image
const c = new Canvas(original.width, original.height); // create canvas
const ctx = c.getContext('2d');
ctx.drawImage(original, 0, 0, c.width, c.height); // draw original onto output canvas
for (const obj of data.parts) { // draw all detected objects
rect({
canvas: c,
x: obj.box[0],
y: obj.box[1],
width: obj.box[2],
height: obj.box[3],
title: `${Math.round(100 * obj.score)}% ${obj.class}`,
});
}
const out = fs.createWriteStream(outImage); // write canvas to jpeg
out.on('finish', () => {
if (options.debug) log.state('created output image:', outImage);
resolve(true);
});
out.on('error', (err) => {
log.error('error creating image:', outImage, err);
resolve(true);
});
const stream = c.createJPEGStream({
quality: 0.6,
progressive: true,
chromaSubsampling: true
});
stream.pipe(out);
});
}
// parse prediction data
async function processPrediction(boxesTensor, scoresTensor, classesTensor, inputTensor) {
const boxes = await boxesTensor.array();
const scores = await scoresTensor.data();
const classes = await classesTensor.data();
const nmsT = await tf.image.nonMaxSuppressionAsync(boxes[0], scores, options.maxResults, options.iouThreshold, options.minScore); // sort & filter results
const nms = await nmsT.data();
tf.dispose(nmsT);
const parts = [];
for (const i in nms) { // create body parts object
const id = parseInt(i);
parts.push({
score: scores[i],
id: classes[id],
class: labels[classes[id]], // lookup classes
box: [ // convert box from x0,y0,x1,y1 to x,y,width,heigh
Math.trunc(boxes[0][id][0]),
Math.trunc(boxes[0][id][1]),
Math.trunc((boxes[0][id][3] - boxes[0][id][1])),
Math.trunc((boxes[0][id][2] - boxes[0][id][0])),
],
});
}
const result = {
input: {
file: inputTensor.file,
width: inputTensor.shape[2],
height: inputTensor.shape[1]
},
person: parts.filter((a) => composite.person.includes(a.id)).length > 0,
sexy: parts.filter((a) => composite.sexy.includes(a.id)).length > 0,
nude: parts.filter((a) => composite.nude.includes(a.id)).length > 0,
parts,
};
if (options.debug) log.data('result:', result);
return result;
}
// Made alone
async function detect(input){
const t = {};
if (!models[options.modelPath]) { // load model if not already loaded
try {
models[options.modelPath] = await tf.loadGraphModel(options.modelPath);
models[options.modelPath].path = options.modelPath;
if (options.debug) log.state('loaded graph model:', options.modelPath);
} catch (err) {
log.error('error loading graph model:', options.modelPath, err.message, err);
return null;
}
}
t.input = getTensorFromImage(input); // get tensor from image
[t.boxes, t.scores, t.classes] = await models[options.modelPath].executeAsync(t.input, options.outputNodes); // run prediction
const res = await processPrediction(t.boxes, t.scores, t.classes, t.input); // parse outputs
Object.keys(t).forEach((tensor) => tf.dispose(t[tensor])); // free up memory
return res;
}
async function cadre(input){
let split = input.split(".");
split[split.length-2] = split[split.length-2] + "_processed";
let output = split.join(".");
const t = {};
if (!models[options.modelPath]) { // load model if not already loaded
try {
models[options.modelPath] = await tf.loadGraphModel(options.modelPath);
models[options.modelPath].path = options.modelPath;
if (options.debug) log.state('loaded graph model:', options.modelPath);
} catch (err) {
log.error('error loading graph model:', options.modelPath, err.message, err);
return null;
}
}
t.input = getTensorFromImage(input); // get tensor from image
[t.boxes, t.scores, t.classes] = await models[options.modelPath].executeAsync(t.input, options.outputNodes); // run prediction
const t0 = process.hrtime.bigint();
const res = await processPrediction(t.boxes, t.scores, t.classes, t.input); // parse outputs
const t1 = process.hrtime.bigint();
Object.keys(t).forEach((tensor) => tf.dispose(t[tensor])); // free up memory
await saveProcessedImage_NO_BLUR(input, output, res); // save processed image and return result
log.state(`done: model:${options.modelPath} time:${(t1 - t0) / 1000n / 1000n} input:${input} output:${output} objects:`, res.parts?.length);
return output;
}
async function bluring(input){
let split = input.split(".");
split[split.length-2] = split[split.length-2] + "_processed";
let output = split.join(".");
console.log(output);
const t = {};
if (!models[options.modelPath]) { // load model if not already loaded
try {
models[options.modelPath] = await tf.loadGraphModel(options.modelPath);
models[options.modelPath].path = options.modelPath;
if (options.debug) log.state('loaded graph model:', options.modelPath);
} catch (err) {
log.error('error loading graph model:', options.modelPath, err.message, err);
return null;
}
}
t.input = getTensorFromImage(input); // get tensor from image
[t.boxes, t.scores, t.classes] = await models[options.modelPath].executeAsync(t.input, options.outputNodes); // run prediction
const t0 = process.hrtime.bigint();
const res = await processPrediction(t.boxes, t.scores, t.classes, t.input); // parse outputs
const t1 = process.hrtime.bigint();
Object.keys(t).forEach((tensor) => tf.dispose(t[tensor])); // free up memory
await saveProcessedImage_blur_only(input, output, res); // save processed image and return result
log.state(`done: model:${options.modelPath} time:${(t1 - t0) / 1000n / 1000n} input:${input} output:${output} objects:`, res.parts?.length);
return output;
}
async function ban(input){
const t = {};
if (!models[options.modelPath]) { // load model if not already loaded
try {
models[options.modelPath] = await tf.loadGraphModel(options.modelPath);
models[options.modelPath].path = options.modelPath;
if (options.debug) log.state('loaded graph model:', options.modelPath);
} catch (err) {
log.error('error loading graph model:', options.modelPath, err.message, err);
return null;
}
}
t.input = getTensorFromImage(input); // get tensor from image
[t.boxes, t.scores, t.classes] = await models[options.modelPath].executeAsync(t.input, options.outputNodes); // run prediction
const res = await processPrediction(t.boxes, t.scores, t.classes, t.input); // parse outputs
Object.keys(t).forEach((tensor) => tf.dispose(t[tensor])); // free up memory
if(res.nude){
return './public/banned.jpg';
}
return input;
}
///////// Utils /////////
function generateRandomString(length) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}
///////// WEB SERVER /////////
// define the storage location for the uploaded files
const uploadDir = path.join(__dirname, 'uploads');
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, uploadDir);
},
filename: (req, file, cb) => {
cb(null, generateRandomString(20) + (file.originalname.split('.').pop() === 'jpg' ? '.jpg' : '.png'));
},
});
const upload = multer({
storage
});
// route
app.post('/api/*', upload.single('file'), async (req, res) => {
let filename = ""
try {
filename = req.file.filename;
} catch (error) {
res.status(400).send('No file uploaded, header must be "file"');
return;
}
let action = req.originalUrl.split('/')[2];
await tf.enableProdMode();
await tf.ready();
switch (action) {
case 'detect':
// Only respond with the JSON analysis content
let result = await detect('./uploads/' + filename);
res.set('Content-Type', 'application/json');
res.send(JSON.stringify(result));
break;
case 'cadre':
// Respond with the image with the cadre but no blur
let filedir = await cadre('./uploads/' + filename);
res.set('Content-Type', 'image/'+(filename.split('.').pop() === 'jpg' ? 'jpeg' : 'png'));
await res.sendFile(path.join(__dirname, filedir));
break;
case 'blur':
// Respond with the image with the blur
let filedir2 = await bluring('./uploads/' + filename);
res.set('Content-Type', 'image/'+(filename.split('.').pop() === 'jpg' ? 'jpeg' : 'png'));
await res.sendFile(path.join(__dirname, filedir2));
break;
case 'ban':
// Respond with the image with the blur
let filedir3 = await ban('./uploads/' + filename);
res.set('Content-Type', 'image/'+(filedir3.split('.').pop() === 'jpg' ? 'jpeg' : 'png'));
await res.sendFile(path.join(__dirname, filedir3));
break;
default:
res.status(400).send('No action provided in the header');
}
console.log(`File ${filename} traited`);
setTimeout(() => {
// delete the file after it has been processed
try {
fs.unlinkSync('./uploads/' + filename);
fs.unlinkSync('./uploads/' + filename.split('.')[0] + '_processed.' + filename.split('.')[1])
} catch (error) {
}
}, 10000);
});
app.use('/uploads', express.static(uploadDir));
// Listen on port 3000
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});