Пример #1
0
 def __init__(self,
              blur_mask_fade=NUDITY_DETECTION_SETTINGS['blur_mask_fade'],
              threshold=NUDITY_DETECTION_SETTINGS['threshold']):
     self._classifier = NudeClassifier()
     self._blur = ImageBlur(PixelBlur(30), blur_mask_fade)
     self._threshold = threshold
     self._graph = tf.get_default_graph()
Пример #2
0
def detect(src_path, video):
    frame_size = (160, 90)
    interval_millis = 1000
    classifier = NudeClassifier(settings.NUDE_NET_CLASSIFIER_MODEL_PATH)

    video.status = Video.Status.DETECTING
    video.save()

    src_name = os.path.splitext(os.path.basename(src_path))[0]
    output_dir = os.path.join(os.path.dirname(src_path), src_name)
    os.makedirs(output_dir)
    extract_frames(src_path, output_dir, frame_size, interval_millis)

    detected = list()
    thresold = 3 * interval_millis
    print('start detecting %d files' % (len(os.listdir(output_dir))))
    for frame in sorted(os.listdir(output_dir), key=lambda f: frame_order(f)):
        order = frame_order(frame)
        if order < 0:
            continue
        framepath = os.path.join(output_dir, frame)
        if not os.path.exists(framepath) or os.path.isdir(framepath):
            continue
        result = classifier.classify(framepath)[framepath]
        nudity_prob = result['nude']
        if nudity_prob > 0.8:
            start_millis = order * interval_millis
            end_millis = (order + 1) * interval_millis
            if not detected:
                detected.append(
                    DetectedScene(src_video=video,
                                  start_millis=start_millis,
                                  end_millis=end_millis,
                                  cause=DetectedScene.DetectionCause.NUDITY))
            else:
                latest = detected[-1]
                if latest.end_millis + thresold <= start_millis:
                    detected.append(
                        DetectedScene(
                            src_video=video,
                            start_millis=start_millis,
                            end_millis=end_millis,
                            cause=DetectedScene.DetectionCause.NUDITY))
                else:
                    latest.end_millis = end_millis
    print('the number of detected scenes is %d' % len(detected))
    for scene in detected:
        scene.save()
    video.status = Video.Status.DETECTED
    video.save()
    try:
        shutil.rmtree(output_dir)
    except Exception as e:
        print('fail to remove directory', e)
    return detected
Пример #3
0
async def sex_message_check(redis, db):
    classifier = NudeClassifier()
    while True:
        lst = await redis.hgetall('avr', encoding='utf-8')
        if not lst == {}:
            for i in lst.keys():
                path = lst[i]
                status = classifier.classify(path)
                if status[path]['safe'] < status[path]['unsafe']:
                    await Message.delete_image(db=db, user=i, image=path)

        await asyncio.sleep(10)
Пример #4
0
    def __init__(self, bot: Red):
        super().__init__()
        self.bot = bot
        self.config = Config.get_conf(self, identifier=9811198108111121, force_registration=True)

        default_guild = {"enabled": False, "channel_id": None}

        self.config.register_guild(**default_guild)

        # self.detector = NudeDetector()
        self.classifier = NudeClassifier()

        self.data_path: pathlib.Path = cog_data_path(self)

        self.current_processes = 0
Пример #5
0
class CensorNudity:
    def __init__(self,
                 blur_mask_fade=NUDITY_DETECTION_SETTINGS['blur_mask_fade'],
                 threshold=NUDITY_DETECTION_SETTINGS['threshold']):
        self._classifier = NudeClassifier()
        self._blur = ImageBlur(PixelBlur(30), blur_mask_fade)
        self._threshold = threshold
        self._graph = tf.get_default_graph()

    def __call__(self, image: Image) -> Image:
        # Create temporary file (nudenet needs image to be saved)
        path = Path(str(uuid.uuid4()) + '.jpg')
        Image.save(image, path)

        with self._graph.as_default():
            detected_nudity = self._classifier.classify([path])

        # Delete temporary file
        path.unlink()

        if detected_nudity[path]['unsafe'] > self._threshold:
            nudity_boxes = [BoundingBox(0, 0, *image.size)]
            blurred_image_np = self._blur.blur(image, nudity_boxes)
            return fromarray(blurred_image_np)
        else:
            return image
Пример #6
0
def scan_image(image_url):
    if image_url is None:
        return None

    file_name = ''.join(
        random.choices(string.ascii_uppercase + string.digits, k=18))
    tmp_path = os.path.join(tempfile.gettempdir(), file_name)

    with urllib.request.urlopen(image_url) as url:
        output = open(tmp_path, "wb")
        output.write(url.read())
        output.close()

    classifier = NudeClassifier()
    img_scan_result = classifier.classify(tmp_path)
    os.remove(tmp_path)
    return img_scan_result[tmp_path]['unsafe']
Пример #7
0
def check_for_nudity(user_data: dict):
    photo_path_list = user_data["message_photo_paths"]
    print(
        "\nChecking for nudity in message photos. This might take a while depending on the number of photos."
    )
    classifier = NudeClassifier()
    classification = classifier.classify(photo_path_list, 4)
    unsafe_photo_paths = []
    for photo_path in photo_path_list:
        try:
            if classification[photo_path]["unsafe"] > 0.95:
                print("OYASHII: ", photo_path)
                unsafe_photo_paths.append(photo_path)
        except KeyError:
            print("A photo is mysteriously missing.")

    nbr_of_unsafe_photos = len(unsafe_photo_paths)
    output_html = unsafe_photo_carousel_html_generator(unsafe_photo_paths)
    user_data["unsafe_html"] = output_html
    user_data["nbr_of_unsafe_photos"] = nbr_of_unsafe_photos
    user_data["unsafe_photo_paths"] = unsafe_photo_paths
    return user_data
Пример #8
0
    def run(self, path):
        """ Classify an image using NudeNet."""
        # if there is not a classifier, create it. This takes a long time, so create only one classifier (global variable)
        global classifier
        if classifier is None:
            self.logger().debug('Creating classifier from model=%s',
                                self.myconfig('model'))
            modelfile = self.myconfig('model')
            classifier = NudeClassifier(modelfile)
        threshold = float(self.myconfig('threshold'))

        self.logger().debug('Classifying path=%s', path)

        # classify path
        if os.path.isabs(path):
            abspath = path
            relpath = relative_path(path, self.myconfig('casedir'))
        else:
            abspath = os.path.join(self.myconfig('casedir'), path)
            relpath = path
        result = dict(path=relpath,
                      aiclassify=dict(classifier='NudeNet'),
                      preview=relpath)
        try:
            # we must convert the results, since they are returned as numpy object
            classification = classifier.classify(abspath)[abspath]
            result['aiclassify']['results'] = dict(
                safe=float(classification['safe']),
                unsafe=float(classification['unsafe']))
            result['aiclassify']['is_nude'] = result['aiclassify']['results'][
                'unsafe'] > threshold
        except Exception as exc:
            self.logger().warning('Cannot process path=%s %s', path, exc)
            result['aiclassify']['results'] = dict(safe=None, unsafe=None)
            result['aiclassify']['is_nude'] = None
        yield result
Пример #9
0
import pydload
import uuid
import json
import time
import requests
import logging

from flask import Flask, request
from flask_cors import CORS, cross_origin

from nudenet import NudeClassifier
classifier = NudeClassifier("models/classifier_model")
app = Flask(__name__)
cors = CORS(app)

# @app.route('/hello')
# @cross_origin()
# def hello():
#     return "Hello World!"


@app.route('/nudenet', methods=['GET', 'POST'])
def nudenet_classifier_from_url():
    if request.method == 'GET':
        url = request.args.get('url')
    elif request.method == 'POST':
        url = request.json['url']

    try:
        path = str(uuid.uuid4())
        dload_status = pydload.dload(url, path)
Пример #10
0
import os
import cv2
from nudenet import NudeClassifier, NudeDetector

video_path = 'p**n.mp4'

classifier = NudeClassifier()
dic = classifier.classify_video(video_path, batch_size=4)
# dic = classifier.classify('example.jpg')

# detector = NudeDetector()
# dic = detector.detect('example.jpg')

print(dic)

video = cv2.VideoCapture(video_path)
print(video.isOpened())
fps = video.get(cv2.CAP_PROP_FPS)
length = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
print("fps:", fps, "length", length)

# print(os.getcwd())
Пример #11
0
Options:
--tolerance=<tolerance>   Number of video frames to tolerate before issuing a warning.[default=4]
--debug                   Prints the inference logs.
"""
from collections import deque
import cv2

from docopt import docopt
from nudenet import NudeClassifier
from secureconnect import inference
from secureconnect.exceptions import UnsafeEnvironment


# load the model once for every inference.
mdl = NudeClassifier()


def is_safety_buffer_critical(queue, tolerance):
    """
    Exit program with an error.

    If all items in the queue are True. This means we
    have exhausted our tolerance for nsfw content and it
    is unsafe for continuing to use video-source.
    """
    unsafe_flags = [item for item in list(queue) if item]
    if unsafe_flags == tolerance:
        raise UnsafeEnvironment

Пример #12
0
import requests
import base64
import random
import json
from nudenet import NudeClassifier
import os
from datetime import datetime
from termcolor import colored

nsfw_classifier = NudeClassifier()
animals = ["cat", "dog", "wolf", "otter", "panda"]
muted_people = []

cat_images_api = "https://api.thecatapi.com/v1/images/search"
dog_facts_api = "https://dog-facts-api.herokuapp.com/api/v1/resources/dogs?number=1"
trivia_api = "https://opentdb.com/api.php?amount=1&category=9&difficulty=easy&type=boolean&encode=base64"
yoda_api = "http://yoda-api.appspot.com/api/v1/yodish?text="
joke_api = "https://official-joke-api.appspot.com/random_joke"
nasa_image_search_api = "https://images-api.nasa.gov/search?"


def get_json_data(url):
    result = requests.get(url)
    result_json = result.json()
    return result_json


def get_cat_image_url():
    json_object = get_json_data(cat_images_api)
    image_object = json_object[0]
    image_url = image_object["url"]
Пример #13
0
    def run(self, path):
        """ Classify a video using NudeNet."""
        if os.path.isabs(path):
            relpath = relative_path(path, self.myconfig('casedir'))
            abspath = path
        else:
            relpath = path
            abspath = os.path.join(self.myconfig('casedir'), path)

        self.check_params(abspath, check_path=True, check_path_exists=True)
        # if there is not a classifier, create it. This takes a long time, so create only one classifier (global variable)
        global classifier
        if classifier is None:
            self.logger().debug('Creating classifier from model=%s',
                                self.myconfig('model'))
            modelfile = self.myconfig('model')
            classifier = NudeClassifier(modelfile)

        if os.path.isabs(path):
            relpath = relative_path(path, self.myconfig('casedir'))
        else:
            relpath = path

        previews = list(self.from_module.run(path))
        # if the previews could be created, the video cannot be parsed: return None
        if not previews or len(previews) == 0:
            return [
                dict(path=relpath,
                     aiclassify=dict(classifier='NudeNet',
                                     results=dict(safe=None, unsafe=None),
                                     is_nude=None))
            ]

        # else, the video was parsed correctly: test each one of the previews.
        max_unsafe = 0
        min_safe = 0
        num_is_nude = 0
        preview_path = None
        for preview_image in previews[0]['preview']:
            # classify preview_image
            result = list(super().run(preview_image))[0]
            if result['aiclassify']['results']['unsafe'] is not None and result[
                    'aiclassify']['results']['unsafe'] > max_unsafe:
                max_unsafe = result['aiclassify']['results']['unsafe']
                min_safe = result['aiclassify']['results']['safe']
            if result['aiclassify']['is_nude']:
                num_is_nude += 1
                # if an image is unsafe: set the preview to this image
                preview_path = relative_path(preview_image,
                                             self.myconfig('casedir'))
        final_result = dict(classifier='NudeNet',
                            results=dict(safe=min_safe, unsafe=max_unsafe),
                            is_nude=None)
        if len(previews[0]['preview']) > 0:
            final_result['is_nude'] = (
                1.0 * num_is_nude / len(previews[0]['preview']) > float(
                    self.myconfig('threshold_preview')))
            if preview_path is None:
                # if no preview path is already set, get the image in the middle
                preview_path = previews[0]['preview'][int(
                    len(previews[0]['preview']) / 2)]
        return [
            dict(path=relpath, aiclassify=final_result, preview=preview_path)
        ]
Пример #14
0
# https://github.com/notAI-tech/NudeNet
# https://github.com/ndaysinaiK/nude-test

# pip install --upgrade nudenet

import time
import datetime
start = time.time()

# Import module
from nudenet import NudeClassifier

# initialize classifier (downloads the checkpoint file automatically the first time)
classifier = NudeClassifier()

from os import listdir
from os.path import isfile, join
from pathlib import Path
from shutil import copyfile

mypath = '../img/'
safePath = mypath + 'safe/'
unsafePath = mypath + 'unsafe/'

onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
Path(safePath).mkdir(parents=True, exist_ok=True)
Path(unsafePath).mkdir(parents=True, exist_ok=True)

for f in listdir(mypath):
    if isfile(join(mypath, f)):
        filename = mypath + f
Пример #15
0
import pyautogui
from nudenet import NudeClassifier
import time
classifier = NudeClassifier('models/classifier_model')

def main():
    # while True:
    # for i in range (10):
    #     print("taking screenshot")
    #     myScreenshot = pyautogui.screenshot()
    #     myScreenshot.save('imgs/test.png')

    #     print("starting classification")
    #     classifier = NudeClassifier('models/classifier_model')
    #     print(classifier.classify('imgs/test.png'))

    filepath = 'imgs/test.png'
    print("starting classification")
    classified_dict = classifier.classify('imgs/test.png')
    print(classified_dict)
    print(classified_dict[filepath]['unsafe'])

if __name__ == "__main__":
    main()
Пример #16
0
    def __init__(self, mode, specific_formats=None, specific_folder=None):

        self.mode = mode
        self.lst_of = {}
        self.doc_ext = []
        self.img_ext = []
        self.vid_ext = []
        self.sound_ext = []
        self.zip_ext = []
        self.code_ext = []
        self.media_ext = []
        self.data_ext = []
        self.app_ext = []
        self.font_ext = []
        self.sys_ext = []
        self.flags = []
        self.specifics = []
        self.all_files = {}
        self.errors = []
        self.file_structure = {}
        self.load_ext()
        self.now = datetime.now()
        self.dt_string = self.now.strftime("%d-%m-%Y %Hh%M")
        self.nude_classifier = NudeClassifier()
        self.nude_detector = NudeDetector()
        self.s = sched.scheduler(time.time, time.sleep)

        self.number_of_files = 0
        self.time_taken = 0
        self.prev_dir = None
        self.curr_dir = None
        self.faces = None
        self.points = None

        self.walked_dir = "checked.dir"
        self.all_walked = []
        self.load_walked()

        self.available_dirs = []
        self.non_available_dirs = []
        self.attach = ":/"
        self.let_dir = [
            "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
            "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
        ]

        self.runt = threading.Thread(target=self.find_all_dirs)
        self.runt.start()

        self.master_ext = [
            self.doc_ext, self.img_ext, self.vid_ext, self.sound_ext,
            self.zip_ext, self.code_ext, self.media_ext, self.data_ext,
            self.app_ext, self.font_ext, self.sys_ext, self.flags
        ]

        self.type_s = [
            "Documents", "Images", "Videos", "Sounds", "Compressed_Files",
            "Programming_Files", "Discs_Media", "Databases", "Applications",
            "Fonts", "System_Files", "Infected"
        ]

        self.face_detection = fc.Detection()
        self.face_recognition = fc.Recognition()
        self.face_verification = fc.Verification()

        if specific_formats is not None and specific_folder is not None:
            self.specifics = self.specifics + specific_formats
            self.master_ext.append(self.specifics)

            self.type_s.append(specific_folder)
Пример #17
0
bot_mode = config['bot']['mode']
debug = config.getboolean('bot', 'debug')
token = config['bot']['token']
clientid = config['bot']['clientid']
prefix = str(config['bot']['prefix'])
botperms = config['bot']['botperms']

# policy
denythreshold = config.getfloat('policy', 'denythreshold')
accepted_formats = config['policy']['acceptedfiles'].split(',')
allowtie = config.getboolean('policy', 'allow_ties')

bot = commands.Bot(command_prefix=prefix)
# nude detector is not used unless in debug, so you can see why some images are deemed nsfw
detector = NudeDetector()
classifier = NudeClassifier()


# cog loading
@bot.event
async def on_ready():
    invlink = f"https://discord.com/oauth2/authorize?client_id={clientid}&permissions={botperms}&scope=bot"
    print(f"lewd stopper is ready, invite me at link {invlink}")
    if debug:
        print(
            "==== WARNING: DEBUG MODE IS ON! NO MESSAGES WILL BE DELETED.====\n==== FOR THIS BOT TO MODERATE PROPERLY, "
            "TURN OFF DEBUG IN CONFIG.INI ====")


# this function will be ran async by the executor
def get_classification(image_loc):
Пример #18
0
from nudenet import NudeClassifier

m = NudeClassifier()


def predictor(x, batch_size=2, extras=[]):
    preds = m.classify(x, batch_size=batch_size)
    return [preds.get(_) for _ in x]
Пример #19
0
from nudenet import NudeClassifier
import os
files = os.listdir('Valid')
if not os.path.exists('Valid/Pron'):
    os.makedirs('Valid/Pron')
if not os.path.exists('Valid/Error'):
    os.makedirs('Valid/Error')
files.remove('Pron')
files.remove('Error')
classifier = NudeClassifier()
for i in files:
    try:
        data = classifier.classify('Valid/' + i)[i]['unsafe']
        if data > 0.7:
            print(i + ' is OK')
            os.rename('Valid/' + i, 'Valid/Pron/' + i)
        else:
            os.rename('Valid/' + i, 'Valid/Error/' + i)
    except:
        continue
Пример #20
0
from nudenet import NudeClassifier
import sys

classifier = NudeClassifier()

image_path = sys.argv[1]

result = classifier.classify(image_path)
print(0 if result[image_path]['safe'] > result[image_path]['unsafe'] else 1)
Пример #21
0
class sorter():
    def __init__(self, mode, specific_formats=None, specific_folder=None):

        self.mode = mode
        self.lst_of = {}
        self.doc_ext = []
        self.img_ext = []
        self.vid_ext = []
        self.sound_ext = []
        self.zip_ext = []
        self.code_ext = []
        self.media_ext = []
        self.data_ext = []
        self.app_ext = []
        self.font_ext = []
        self.sys_ext = []
        self.flags = []
        self.specifics = []
        self.all_files = {}
        self.errors = []
        self.file_structure = {}
        self.load_ext()
        self.now = datetime.now()
        self.dt_string = self.now.strftime("%d-%m-%Y %Hh%M")
        self.nude_classifier = NudeClassifier()
        self.nude_detector = NudeDetector()
        self.s = sched.scheduler(time.time, time.sleep)

        self.number_of_files = 0
        self.time_taken = 0
        self.prev_dir = None
        self.curr_dir = None
        self.faces = None
        self.points = None

        self.walked_dir = "checked.dir"
        self.all_walked = []
        self.load_walked()

        self.available_dirs = []
        self.non_available_dirs = []
        self.attach = ":/"
        self.let_dir = [
            "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
            "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
        ]

        self.runt = threading.Thread(target=self.find_all_dirs)
        self.runt.start()

        self.master_ext = [
            self.doc_ext, self.img_ext, self.vid_ext, self.sound_ext,
            self.zip_ext, self.code_ext, self.media_ext, self.data_ext,
            self.app_ext, self.font_ext, self.sys_ext, self.flags
        ]

        self.type_s = [
            "Documents", "Images", "Videos", "Sounds", "Compressed_Files",
            "Programming_Files", "Discs_Media", "Databases", "Applications",
            "Fonts", "System_Files", "Infected"
        ]

        self.face_detection = fc.Detection()
        self.face_recognition = fc.Recognition()
        self.face_verification = fc.Verification()

        if specific_formats is not None and specific_folder is not None:
            self.specifics = self.specifics + specific_formats
            self.master_ext.append(self.specifics)

            self.type_s.append(specific_folder)

    def get_mode(self):
        return self.__mode

    def get_lst_of(self):
        return self.__lst_of

    def get_doc_ext(self):
        return self.__doc_ext

    def get_img_ext(self):
        return self.__img_ext

    def get_vid_ext(self):
        return self.__vid_ext

    def get_sound_ext(self):
        return self.__sound_ext

    def get_zip_ext(self):
        return self.__zip_ext

    def get_code_ext(self):
        return self.__code_ext

    def get_media_ext(self):
        return self.__media_ext

    def get_data_ext(self):
        return self.__data_ext

    def get_app_ext(self):
        return self.__app_ext

    def get_font_ext(self):
        return self.__font_ext

    def get_sys_ext(self):
        return self.__sys_ext

    def get_flags(self):
        return self.__flags

    def get_specifics(self):
        return self.__specifics

    def get_all_files(self):
        return self.__all_files

    def get_errors(self):
        return self.__errors

    def get_file_structure(self):
        return self.__file_structure

    def get_now(self):
        return self.__now

    def get_dt_string(self):
        return self.__dt_string

    def get_nude_classifier(self):
        return self.__nude_classifier

    def get_number_of_files(self):
        return self.__number_of_files

    def get_time_taken(self):
        return self.__time_taken

    def get_prev_dir(self):
        return self.__prev_dir

    def get_curr_dir(self):
        return self.__curr_dir

    def get_master_ext(self):
        return self.__master_ext

    def get_type_s(self):
        return self.__type_s

    def set_mode(self, value):
        self.__mode = value

    def set_lst_of(self, value):
        self.__lst_of = value

    def set_doc_ext(self, value):
        self.__doc_ext = value

    def set_img_ext(self, value):
        self.__img_ext = value

    def set_vid_ext(self, value):
        self.__vid_ext = value

    def set_sound_ext(self, value):
        self.__sound_ext = value

    def set_zip_ext(self, value):
        self.__zip_ext = value

    def set_code_ext(self, value):
        self.__code_ext = value

    def set_media_ext(self, value):
        self.__media_ext = value

    def set_data_ext(self, value):
        self.__data_ext = value

    def set_app_ext(self, value):
        self.__app_ext = value

    def set_font_ext(self, value):
        self.__font_ext = value

    def set_sys_ext(self, value):
        self.__sys_ext = value

    def set_flags(self, value):
        self.__flags = value

    def set_specifics(self, value):
        self.__specifics = value

    def set_all_files(self, value):
        self.__all_files = value

    def set_errors(self, value):
        self.__errors = value

    def set_file_structure(self, value):
        self.__file_structure = value

    def set_now(self, value):
        self.__now = value

    def set_dt_string(self, value):
        self.__dt_string = value

    def set_nude_classifier(self, value):
        self.__nude_classifier = value

    def set_number_of_files(self, value):
        self.__number_of_files = value

    def set_time_taken(self, value):
        self.__time_taken = value

    def set_prev_dir(self, value):
        self.__prev_dir = value

    def set_curr_dir(self, value):
        self.__curr_dir = value

    def set_master_ext(self, value):
        self.__master_ext = value

    def set_type_s(self, value):
        self.__type_s = value

    def del_mode(self):
        del self.__mode

    def del_lst_of(self):
        del self.__lst_of

    def del_doc_ext(self):
        del self.__doc_ext

    def del_img_ext(self):
        del self.__img_ext

    def del_vid_ext(self):
        del self.__vid_ext

    def del_sound_ext(self):
        del self.__sound_ext

    def del_zip_ext(self):
        del self.__zip_ext

    def del_code_ext(self):
        del self.__code_ext

    def del_media_ext(self):
        del self.__media_ext

    def del_data_ext(self):
        del self.__data_ext

    def del_app_ext(self):
        del self.__app_ext

    def del_font_ext(self):
        del self.__font_ext

    def del_sys_ext(self):
        del self.__sys_ext

    def del_flags(self):
        del self.__flags

    def del_specifics(self):
        del self.__specifics

    def del_all_files(self):
        del self.__all_files

    def del_errors(self):
        del self.__errors

    def del_file_structure(self):
        del self.__file_structure

    def del_now(self):
        del self.__now

    def del_dt_string(self):
        del self.__dt_string

    def del_nude_classifier(self):
        del self.__nude_classifier

    def del_number_of_files(self):
        del self.__number_of_files

    def del_time_taken(self):
        del self.__time_taken

    def del_prev_dir(self):
        del self.__prev_dir

    def del_curr_dir(self):
        del self.__curr_dir

    def del_master_ext(self):
        del self.__master_ext

    def del_type_s(self):
        del self.__type_s

    def run_t(self):
        self.s.enter(60, 1, self.find_all_dirs, (self.s, ))
        self.s.run()

    def find_all_dirs(self):

        for i in range(len(self.let_dir)):
            dr = self.let_dir[i] + self.attach
            if os.path.isdir(dr):
                self.available_dirs.append(dr)
            else:
                self.non_available_dirs.append(dr)

        #self.s.enter(60, 1, self.find_all_dirs, (self.s,))

    def load_ext(self):
        extensions_path = "C:/Users/GabhaDi/eclipse-workspace2/project sortMyFiles/src/extensions/"
        name = [
            "application", "code", "data", "document", "flags", "font",
            "image", "media", "sound", "system", "video", "zip"
        ]
        #full_path = os.path.abspath(extensions_path)

        i = 0

        print("The full to your extensions is ", extensions_path)

        if os.path.isdir(extensions_path) is True:
            print("loading all extensions...")
            list_files = os.listdir(extensions_path)

            for file in list_files:

                if "document" in file:

                    with open(extensions_path + file) as f:
                        for line in f:
                            self.doc_ext.append(line.rstrip("\n"))

                    print("Successfully loaded document exts, size : ",
                          len(self.doc_ext), self.doc_ext)

                if "image" in file:

                    with open(extensions_path + file) as f:
                        for line in f:
                            self.img_ext.append(line.rstrip("\n"))

                    print("Successfully loaded image exts, size : ",
                          len(self.img_ext), self.img_ext)

                if "video" in file:

                    with open(extensions_path + file) as f:
                        for line in f:
                            self.vid_ext.append(line.rstrip("\n"))

                    print("Successfully loaded video exts, size : ",
                          len(self.vid_ext), self.vid_ext)

                if "application" in file:

                    with open(extensions_path + file) as f:
                        for line in f:
                            self.app_ext.append(line.rstrip("\n"))

                    print("Successfully loaded app exts, size : ",
                          len(self.app_ext), self.app_ext)

                if "code" in file:

                    with open(extensions_path + file) as f:
                        for line in f:
                            self.code_ext.append(line.rstrip("\n"))

                    print("Successfully loaded source code exts, size : ",
                          len(self.code_ext), self.code_ext)

                if "data" in file:

                    with open(extensions_path + file) as f:
                        for line in f:
                            self.data_ext.append(line.rstrip("\n"))

                    print("Successfully loaded database exts, size : ",
                          len(self.data_ext), self.data_ext)

                if "flags" in file:

                    with open(extensions_path + file) as f:
                        for line in f:
                            self.flags.append(line.rstrip("\n"))

                    print(
                        "Successfully loaded dangerous and malicious exts, size : ",
                        len(self.flags), self.flags)

                if "font" in file:

                    with open(extensions_path + file) as f:
                        for line in f:
                            self.font_ext.append(line.rstrip("\n"))

                    print("Successfully loaded font exts, size : ",
                          len(self.font_ext), self.font_ext)

                if "media" in file:

                    with open(extensions_path + file) as f:
                        for line in f:
                            self.media_ext.append(line.rstrip("\n"))

                    print("Successfully loaded media exts, size : ",
                          len(self.media_ext), self.media_ext)

                if "sound" in file:

                    with open(extensions_path + file) as f:
                        for line in f:
                            self.sound_ext.append(line.rstrip("\n"))

                    print("Successfully loaded sound and audio exts, size : ",
                          len(self.sound_ext), self.sound_ext)

                if "system" in file:

                    with open(extensions_path + file) as f:
                        for line in f:
                            self.sys_ext.append(line.rstrip("\n"))

                    print("Successfully loaded system exts, size : ",
                          len(self.sys_ext), self.sys_ext)

                if "zip" in file:

                    with open(extensions_path + file) as f:
                        for line in f:
                            self.zip_ext.append(line.rstrip("\n"))

                    print(
                        "Successfully loaded archive and compressions exts, size : ",
                        len(self.zip_ext), self.zip_ext)

            print("Successfully loaded all extensions...")

    def load_walked(self):

        if os.path.isfile(self.walked_dir):

            with open(self.walked_dir) as f:
                for line in f:
                    self.all_walked.append(line.rstrip("\n"))
        else:
            print("File doesnt't exist.")

    def load_session(self, dir):

        if len(self.all_files) != 0:
            self.all_files.clear()
        all_files = []
        pc = []
        if os.path.isdir(dir) == True:
            dir_list = os.listdir(dir)
            session = "session.sess"
            session_cp = "session_cp.sess"
            if (session in dir_list) is True and (session_cp
                                                  in dir_list) is True:
                with open(session) as f:
                    for line in f:
                        all_files.append(
                            [int(n) for n in line.strip().split(',')])
                    for pair in all_files:
                        try:
                            self.all_files[pair[0]] = pair[1]
                        except IndexError:
                            print(
                                "A line in the file doesn't have enough entries"
                            )

                    with open(session_cp) as fn:
                        for line in fn:
                            pc.append(
                                [int(n) for n in line.strip().split(',')])

                        for pair in pc:
                            try:
                                self.prev_dir = pair[0]
                                self.curr_dir = pair[1]
                            except IndexError:
                                print(
                                    "A line in the file doesn't have enough entries"
                                )
                    print(
                        f"Successfully loaded sessions for from files {session} and {session_cp} from directory {dir}"
                    )
                    return 1
            else:
                print(
                    f"Something went wrong...i couldnt find the files {session} and {session_cp} in dir"
                )
                return -1
        else:
            print(
                f"Something went wrong...invalid directory provided ERROR : {dir} doesn't exist"
            )
            return -1

    def add_dir(self, new_dir):

        if (os.path.exists(self.walked_dir) == False):
            f = open(self.walked_dir, "w+")
        else:
            f = open(self.walked_dir, "a+")

        _checked = open(self.walked_dir, 'r').readlines()
        _checked = [i.strip() for i in _checked]

        if (new_dir in _checked) is True:
            print("Skipping ", new_dir, " it has been added.")

        elif new_dir not in _checked:

            f.write(new_dir + "\n")
        else:
            f.write(new_dir + "\n")

        f.close()

    def files(self, directory):
        total_files = 0
        total_directories = 0
        if os.path.isdir(directory) == True:
            dir_list = os.listdir(directory)
            print("Listing dir")
            print(dir_list)
            print("Total size : ", len(dir_list))

            for file in dir_list:
                print(file)
                if os.path.isfile(os.path.abspath(file) == True):
                    i = 0

                    for extensions in self.master_ext:

                        for ext in extensions:

                            if ext in file:

                                self.lst_of[file] = self.type_s[i]

                if os.path.isdir(os.path.abspath(file)) == True:
                    #new_dir_list = os.listdir(file)
                    print()
                    print()
                    print(os.path.dirname(file))
                    self.files(os.path.abspath(file))

                    i += 1

    def copy_(self, new_directory):
        if not os.path.exists(new_directory):
            os.mkdir(new_directory)

            for key, value in self.lst_of.items():
                #new_directory = new_directory + value + "/"
                try:
                    print(key, new_directory + "/" + value + "/")
                    if not os.path.exists(new_directory + "/" + value + "/"):
                        os.mkdir(new_directory + "/" + value + "/")
                        copy(key, new_directory + "/" + value + "/")
                    else:
                        copy(key, new_directory + "/" + value + "/")
                except Exception as e:
                    print(e)
                    continue

        else:

            for key, value in self.lst_of.items():
                try:
                    #new_directory = new_directory + value + "/"
                    print(key, new_directory + "/" + value + "/")
                    if not os.path.exists(new_directory + "/" + value + "/"):
                        os.mkdir(new_directory + "/" + value + "/")
                        copy(key, new_directory + "/" + value + "/")
                    else:
                        copy(key, new_directory + "/" + value + "/")
                except Exception as e:
                    print(e)
                    continue

    def move_(self, new_directory):
        if not os.path.exists(new_directory):
            os.mkdir(new_directory)

            for key, value in self.lst_of.items():
                #new_directory = new_directory + value + "/"
                try:
                    print(key, new_directory + "/" + value + "/")
                    if not os.path.exists(new_directory + "/" + value + "/"):
                        os.mkdir(new_directory + "/" + value + "/")
                        move(key, new_directory + "/" + value + "/")
                    else:
                        move(key, new_directory + "/" + value + "/")
                except Exception as e:
                    print(e)
                    continue

        else:

            for key, value in self.lst_of.items():
                try:
                    #new_directory = new_directory + value + "/"
                    print(key, new_directory + "/" + value + "/")
                    if not os.path.exists(new_directory + "/" + value + "/"):
                        os.mkdir(new_directory + "/" + value + "/")
                        move(key, new_directory + "/" + value + "/")
                    else:
                        move(key, new_directory + "/" + value + "/")
                except Exception as e:
                    print(e)
                    continue

    def rollback(self, files, prev_dir, curr_dir):

        if len(files) < 0:
            print(
                "Please provide the files that you want to undo the process.")
            return

        if type(files) is not dict:
            print("files should be list, but ", type(files),
                  " was provided instead.")
            return

        if not os.path.isdir(prev_dir):
            print(
                "The provided previous directory is not a directory or doesn't exist, please check ",
                prev_dir)
            return

        if not os.path.isdir(curr_dir):
            print(
                "The provide current directory is not a directory or doesn't exist, please check ",
                curr_dir)
            return

        print("Rollback Operation started : from - ", curr_dir, " to  - ",
              prev_dir)

        dir_walk = os.walk(curr_dir)
        new_files = {}

        for (root, dirs, files_) in dir_walk:

            for f in files_:
                for file, full_path in files.items():

                    if f == file:

                        new_files[os.path.join(root, f)] = full_path

        print(new_files)
        for each_file, fullpath in new_files.items():
            try:
                print("FROM -- [", each_file, "] TO -- [", fullpath,
                      "] success")
                move(each_file, fullpath)

            except Exception as e:
                print(e)
                continue

    def display(self, dic):

        for key, value in dic.items():
            try:
                print(key, value)
            except Exception as e:
                print(e)
                continue

    #Todo rollback code here
    #Main function is to reverse any sorting done by the program
    # 1. Get all the files and their paths
    # 2. Move them from their curr directory to their previous directories
    # 3. Verify and check if the operation was successful
    # 4. Save the progress for later use

    def check_for_faces(self, directory):

        (self.im_width, self.im_height) = (160, 160)
        face_pic = 0
        file_sess = "face.check"
        print("Checking for faces...", directory)

        directory = directory + "/images"

        face_ = directory + "/" + "faces"

        file_s = directory + "/" + file_sess

        if os.path.exists(file_s) is False:
            f = open(file_s, 'w+')
        else:
            f = open(file_s, 'a+')

        previous_session = open(file_s, 'r').readlines()
        previous_session = [i.strip() for i in previous_session]
        print(file_s, len(previous_session), previous_session)

        if os.path.isdir(face_) is False:
            os.mkdir(face_)

        if os.path.isdir(directory) is True:
            images = os.listdir(directory)

            for each_pic in images:
                self.pin = (sorted([
                    int(n[:n.find(".")])
                    for n in os.listdir(face_) if n[0] != "."
                ] + [0])[-1] + 1)
                if (each_pic in previous_session) is True:
                    print("Skipping file", each_pic, "already scanned")
                    continue

                elif each_pic not in previous_session:
                    f.write(each_pic + "\n")

                    try:
                        image = cv2.imread(
                            os.path.abspath(directory + "/" + each_pic), 1)

                        normal = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                        normal = cv2.cvtColor(normal, cv2.COLOR_RGB2BGR)

                        if self.face_recognition.detection(image) is None:
                            faces = None

                        if self.face_recognition.detection(image) is not None:
                            faces, points = self.face_recognition.detection(
                                image)

                        if faces is not None:
                            for face in faces:
                                face_bb = face.bounding_box.astype(int)

                                yourface = normal[
                                    max(0, face_bb[1]
                                        ):min(face_bb[3], normal.shape[0] - 1),
                                    max(0, face_bb[0]
                                        ):min(face_bb[2], normal.shape[1] -
                                              1), ]

                                for i in range(points.shape[1]):
                                    pts = points[:, i].astype(np.int32)
                                    for j in range(pts.size // 2):
                                        pt = (pts[j], pts[5 + j])
                                        cv2.circle(
                                            image,
                                            center=pt,
                                            radius=1,
                                            color=(255, 0, 0),
                                            thickness=2,
                                        )

                                face_resize = cv2.resize(
                                    yourface, (self.im_width, self.im_height))

                                cv2.rectangle(
                                    image,
                                    (face_bb[0], face_bb[1]),
                                    (face_bb[2], face_bb[3]),
                                    (22, 20, 60),
                                    1,
                                )

                                cv2.imwrite("%s/%s.png" % (face_, self.pin),
                                            face_resize)

                                print(
                                    f"Face detected in image {each_pic} now creating face in {face_+'/'+str(self.pin)}.png"
                                )

                                face_pic += 1

                                cv2.destroyAllWindows()
                    except:
                        continue
                else:
                    f.write(each_pic + "\n")

        print(f"Process done. Created {face_pic} face files to {face_+'/'}")

    def check_for_porn(self, directory):

        profane_pic = 0
        file_sess = "profanity.check"
        print("Checking for nudity and pornographic material...", directory)
        directory = directory + "/images"

        profanity = directory + "/" + "Profanity"

        file_s = directory + "/" + file_sess

        if os.path.exists(file_s) is False:

            f = open(file_s, 'w+')
        else:
            f = open(file_s, 'a+')
        previous_session = open(file_s, 'r').readlines()
        previous_session = [i.strip() for i in previous_session]
        print(file_s, len(previous_session), previous_session)

        if os.path.isdir(profanity) is False:
            os.mkdir(profanity)

        if os.path.isdir(directory) is True:

            images = os.listdir(directory)

            for each_pic in images:

                if (each_pic in previous_session) is True:
                    print("Skipping file", each_pic, "already scanned")
                    continue

                elif each_pic not in previous_session:
                    f.write(each_pic + "\n")
                    preds = self.nude_classifier.classify(
                        os.path.abspath(directory + "/" + each_pic))

                    #print(preds, type(preds))
                    for (path, dicti) in preds.items():

                        for lvl, score in dicti.items():
                            if lvl is 'unsafe' and score >= 0.9:
                                print(
                                    f"Profanity detected ",
                                    f"Image ({each_pic}) MOVING to {profanity}..."
                                )
                                move(path, os.path.abspath(profanity))
                                profane_pic += 1

                            elif lvl is 'unsafe' and score >= .6 and score < 0.9:

                                print(
                                    f"Indecency detected ",
                                    f"Am not sure about Image ({each_pic}) should i move it to {profanity}..."
                                )

                                image = cv2.imread(path, 1)
                                width = 320
                                height = 320

                                dim = (width, height)
                                try:
                                    resized_img = cv2.resize(
                                        image, dim, cv2.INTER_AREA)
                                    cv2.imshow("SUGGESTION", resized_img)
                                except:
                                    print(e)
                                    continue
                                print(
                                    f" Move {path} Press(y) to move or n to continue?"
                                )
                                k = cv2.waitKey(0)

                                if (k == ord('y')):
                                    move(path, os.path.abspath(profanity))
                                    profane_pic += 1
                                    cv2.destroyAllWindows()
                                    print(
                                        f"Suggestion moved {path} to {profanity}"
                                    )
                                else:
                                    cv2.destroyAllWindows()
                                    continue

                    #print(preds)

                    #f.append(each_pic+"\n")
                else:

                    f.write(each_pic + "\n")
        print(
            f"Process done. Moved {profane_pic} profane files to {profanity}")

    def save_session(self, fls, prev, curr):

        #if(type(self.files) is not dict):

        #    print(f"Must be dictionary not {type(files)}")
        #    return

        if (len(fls) == 0):

            print("print data not provided its empty")
            return

        if (os.path.isdir(prev) == False):

            print("Please provide a valid directory ERROR : ", prev)

        if (os.path.isdir(curr) == False):

            print("Please provide a valid directory ERROR : ", curr)

        filename = "session.sess"
        full_filename = str(os.path.abspath(os.path.join(curr, filename)))
        #full_dr = new_directory + "/" + filename
        print(full_filename, self.dt_string, "string : ", fls)
        fn = "session_cp.sess"
        full_fn = str(os.path.abspath(os.path.join(curr, fn)))

        if os.path.exists(full_filename) is False:
            f = open(full_filename, 'w+', encoding="utf-8")
            fp = open(full_fn, 'w+', encoding="utf-8")
            fp.write(prev + "," + curr + "\n")
        else:
            f = open(full_filename, 'a+', encoding="utf-8")
            fp = open(full_fn, 'w+', encoding="utf-8")
            fp.write(prev + "," + curr + "\n")

        _checked_ = open(full_filename, 'r').readlines()
        _checked_ = [i.strip() for i in _checked_]

        for file, dr in self.all_files.items():
            x = file + "," + dr
            if (x in _checked_):
                print("Skipping ", x, "it has been added")
                continue
            elif x not in _checked_:

                f.write(x + "\n")
            else:
                f.write(x + "\n")

        f.close()
        fp.close()

        print("Successfully Saved session at ", self.dt_string)

    def sort_dir(self, directory):

        self.prev_dir = directory

        #given a dir, check if its a dir or not
        print("Starting sorting operation...", directory)
        if os.path.isdir(directory) == True:
            #print("Listing dir")
            dir_list = os.walk(directory)
            #print(dir_list)
            #print("Total size : ", len(dir_list))

            for (root, dirs, files) in dir_list:

                self.number_of_files = len(files)

                #print(files)

                i = 0

                print("files LEN ", self.number_of_files, " DIRS LEN ",
                      len(dirs), "root ", root, "dirs ", dirs, "files ", files)

                for direct in dirs:
                    full_dir = os.path.join(root, direct)

                    if os.path.isdir(full_dir) is True:
                        full_dir_files = os.listdir(full_dir)

                        self.file_structure[full_dir] = full_dir_files

                for extensions in self.master_ext:

                    for ext in extensions:

                        for file in files:

                            try:
                                EXT_ = file.split(".")[1]
                                EXT_ = "." + EXT_
                                if EXT_ == ext:
                                    #print("ROOT ", root, "DIRECTORIES : ", dirs, "FILES ", files)
                                    self.lst_of[os.path.join(
                                        root, file)] = self.type_s[i]
                                    print("TYPE : ", self.type_s[i],
                                          "FILE EXT FOUND : ", ext,
                                          "FILE NAME : ",
                                          os.path.join(root, file), "ITER : ",
                                          i, EXT_)

                                    #self.all_files.append(file)
                                    self.all_files[file] = os.path.join(
                                        root, file)
                            except:
                                print("ERROR WITH FILE : ", file)
                                continue

                    i += 1

            new_directory = input("Please enter name of new directory...")

            print("Available dirs please choose which dir to ", self.mode,
                  " to?")
            self.runt.join()
            for i in range(len(self.available_dirs)):
                print(i, self.available_dirs[i])

            choice = input("Please choose directory?")

            while True:

                if int(choice) < 0 or int(choice) > len(self.available_dirs):
                    print("Invalid choice!!!, Please choose again.")
                    choice = input("Please choose directory?")
                    continue
                else:
                    break

            new_directory = self.available_dirs[int(
                choice)] + new_directory + "_" + os.path.basename(directory)

            self.curr_dir = new_directory
            print(new_directory)
            print(self.lst_of)
            self.display(self.lst_of)
            if self.mode == 'move':
                self.move_(new_directory)
            else:
                self.copy_(new_directory)

            self.save_session(self.all_files, self.prev_dir, self.curr_dir)

            self.add_dir(new_directory)
            print("All files : ", self.all_files, " Previous Directory : ",
                  self.prev_dir, " Current Directory : ", self.curr_dir)

            choice = input(
                f"Operation {self.mode} is done, Are you sure you want to continue or Rollback the process Y - Yes(Rollback) / N - No(Continue)?"
            )
            choice = choice.lower()
            if "y" in choice or choice is "y" or choice is "yes" or "yes" in choice:
                #prev_dir = input("Please enter the previous directory where the files where located before operation")
                #curr_dir = input("Please enter the current directory where the files where are located now")
                self.rollback(self.all_files, self.prev_dir, self.curr_dir)

                print(
                    "Rollback operation successfully files where placed back to their original places."
                )

            threading.Thread(target=self.check_for_porn(new_directory)).start()

        else:

            print("Directory doesn't exist : ", directory,
                  "Please enter a valid directory!")

            path = input(
                "Please enter the directory or folder or file path you want to sort?"
            )
            srt.sort_dir(rf"{path}")

    mode = property(get_mode, set_mode, del_mode, "mode's docstring")
    lst_of = property(get_lst_of, set_lst_of, del_lst_of, "lst_of's docstring")
    doc_ext = property(get_doc_ext, set_doc_ext, del_doc_ext,
                       "doc_ext's docstring")
    img_ext = property(get_img_ext, set_img_ext, del_img_ext,
                       "img_ext's docstring")
    vid_ext = property(get_vid_ext, set_vid_ext, del_vid_ext,
                       "vid_ext's docstring")
    sound_ext = property(get_sound_ext, set_sound_ext, del_sound_ext,
                         "sound_ext's docstring")
    zip_ext = property(get_zip_ext, set_zip_ext, del_zip_ext,
                       "zip_ext's docstring")
    code_ext = property(get_code_ext, set_code_ext, del_code_ext,
                        "code_ext's docstring")
    media_ext = property(get_media_ext, set_media_ext, del_media_ext,
                         "media_ext's docstring")
    data_ext = property(get_data_ext, set_data_ext, del_data_ext,
                        "data_ext's docstring")
    app_ext = property(get_app_ext, set_app_ext, del_app_ext,
                       "app_ext's docstring")
    font_ext = property(get_font_ext, set_font_ext, del_font_ext,
                        "font_ext's docstring")
    sys_ext = property(get_sys_ext, set_sys_ext, del_sys_ext,
                       "sys_ext's docstring")
    flags = property(get_flags, set_flags, del_flags, "flags's docstring")
    specifics = property(get_specifics, set_specifics, del_specifics,
                         "specifics's docstring")
    all_files = property(get_all_files, set_all_files, del_all_files,
                         "all_files's docstring")
    errors = property(get_errors, set_errors, del_errors, "errors's docstring")
    file_structure = property(get_file_structure, set_file_structure,
                              del_file_structure, "file_structure's docstring")
    now = property(get_now, set_now, del_now, "now's docstring")
    dt_string = property(get_dt_string, set_dt_string, del_dt_string,
                         "dt_string's docstring")
    nude_classifier = property(get_nude_classifier, set_nude_classifier,
                               del_nude_classifier,
                               "nude_classifier's docstring")
    number_of_files = property(get_number_of_files, set_number_of_files,
                               del_number_of_files,
                               "number_of_files's docstring")
    time_taken = property(get_time_taken, set_time_taken, del_time_taken,
                          "time_taken's docstring")
    prev_dir = property(get_prev_dir, set_prev_dir, del_prev_dir,
                        "prev_dir's docstring")
    curr_dir = property(get_curr_dir, set_curr_dir, del_curr_dir,
                        "curr_dir's docstring")
    master_ext = property(get_master_ext, set_master_ext, del_master_ext,
                          "master_ext's docstring")
    type_s = property(get_type_s, set_type_s, del_type_s, "type_s's docstring")
Пример #22
0
# Import module
from nudenet import NudeClassifier

# initialize classifier (downloads the checkpoint file automatically the first time)
classifier = NudeClassifier()

# Classify single image
# dic = classifier.classify('test.jpg')
# print(dic)
# Returns {'path_to_image_1': {'safe': PROBABILITY, 'unsafe': PROBABILITY}}
# Classify multiple images (batch prediction)
# batch_size is optional; defaults to 4

# classifier.classify(['path_to_image_1', 'path_to_image_2'], batch_size=BATCH_SIZE)
# # Returns {'path_to_image_1': {'safe': PROBABILITY, 'unsafe': PROBABILITY},
# #          'path_to_image_2': {'safe': PROBABILITY, 'unsafe': PROBABILITY}}

# # Classify video
# # batch_size is optional; defaults to 4
dic = classifier.classify_video('p**n.mp4')
# print("\n\n\n")
# for key, value in dic['preds'].items():
#     print('프레임 {}의 결과 값은 {} 입니다'.format(key, value))

count = 0
num_frames = 0

for key, value in dic['preds'].items():
    if float(value['unsafe']) > 0.6:
        count += 1
    num_frames += 1
Пример #23
0
import os
import tensorflow as tf

tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

from nudenet import NudeClassifier

classifier = NudeClassifier()

result = classifier.classify('/nsfw/one.jpg')
result2 = classifier.classify('/nsfw/two.xjpg')

print('')
print('')
print('TEST RESULTS:')
print('********************************************')
print('')

print('one.jpg')
print(result)
print('')
print('two.xjpg')
print(result2)

print('')
print('********************************************')
print('')
def main(SUBREDDIT_NAMES):
    tempjpg = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                           'temp.jpg')
    classifier = NudeClassifier()
    valid_extensions = ['.jpg', '.jpeg', '.bmp', '.png', '.tiff']
    SUBREDDIT_NAMES = SUBREDDIT_NAMES.replace(',', '+').replace(' ', '')
    while True:
        con = sqlite3.connect('log.db')
        cur = con.cursor()
        try:

            for submission in reddit.subreddit(
                    SUBREDDIT_NAMES).stream.submissions():

                gallery = []
                URL = submission.url
                #add .jpg to image link if its an imgur link
                if 'imgur.com' in URL:
                    URL += '.jpg'
                    gallery.append(URL)
                #get inidividual images from gallery
                elif 'reddit.com/gallery' in URL:
                    ids = [
                        i['media_id'] for i in submission.gallery_data['items']
                    ]
                    for i in ids:
                        try:
                            url = submission.media_metadata[i]['p'][0]['u']
                            url = url.split("?")[0].replace("preview", "i")
                            gallery.append(url)
                        except KeyError:
                            pass
                #normal image url
                else:
                    gallery.append(URL)

                for i in gallery:
                    isimage = False
                    if i.endswith(tuple(valid_extensions)):
                        isimage = True
                    if isimage == True:
                        try:
                            #save image as temp file
                            with urllib.request.urlopen(i) as url:
                                with open(tempjpg, 'wb') as f:
                                    f.write(url.read())
                                    f.close()
                        except Exception as err:
                            print(err)

                        prediction = classifier.classify(
                            tempjpg)[tempjpg]['unsafe']
                        #remove post if REMOVE_SUBMISSION is True
                        if prediction > NSFW_PROB_THRESHOLD:
                            #print("nsfw")
                            if LOGGING_ON:
                                cur.execute(
                                    "INSERT INTO logbook VALUES (?,?,?)",
                                    (submission.created_utc,
                                     str(submission.author),
                                     submission.permalink))
                                con.commit()
                            if not MOD_TEST:
                                submission.mod.nsfw()
                                if REMOVE_SUBMISSION:
                                    submission.mod.remove()
                                    submission.mod.send_removal_message(
                                        REMOVAL_MESSAGE)
                            #send mod mail to mod discussions for testing
                            else:
                                submission.subreddit.message(
                                    "NSFW image detected!",
                                    "post: " + submission.permalink + ' p = ' +
                                    str(prediction) +
                                    ', threshold is currently ' +
                                    str(NSFW_PROB_THRESHOLD))
                            break
                        else:
                            #print("notnsfw")
                            pass

        except Exception as err:
            con.close()
            print(err)

    con.close()
Пример #25
0
class Nudity(commands.Cog):
    """Monitor images for NSFW content and moves them to a nsfw channel if possible"""

    def __init__(self, bot: Red):
        super().__init__()
        self.bot = bot
        self.config = Config.get_conf(self, identifier=9811198108111121, force_registration=True)

        default_guild = {"enabled": False, "channel_id": None}

        self.config.register_guild(**default_guild)

        # self.detector = NudeDetector()
        self.classifier = NudeClassifier()

        self.data_path: pathlib.Path = cog_data_path(self)

        self.current_processes = 0

    async def red_delete_data_for_user(self, **kwargs):
        """Nothing to delete"""
        return

    @commands.command(aliases=["togglenudity"], name="nudity")
    async def nudity(self, ctx: commands.Context):
        """Toggle nude-checking on or off"""
        is_on = await self.config.guild(ctx.guild).enabled()
        await self.config.guild(ctx.guild).enabled.set(not is_on)
        await ctx.send("Nude checking is now set to {}".format(not is_on))

    @commands.command()
    async def nsfwchannel(self, ctx: commands.Context, channel: discord.TextChannel = None):
        if channel is None:
            await self.config.guild(ctx.guild).channel_id.set(None)
            await ctx.send("NSFW Channel cleared")
        else:
            if not channel.is_nsfw():
                await ctx.send("This channel isn't NSFW!")
                return
            else:
                await self.config.guild(ctx.guild).channel_id.set(channel.id)
                await ctx.send("NSFW channel has been set to {}".format(channel.mention))

    async def get_nsfw_channel(self, guild: discord.Guild):
        channel_id = await self.config.guild(guild).channel_id()

        if channel_id is None:
            return None
        else:
            return guild.get_channel(channel_id=channel_id)

    async def nsfw(self, message: discord.Message, images: dict):
        content = message.content
        guild: discord.Guild = message.guild
        if not content:
            content = "*`None`*"
        try:
            await message.delete()
        except discord.Forbidden:
            await message.channel.send("NSFW Image detected!")
            return

        embed = discord.Embed(title="NSFW Image Detected")
        embed.add_field(name="Original Message", value=content)
        embed.set_author(name=message.author.name, icon_url=message.author.avatar_url)
        await message.channel.send(embed=embed)

        nsfw_channel = await self.get_nsfw_channel(guild)

        if nsfw_channel is None:
            return
        else:
            for image, r in images.items():
                if r["unsafe"] > 0.7:
                    await nsfw_channel.send(
                        "NSFW Image from {}".format(message.channel.mention),
                        file=discord.File(
                            image,
                        ),
                    )

    @commands.Cog.listener()
    async def on_message(self, message: discord.Message):
        is_private = isinstance(message.channel, discord.abc.PrivateChannel)

        if not message.attachments or is_private or message.author.bot:
            # print("did not qualify")
            return

        if await self.bot.cog_disabled_in_guild(self, message.guild):
            return

        try:
            is_on = await self.config.guild(message.guild).enabled()
        except AttributeError:
            return

        if not is_on:
            print("Not on")
            return

        channel: discord.TextChannel = message.channel

        if channel.is_nsfw():
            print("nsfw channel is okay")
            return

        check_list = []
        for attachment in message.attachments:
            # async with aiohttp.ClientSession() as session:
            #     img = await fetch_img(session, attachment.url)

            ext = attachment.filename

            temp_name = self.data_path / f"nudecheck{self.current_processes}_{ext}"

            self.current_processes += 1

            print("Pre attachment save")
            await attachment.save(temp_name)
            check_list.append(temp_name)

        print("Pre nude check")
        # nude_results = self.detector.detect(temp_name)
        nude_results = self.classifier.classify([str(n) for n in check_list])
        # print(nude_results)

        if True in [r["unsafe"] > 0.7 for r in nude_results.values()]:
            # print("Is nude")
            await message.add_reaction("❌")
            await self.nsfw(message, nude_results)
        else:
            # print("Is not nude")
            await message.add_reaction("✅")
Пример #26
0
from nudenet import NudeClassifier
import sys

classifier = NudeClassifier()
print(classifier.classify("images/%s" % sys.argv[1]))