File size: 2,864 Bytes
5cf44e7
 
 
 
da86b23
bb0d58b
 
59b75e7
bb0d58b
1b3cb87
c09c712
 
2ee0b24
c09c712
 
 
 
 
 
 
 
 
 
 
 
e34dea2
c09c712
2ee0b24
 
c09c712
 
bb0d58b
3296b6f
da86b23
 
 
 
 
2ee0b24
950f45e
2ee0b24
 
 
da86b23
 
 
 
 
d2b29d9
da86b23
2ee0b24
da86b23
 
 
 
 
 
 
2ee0b24
 
 
 
da86b23
f3ee0fd
bb0d58b
c09c712
 
 
751cc0c
c09c712
 
 
2ee0b24
c09c712
2709008
 
 
 
 
c09c712
 
 
 
 
2ee0b24
 
c09c712
da86b23
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import gradio as gr
import spaces
import torch

from ultralytics import YOLO
from PIL import Image
import supervision as sv
import numpy as np

@spaces.GPU
def yolov8_inference(
    image,
    selected_labels_list
):
    """
    YOLOv8 inference function
    Args:
        image: Input image
        model_path: Path to the model
        image_size: Image size
        conf_threshold: Confidence threshold
        iou_threshold: IOU threshold
    Returns:
        Rendered image
    """
    model = YOLO('erax_nsfw_v1.pt').to('cuda')
    # set model parameters
    model.overrides['conf'] = 0.3  # NMS confidence threshold
    model.overrides['iou'] = 0.2  # NMS IoU threshold
    model.overrides['agnostic_nms'] = False  # NMS class-agnostic
    model.overrides['max_det'] = 1000  # maximum number of detections per image

    results = model([image])
    for result in results:
        annotated_image = result.orig_img.copy()
        h, w = annotated_image.shape[:2]
        anchor = h if h > w else w
        
        # Create the dictionary by filtering list1 and list2 based on list3
        selected_classes = [[0, 1, 2, 3, 4][["anus", "make_love", "nipple", "penis", "vagina"].index(item)] for item in selected_labels_list]
        
        # print(filtered_mapping)
        # selected_classes = [0, 1, 2, 3, 4] # all classes
        detections = sv.Detections.from_ultralytics(result)
        detections = detections[np.isin(detections.class_id, selected_classes)]
           
        
        label_annotator = sv.LabelAnnotator(text_color=sv.Color.BLACK,
                                            text_position=sv.Position.CENTER,
                                            text_scale=anchor/1700)
    
        
        pixelate_annotator = sv.PixelateAnnotator(pixel_size=anchor/50)
        annotated_image = pixelate_annotator.annotate(
            scene=annotated_image.copy(),
            detections=detections
        )
        
        annotated_image = label_annotator.annotate(
            annotated_image,
            detections=detections
        )        
    
    return annotated_image[:, :, ::-1]


inputs = [
    gr.Image(type="filepath", label="Input Image"),
    gr.CheckboxGroup(["anus", "make_love", "nipple", "penis", "vagina"], label="Input Labels"),
]

outputs = gr.Image(type="filepath", label="Output Image")
title = "EraX NSFW V1.0 Models for NSFW detection"

examples = [
            ['demo/img_1.jpg', ["anus", "make_love", "nipple", "penis", "vagina"]], \
            ['demo/img_2.jpg', ["anus", "make_love", "nipple", "penis", "vagina"]], \
            ['demo/img_3.jpg', ["anus", "make_love", "nipple", "penis", "vagina"]]
           ]
demo_app = gr.Interface(
    fn=yolov8_inference,
    inputs=inputs,
    outputs=outputs,
    title=title,
    examples=examples,
    cache_examples=True,
)
demo_app.launch(debug=True)