-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
51 lines (44 loc) · 1.8 KB
/
index.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
import express from 'express';
import sharp from 'sharp'
import fs from 'fs';
import https from 'https';
import tf from '@tensorflow/tfjs-node';
import nsfwjs from 'nsfwjs';
const app = express();
app.use(express.json());
(async () => {
const model = await nsfwjs.load(`https://nsfwjs.com/model/`, { size: 299 });;
app.get("/detector", async(req, res) => {
var url = req.query.url;
if (!url) return res.status(400).send("invalid URL.");
https.get(url, (response) => {
const chunks = [];
response.on('data', (chunk) => {
chunks.push(chunk);
});
response.on('end', () => {
const inputBuffer = Buffer.concat(chunks);
sharp(inputBuffer)
.png()
.toBuffer()
.then(async(outputBuffer) => {
const tocheck = await tf.node.decodeImage(outputBuffer, 3);
const predictions = await model.classify(tocheck);
tocheck.dispose();
const score = predictions.find(e => e.className === 'Porn').probability + predictions.find(e => e.className === 'Hentai').probability + predictions.find(e => e.className === 'Sexy').probability;
if (score >= 0.4) return res.status(401).send("nsfw content detected");
res.setHeader('Content-Type', 'image/png');
res.status(200).send(outputBuffer);
})
.catch((err) => {
console.error(err);
});
});
}).on('error', (err) => {
console.error(err);
});
})
})()
app.listen(3000, () => {
console.log("Server is ready.");
})