How to use NudeNet - 8 common examples

To help you get started, we’ve selected a few NudeNet examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bedapudi6788 / NudeNet / nudenet / classifier.py View on Github external
images_preds = {}
        
        for i, loaded_image_path in enumerate(loaded_image_paths):
            images_preds[loaded_image_path] = {}
            for _ in range(len(preds[i])):
                images_preds[loaded_image_path][preds[i][_]] = probs[i][_]

        return images_preds


if __name__ == '__main__':
    print('\n Enter path for the keras weights, leave empty to use "./nsfw.299x299.h5" \n')
    weights_path = input().strip()
    if not weights_path: weights_path = "../nsfw.299x299.h5"
    
    m = Classifier(weights_path)

    while 1:
        print('\n Enter single image path or multiple images seperated by || (2 pipes) \n')
        images = input().split('||')
        images = [image.strip() for image in images]
        print(m.predict(images), '\n')
github bedapudi6788 / NudeNet / nudenet / detector.py View on Github external
def detect(self, img_path, min_prob=0.6):
        image = read_image_bgr(img_path)
        image = preprocess_image(image)
        image, scale = resize_image(image)
        boxes, scores, labels = Detector.detection_model.predict_on_batch(np.expand_dims(image, axis=0))
        boxes /= scale
        processed_boxes = []
        for box, score, label in zip(boxes[0], scores[0], labels[0]):
            if score < min_prob:
                continue
            box = box.astype(int).tolist()
            label = Detector.classes[label]
            processed_boxes.append({'box': box, 'score': score, 'label': label})
            
        return processed_boxes
github bedapudi6788 / NudeNet / nudenet / detector.py View on Github external
for box in boxes:
            part = image[box[1]:box[3], box[0]:box[2]]
            image = cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), (0, 0, 0), cv2.FILLED)
            # image = cv2.GaussianBlur(part,(23, 23), 30)
            # image[box[1]:box[3], box[0]:box[2]] = part
        
        if visualize:
            cv2.imshow("Blurred image", image)
            cv2.waitKey(0)
        
        if out_path:
            cv2.imwrite(out_path, image)


if __name__ == '__main__':
    m = Detector('/Users/bedapudi/Desktop/inference_resnet50_csv_14.h5')
    print(m.censor('/Users/bedapudi/Desktop/n2.jpg', out_path='a.jpg'))
github bedapudi6788 / NudeNet / nudenet / detector.py View on Github external
def detect(self, img_path, min_prob=0.6):
        image = read_image_bgr(img_path)
        image = preprocess_image(image)
        image, scale = resize_image(image)
        boxes, scores, labels = Detector.detection_model.predict_on_batch(np.expand_dims(image, axis=0))
        boxes /= scale
        processed_boxes = []
        for box, score, label in zip(boxes[0], scores[0], labels[0]):
            if score < min_prob:
                continue
            box = box.astype(int).tolist()
            label = Detector.classes[label]
            processed_boxes.append({'box': box, 'score': score, 'label': label})
            
        return processed_boxes
github bedapudi6788 / NudeNet / nudenet / detector.py View on Github external
def censor(self, img_path, out_path=None, visualize=True, parts_to_blur=['BELLY', 'BUTTOCKS', 'F_BREAST', 'F_GENITALIA', 'M_GENETALIA', 'M_BREAST']):
        if not out_path and not visualize:
            print('No out_path passed and visualize is set to false. There is no point in running this function then.')

        image = cv2.imread(img_path)
        boxes = Detector.detect(self, img_path)
        boxes = [i['box'] for i in boxes if i['label'] in parts_to_blur]

        for box in boxes:
            part = image[box[1]:box[3], box[0]:box[2]]
            image = cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), (0, 0, 0), cv2.FILLED)
            # image = cv2.GaussianBlur(part,(23, 23), 30)
            # image[box[1]:box[3], box[0]:box[2]] = part
        
        if visualize:
            cv2.imshow("Blurred image", image)
            cv2.waitKey(0)
        
        if out_path:
            cv2.imwrite(out_path, image)
github bedapudi6788 / NudeNet / nudenet / detector.py View on Github external
'''
            model = Detector()
        '''
        url = 'https://github.com/bedapudi6788/NudeNet/releases/download/v0/detector_model'
        home = os.path.expanduser("~")
        model_folder = os.path.join(home, '.NudeNet/')
        if not os.path.exists(model_folder):
            os.mkdir(model_folder)
        
        model_path = os.path.join(model_folder, 'detector')

        if not os.path.exists(model_path):
            print('Downloading the checkpoint to', model_path)
            pydload.dload(url, save_to_path=model_path, max_time=None)

        Detector.detection_model = models.load_model(model_path, backbone_name='resnet101')
github bedapudi6788 / NudeNet / nudenet / classifier.py View on Github external
'''
            model = Classifier()
        '''
        url = 'https://github.com/bedapudi6788/NudeNet/releases/download/v0/classifier_model'
        home = os.path.expanduser("~")
        model_folder = os.path.join(home, '.NudeNet/')
        if not os.path.exists(model_folder):
            os.mkdir(model_folder)
        
        model_path = os.path.join(model_folder, 'classifier')

        if not os.path.exists(model_path):
            print('Downloading the checkpoint to', model_path)
            pydload.dload(url, save_to_path=model_path, max_time=None)

        Classifier.nsfw_model = keras.models.load_model(model_path)
github bedapudi6788 / NudeNet / nudenet / classifier.py View on Github external
'''
            inputs:
                image_paths: list of image paths or can be a string too (for single image)
                batch_size: batch_size for running predictions
                image_size: size to which the image needs to be resized
                categories: since the model predicts numbers, categories is the list of actual names of categories
        '''
        if isinstance(image_paths, str):
            image_paths = [image_paths]

        loaded_images, loaded_image_paths = load_images(image_paths, image_size)
        
        if not loaded_image_paths:
            return {}

        model_preds = Classifier.nsfw_model.predict(loaded_images, batch_size = batch_size)

        preds = np.argsort(model_preds, axis = 1).tolist()

        probs = []
        for i, single_preds in enumerate(preds):
            single_probs = []
            for j, pred in enumerate(single_preds):
                single_probs.append(model_preds[i][pred])
                preds[i][j] = categories[pred]
            
            probs.append(single_probs)

        
        images_preds = {}
        
        for i, loaded_image_path in enumerate(loaded_image_paths):