File size: 1,622 Bytes
f2ecde5
dbdec24
 
 
 
 
 
951a2e6
dbdec24
f2ecde5
e8e4ed5
dbdec24
 
 
 
 
63c257d
 
 
 
 
 
dbdec24
 
 
 
 
eb4bba7
 
 
 
 
 
dbdec24
 
 
 
e1ac1c5
dbdec24
 
317c2b3
eb4bba7
dbdec24
63d4d27
 
 
 
 
 
 
dbdec24
 
 
 
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
import spaces
import gradio as gr
from PIL import Image
from transparent_background import Remover
import numpy as np

# Initialize the model globally
remover = Remover(jit=False)

@spaces.GPU
def process_image(input_image, output_type):
    global remover
    
    if output_type == "Mask only":
        # Process the image and get only the mask
        output = remover.process(input_image, type='map')
        if isinstance(output, Image.Image):
            # If output is already a PIL Image, convert to grayscale
            mask = output.convert('L')
        else:
            # If output is a numpy array, convert to PIL Image
            mask = Image.fromarray((output * 255).astype(np.uint8), mode='L')
        return mask
    else:
        # Process the image and return the RGBA result
        output = remover.process(input_image, type='rgba')
        return output
        
description = """<h1 align="center">InSPyReNet Background Remover</h1>
<p><center>
<a href="https://github.com/plemeri/InSPyReNet" target="_blank">[Github]</a>
</center></p>
"""

iface = gr.Interface(
    fn=process_image,
    inputs=[
        gr.Image(type="pil", label="Input Image", height=512),
        gr.Radio(["Default", "Mask only"], label="Output Type", value="Default")
    ],
    outputs=gr.Image(type="pil", label="Output Image", height=512),
    description=description,
    theme='bethecloud/storj_theme',
    examples=[
        ["1.png", "Default"],
        ["2.png", "Default"],
        ["3.jfif", "Default"],
        ["4.webp", "Default"]
    ],
    cache_examples=True
)

if __name__ == "__main__":
    iface.launch()