File size: 3,499 Bytes
5cb1539
3a6f1f2
 
3015442
 
 
3a6f1f2
676b5d6
3a6f1f2
 
 
 
 
 
 
 
e332358
99c661e
 
 
bed615f
 
 
 
 
3015442
99c661e
782da61
e332358
782da61
3015442
 
4b14a38
bed615f
 
 
3a6f1f2
e332358
bed615f
ef928a1
 
df195bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
782da61
 
 
 
df195bf
dc60c06
782da61
dc60c06
ef928a1
bed615f
7707e77
3a6f1f2
 
bed615f
dc60c06
3a6f1f2
e332358
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
import gradio as gr
import os
import cv2
from rembg import new_session, remove
from PIL import Image
from io import BytesIO

def inference(file, mask, model, alpha_influence, segmentation_strength, smoothing):
    im = cv2.imread(file, cv2.IMREAD_COLOR)
    cv2.imwrite(os.path.join("input.png"), im)

    input_path = 'input.png'
    output_path = 'output.png'

    with open(input_path, 'rb') as i:
        with open(output_path, 'wb') as o:
            input = i.read()
            output = remove(
                input, 
                only_mask=(True if mask == "Mask only" else False),
                alpha_matting=True,  # Habilitar el modo alpha matting
                alpha_matting_foreground_threshold=alpha_influence,  # Control de influencia del canal alfa
                alpha_matting_background_threshold=1 - alpha_influence,  # Control del canal alfa para el fondo
                alpha_matting_erode_size=int(segmentation_strength * 10),  # Control de fuerza de segmentación
                alpha_matting_smoothing=smoothing,  # Control de suavizado de bordes de la segmentación
                session=new_session(model)
            )

            o.write(output)

    return Image.open(BytesIO(output))

title = "Background Using RemBG"
description = "<a href='https://www.buymeacoffee.com/diego2554' target='_blank'>Help me improve my computer equipment, I need RTX 4070 :)</a>Gradio demo for RemBG. erase the background of any image, To use it, simply upload your image and adjust the sliders and choose a eraser plugin from the U2net library. / <a href='https://huggingface.co/spaces/KenjieDec/RemBG' target='_blank'>Original article made by KenjieDec</a> / <a href='https://github.com/danielgatis/rembg' target='_blank'>Github Repo</a></p>"
article = "<p style='text-align: center;'><a href='https://github.com/danielgatis/rembg' target='_blank'>Github Repo</a></p>"
article = "<p style='text-align: center;'><a href='https://huggingface.co/spaces/KenjieDec/RemBG' target='_blank'>Model on Hugging Face</a></p>"

gr.Interface(
    inference, 
    [
        gr.inputs.Image(type="filepath", label="Input"),
        gr.inputs.Radio(
            [
                "Default", 
                "Mask only"
            ], 
            type="value",
            default="Default",
            label="Choices"
        ),
        gr.inputs.Dropdown([
            "u2net", 
            "u2netp", 
            "u2net_human_seg", 
            "u2net_cloth_seg", 
            "silueta",
            "isnet-general-use",
            "isnet-anime",
            "sam",
        ], 
        type="value",
        default="isnet-general-use",
        label="Models"
        ),
        gr.inputs.Slider(minimum=0.5, maximum=1.5, default=1, label="Alpha Influence"),
        gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.5, label="Segmentation Strength"),
        gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, label="Smoothing"),
    ], 
    gr.outputs.Image(type="PIL", label="Output"),
    #description = "<a href='https://huggingface.co/spaces/KenjieDec/RemBG' target='_blank'>Original article made by KenjieDec</a><a href='https://github.com/danielgatis/rembg' target='_blank'>Github Repo</a></p>"
    title=title,
    description=description,
    article=article,
    examples=[["lion.png", "Default", "u2net", 1, 0.5, 0.25], ["girl.jpg", "Default", "u2net", 1, 0.5, 0.25], ["anime-girl.jpg", "Default", "isnet-anime", 1, 0.5, 0.25]],
    enable_queue=True
).launch()