-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSFW.js
60 lines (53 loc) · 1.69 KB
/
SFW.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
const axios = require('axios');
const tf = require('@tensorflow/tfjs-node')
tf.enableProdMode()
const nsfw = require('nsfwjs')
const sharp = require('sharp');
const decode = require('image-decode')
let model;
(async () =>
model = await nsfw.load('file://./model/',{type:'graph'}))()
async function convert(image) {
const {width, height,data} = decode(image)
const numChannels = 3;
const numPixels = width*height;
const values = new Int32Array(numPixels * numChannels);
for (let i = 0; i < numPixels; i++){
for (let c = 0; c < numChannels; ++c){
values[i * numChannels + c] = data[i * 4 + c];
}
}
return tf.tensor3d(values, [height, width, numChannels], "int32");
}
async function check(image_url) {
const pic= await axios.get(image_url,{responseType: 'arraybuffer' })
const image = await convert(pic.data);
const predictions = await model.classify(image);
image.dispose()
// console.log(predictions)
if(predictions[0].className==="Hentai"||predictions[0].className==="Porn"||predictions[0].className==="Sexy"){
return true
}else{
return false
}
}
async function blur (image_url) {
const pic= await axios.get(image_url,{responseType: 'arraybuffer' })
return sharp(pic.data).blur(9).toBuffer();
}
async function resize(image_buffer) {
let { width, height } = decode(image_buffer)
let aspect_ratio = width / height
if (aspect_ratio > 1) {
width = 2500
height = Math.floor(2500 / aspect_ratio)
} else if (aspect_ratio < 1) {
height = 2500
width = Math.floor(2500 * aspect_ratio)
} else {
height = 2500
width = 2500
}
return sharp(image_buffer).resize({ width: width, height: height }).toBuffer()
}
module.exports = {check,blur,resize};