Apex-X commited on
Commit
ce2568b
1 Parent(s): f87253f

Upload predicter.py

Browse files
Files changed (1) hide show
  1. roop/predicter.py +25 -0
roop/predicter.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy
2
+ import opennsfw2
3
+ from PIL import Image
4
+
5
+ from roop.typing import Frame
6
+
7
+ MAX_PROBABILITY = 0.85
8
+
9
+
10
+ def predict_frame(target_frame: Frame) -> bool:
11
+ image = Image.fromarray(target_frame)
12
+ image = opennsfw2.preprocess_image(image, opennsfw2.Preprocessing.YAHOO)
13
+ model = opennsfw2.make_open_nsfw_model()
14
+ views = numpy.expand_dims(image, axis=0)
15
+ _, probability = model.predict(views)[0]
16
+ return probability > MAX_PROBABILITY
17
+
18
+
19
+ def predict_image(target_path: str) -> bool:
20
+ return opennsfw2.predict_image(target_path) > MAX_PROBABILITY
21
+
22
+
23
+ def predict_video(target_path: str) -> bool:
24
+ _, probabilities = opennsfw2.predict_video_frames(video_path=target_path, frame_interval=100)
25
+ return any(probability > MAX_PROBABILITY for probability in probabilities)