mirror of
https://git.0x0.st/mia/0x0.git
synced 2024-11-24 01:27:12 +01:00
Replace NSFW detector implementation
This commit is contained in:
parent
3330a85c2c
commit
6393538333
8 changed files with 21 additions and 3566 deletions
13
README.rst
13
README.rst
|
@ -95,12 +95,15 @@ Optional:
|
|||
NSFW Detection
|
||||
--------------
|
||||
|
||||
0x0 supports classification of NSFW content via Yahoo’s open_nsfw Caffe
|
||||
neural network model. This works for images and video files and requires
|
||||
the following:
|
||||
0x0 supports classification of NSFW content via
|
||||
`a machine learning model <https://huggingface.co/giacomoarienti/nsfw-classifier>`_.
|
||||
This works for images and video files and requires the following
|
||||
Python modules:
|
||||
|
||||
* Caffe Python module (built for Python 3)
|
||||
* `PyAV <https://github.com/PyAV-Org/PyAV>`_
|
||||
* torch
|
||||
* transformers
|
||||
* pillow
|
||||
* `av <https://github.com/PyAV-Org/PyAV>`_
|
||||
|
||||
|
||||
Virus Scanning
|
||||
|
|
2
fhost.py
2
fhost.py
|
@ -70,7 +70,7 @@ app.config.update(
|
|||
],
|
||||
FHOST_UPLOAD_BLACKLIST = None,
|
||||
NSFW_DETECT = False,
|
||||
NSFW_THRESHOLD = 0.608,
|
||||
NSFW_THRESHOLD = 0.92,
|
||||
VSCAN_SOCKET = None,
|
||||
VSCAN_QUARANTINE_PATH = "quarantine",
|
||||
VSCAN_IGNORE = [
|
||||
|
|
|
@ -176,7 +176,7 @@ NSFW_DETECT = False
|
|||
# are marked as NSFW.
|
||||
#
|
||||
# If NSFW_DETECT is set to False, then this has no effect.
|
||||
NSFW_THRESHOLD = 0.608
|
||||
NSFW_THRESHOLD = 0.92
|
||||
|
||||
|
||||
# If you want to scan files for viruses using ClamAV, specify the socket used
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
Copyright © 2020 Mia Herkt
|
||||
Copyright © 2024 Mia Herkt
|
||||
Licensed under the EUPL, Version 1.2 or - as soon as approved
|
||||
by the European Commission - subsequent versions of the EUPL
|
||||
(the "License");
|
||||
|
@ -18,57 +18,16 @@
|
|||
and limitations under the License.
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
import os
|
||||
import sys
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
|
||||
os.environ["GLOG_minloglevel"] = "2" # seriously :|
|
||||
import caffe
|
||||
import av
|
||||
av.logging.set_level(av.logging.PANIC)
|
||||
from transformers import pipeline
|
||||
|
||||
class NSFWDetector:
|
||||
def __init__(self):
|
||||
npath = Path(__file__).parent / "nsfw_model"
|
||||
self.nsfw_net = caffe.Net(
|
||||
str(npath / "deploy.prototxt"),
|
||||
caffe.TEST,
|
||||
weights = str(npath / "resnet_50_1by2_nsfw.caffemodel")
|
||||
)
|
||||
self.caffe_transformer = caffe.io.Transformer({
|
||||
'data': self.nsfw_net.blobs['data'].data.shape
|
||||
})
|
||||
# move image channels to outermost
|
||||
self.caffe_transformer.set_transpose('data', (2, 0, 1))
|
||||
# subtract the dataset-mean value in each channel
|
||||
self.caffe_transformer.set_mean('data', np.array([104, 117, 123]))
|
||||
# rescale from [0, 1] to [0, 255]
|
||||
self.caffe_transformer.set_raw_scale('data', 255)
|
||||
# swap channels from RGB to BGR
|
||||
self.caffe_transformer.set_channel_swap('data', (2, 1, 0))
|
||||
|
||||
def _compute(self, img):
|
||||
image = caffe.io.load_image(img)
|
||||
|
||||
H, W, _ = image.shape
|
||||
_, _, h, w = self.nsfw_net.blobs["data"].data.shape
|
||||
h_off = int(max((H - h) / 2, 0))
|
||||
w_off = int(max((W - w) / 2, 0))
|
||||
crop = image[h_off:h_off + h, w_off:w_off + w, :]
|
||||
|
||||
transformed_image = self.caffe_transformer.preprocess('data', crop)
|
||||
transformed_image.shape = (1,) + transformed_image.shape
|
||||
|
||||
input_name = self.nsfw_net.inputs[0]
|
||||
output_layers = ["prob"]
|
||||
all_outputs = self.nsfw_net.forward_all(
|
||||
blobs=output_layers, **{input_name: transformed_image})
|
||||
|
||||
outputs = all_outputs[output_layers[0]][0].astype(float)
|
||||
|
||||
return outputs
|
||||
self.classifier = pipeline("image-classification", model="giacomoarienti/nsfw-classifier")
|
||||
|
||||
def detect(self, fpath):
|
||||
try:
|
||||
|
@ -77,23 +36,13 @@ class NSFWDetector:
|
|||
except: container.seek(0)
|
||||
|
||||
frame = next(container.decode(video=0))
|
||||
img = frame.to_image()
|
||||
res = self.classifier(img)
|
||||
|
||||
if frame.width >= frame.height:
|
||||
w = 256
|
||||
h = int(frame.height * (256 / frame.width))
|
||||
else:
|
||||
w = int(frame.width * (256 / frame.height))
|
||||
h = 256
|
||||
frame = frame.reformat(width=w, height=h, format="rgb24")
|
||||
img = BytesIO()
|
||||
frame.to_image().save(img, format="ppm")
|
||||
|
||||
scores = self._compute(img)
|
||||
except:
|
||||
return -1.0
|
||||
|
||||
return scores[1]
|
||||
return max([x["score"] for x in res if x["label"] not in ["neutral", "drawings"]])
|
||||
except: pass
|
||||
|
||||
return -1.0
|
||||
|
||||
if __name__ == "__main__":
|
||||
n = NSFWDetector()
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
|
||||
Copyright 2016, Yahoo Inc.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
File diff suppressed because it is too large
Load diff
Binary file not shown.
|
@ -12,7 +12,9 @@ python_magic
|
|||
clamd
|
||||
|
||||
# nsfw detection
|
||||
numpy
|
||||
torch
|
||||
transformers
|
||||
pillow
|
||||
|
||||
# mod ui
|
||||
av
|
||||
|
|
Loading…
Reference in a new issue