Skip to content

Commit e1fcf9b

Browse files
authored
Merge pull request divamgupta#295 from AEtheve/add-nsfw-filter
Add nsfw filter
2 parents 034c276 + c8cbd97 commit e1fcf9b

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

electron_app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"electron-notarize": "^1.2.1",
3333
"electron-settings": "^4.0.2",
3434
"javascript-time-ago": "^2.3.13",
35+
"nsfwjs": "^2.4.2",
3536
"konva": "^8.3.13",
3637
"v-click-outside": "^3.1.2",
3738
"vue": "^2.6.11",

electron_app/src/StableDiffusion.vue

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
import { send_to_py } from "./py_vue_bridge.js"
77
import {get_tokens} from './clip_tokeniser/clip_encoder.js'
8+
const nsfwjs = require('nsfwjs');
89
910
function remove_non_ascii(str) {
1011
@@ -27,6 +28,7 @@ export default {
2728
return {
2829
is_backend_loaded : false,
2930
is_input_avail : false,
31+
nsfw_filter: true,
3032
model_loading_msg : "",
3133
model_loading_title : "",
3234
loading_percentage : -1 ,
@@ -50,11 +52,35 @@ export default {
5052
}
5153
if(msg_code == "nwim"){
5254
let impath = msg.substring(5).trim()
53-
if(this.attached_cbs){
54-
if(this.attached_cbs.on_img)
55-
this.attached_cbs.on_img(impath);
56-
}
5755
56+
const img = new Image();
57+
img.src = "file://" + impath;
58+
59+
img.attached_cbs = this.attached_cbs;
60+
61+
62+
img.onload = () => {
63+
if (this.nsfw_filter == false) {
64+
if (img.attached_cbs) {
65+
if (img.attached_cbs.on_img)
66+
img.attached_cbs.on_img(impath);
67+
}
68+
}
69+
nsfwjs.load()
70+
.then(model => model.classify(img))
71+
.then(predictions => {
72+
switch (predictions[0].className) {
73+
case 'Hentai':
74+
case 'Porn':
75+
case 'Sexy':
76+
impath = "nsfw";
77+
}
78+
if (img.attached_cbs) {
79+
if (img.attached_cbs.on_img)
80+
img.attached_cbs.on_img(impath);
81+
}
82+
});
83+
}
5884
}
5985
6086
if(msg_code == "mdvr"){

electron_app/src/assets/nsfw.png

4.98 KB
Loading

electron_app/src/components/ImageItem.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
</div>
1919

2020

21-
<img @click="open_image_popup( path )" class="gal_img" :src="'file://' + path " style="max-height: 100% ; max-width: 100%;" >
21+
<img v-if="path != 'nsfw'" @click="open_image_popup( path )" class="gal_img" :src="'file://' + path " style="max-height: 100% ; max-width: 100%;" >
22+
<img v-else @click="open_image_popup( path )" class="gal_img" :src="require('@/assets/nsfw.png')" style="max-height: 100% ; max-width: 100%;" >
2223
<br>
2324
<div v-if="!hide_extra_save_button" @click="save_image(path)" class="l_button">Save Image</div>
2425
</div>

0 commit comments

Comments
 (0)