cc1234 commited on
Commit
6cbade1
1 Parent(s): db1c251

test out nsfw

Browse files
Files changed (2) hide show
  1. app.py +18 -2
  2. requirements.txt +2 -1
app.py CHANGED
@@ -4,8 +4,11 @@ import gradio as gr
4
  from fastcore.all import *
5
  from fastai.vision.all import *
6
  import numpy as np
 
7
  import timm
8
 
 
 
9
 
10
  def parent_labels(o):
11
  "Label `item` with the parent folder name."
@@ -31,10 +34,23 @@ def predict(image, vtt):
31
  sprite = PILImage.create(image)
32
  offsets = []
33
  images = []
 
 
 
34
  for left, top, right, bottom in getVTToffsets(vtt):
35
- offsets.append((left, top, right, bottom))
36
  cut_frame = sprite.crop((left, top, left + right, top + bottom))
37
- images.append(PILImage.create(np.asarray(cut_frame)))
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  # create dataset
40
  test_dl = learn.dls.test_dl(images, bs=64)
 
4
  from fastcore.all import *
5
  from fastai.vision.all import *
6
  import numpy as np
7
+ import opennsfw2 as n2
8
  import timm
9
 
10
+ model = n2.make_open_nsfw_model()
11
+
12
 
13
  def parent_labels(o):
14
  "Label `item` with the parent folder name."
 
34
  sprite = PILImage.create(image)
35
  offsets = []
36
  images = []
37
+ tensors = []
38
+
39
+ pre_process_data = []
40
  for left, top, right, bottom in getVTToffsets(vtt):
 
41
  cut_frame = sprite.crop((left, top, left + right, top + bottom))
42
+ image = n2.preprocess_image(cut_frame, n2.Preprocessing.YAHOO)
43
+ pre_process_data.append((np.expand_dims(image, axis=0), cut_frame, (left, top, right, bottom)))
44
+
45
+
46
+ offsets = []
47
+ images = []
48
+ tensors = [i[0] for i in pre_process_data]
49
+ predictions = model.predict(np.vstack(tensors))
50
+ for i, prediction in enumerate(predictions):
51
+ if prediction[0] < 0.5:
52
+ images.append(PILImage.create(np.asarray(pre_process_data[i][1])))
53
+ offsets.append(pre_process_data[i][2])
54
 
55
  # create dataset
56
  test_dl = learn.dls.test_dl(images, bs=64)
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  fastai==2.7.7
2
- numpy==1.21.5
3
  timm==0.6.7
 
4
  gradio
 
1
  fastai==2.7.7
2
+ numpy==1.24.2
3
  timm==0.6.7
4
+ opennsfw2==0.10.2
5
  gradio