File size: 1,528 Bytes
8637ba8
 
7955acb
ed1578c
7955acb
ed1578c
7955acb
 
 
4ae29be
7955acb
b22fdc9
ed1578c
 
b22fdc9
ed1578c
 
3d05a69
ed1578c
7955acb
 
 
 
8637ba8
 
 
7955acb
 
 
4ae29be
 
 
7955acb
 
ed1578c
7955acb
4e4209c
62468d4
b74c088
8637ba8
ec18cc1
be1eb30
8637ba8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import gradio as gr
from nudenet import NudeDetector
import cv2
import numpy as np

def circular_blur(image_path, detections, parts_to_blur):
    image = cv2.imread(image_path)
    
    for detection in detections:
        label = detection['class']
        if label in parts_to_blur:
            x, y, width, height = map(int, detection['box'])
            center_x, center_y = x + width // 2, y + height // 2
            radius = int(min(width, height) / 2)

            mask = np.zeros_like(image)
            cv2.circle(mask, (center_x, center_y), radius, (255, 255, 255), -1)
            blurred_image = cv2.GaussianBlur(image, (51, 51), 50)
            image = np.where(mask == 255, blurred_image, image)
    
    blurred_image_path = 'blurred_' + image_path.split('/')[-1]
    cv2.imwrite(blurred_image_path, image)
    return blurred_image_path

def process(input_img):
    detector = NudeDetector(model_path="640m.onnx", inference_resolution=640)
    detections = detector.detect(input_img)
    print(detections)
    parts_to_blur = [
        'FEMALE_GENITALIA_EXPOSED', 'MALE_GENITALIA_EXPOSED', 
        'FEMALE_BREAST_EXPOSED', 'BUTTOCKS_EXPOSED',
        'MALE_BREAST_EXPOSED', 'ANUS_EXPOSED'
    ]
    
    blurred_image_path = circular_blur(input_img, detections, parts_to_blur)
    return blurred_image_path
    
title = "Nudity Censor"
theme = "Nymbo/Alyx_Theme"

iface = gr.Interface(process, gr.components.Image(type='filepath'), gr.components.Image(type="filepath"), title=title, theme=theme)

iface.launch()