This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
71 lines (66 loc) · 2.01 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const tf = require('@tensorflow/tfjs-node');
const nsfw = require('nsfwjs');
const express = require('express');
const app = express();
// const fs = require("fs");
const port = 3000;
const nsfwLoad = nsfw.load();
app.post('/nsfw', (req, res) => {
try {
let data = [];
req.on("data", (chunk) => {
try {
data.push(chunk);
} catch (e) {
res.end(JSON.stringify({
'code': 500,
'msg': e.message,
'data': ''
}));
};
});
req.on("end", () => {
try {
const buffer = Buffer.concat(data);
// 保存图片
// fs.writeFile("./image.jpg", buffer, (err) => {
// if (!err) {
// res.end("ok");
// };
// });
const image = tf.node.decodeImage(buffer);
nsfwLoad.then((model) => {
model.classify(image).then((predictions) => {
let data = {};
for (i of predictions) {
data[i['className']] = Math.trunc(i['probability'] * 10000) / 100;
};
res.end(JSON.stringify({
'code': 200,
'msg': '',
'data': data
}));
});
});
} catch (e) {
res.end(JSON.stringify({
'code': 500,
'msg': e.message,
'data': ''
}));
};
});
} catch (e) {
res.end(JSON.stringify({
'code': 500,
'msg': e.message,
'data': ''
}));
};
});
app.use((req, res) => {
res.end('https://github.com/qiaoshouzi/nsfwjs-api');
});
app.listen(port, () => {
console.log("server run at ", port);
});