File size: 2,864 Bytes
0deb3fa
 
 
 
 
 
aa50dd4
1f5b114
0deb3fa
 
 
 
 
 
aa50dd4
 
 
 
 
0deb3fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa50dd4
 
 
0deb3fa
aa50dd4
0deb3fa
 
aa50dd4
0deb3fa
aa50dd4
0deb3fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dccc7ae
 
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
import PIL.Image
import gradio as gr
import huggingface_hub
import onnxruntime as rt
import numpy as np
import cv2
from PIL import ImageOps

providers = ['CUDAExecutionProvider', 'CPUExecutionProvider']
model_path = huggingface_hub.hf_hub_download("skytnt/anime-seg", "isnetis.onnx")
rmbg_model = rt.InferenceSession(model_path, providers=providers)


def custom_background(background, foreground):
    foreground = ImageOps.contain(foreground, background.size)
    x = (background.size[0] - foreground.size[0]) // 2
    y = (background.size[1] - foreground.size[1]) // 2
    background.paste(foreground, (x, y), foreground)
    return background


def get_mask(img, s=1024):
    img = (img / 255).astype(np.float32)
    h, w = h0, w0 = img.shape[:-1]
    h, w = (s, int(s * w / h)) if h > w else (int(s * h / w), s)
    ph, pw = s - h, s - w
    img_input = np.zeros([s, s, 3], dtype=np.float32)
    img_input[ph // 2:ph // 2 + h, pw // 2:pw // 2 + w] = cv2.resize(img, (w, h))
    img_input = np.transpose(img_input, (2, 0, 1))
    img_input = img_input[np.newaxis, :]
    mask = rmbg_model.run(None, {'img': img_input})[0][0]
    mask = np.transpose(mask, (1, 2, 0))
    mask = mask[ph // 2:ph // 2 + h, pw // 2:pw // 2 + w]
    mask = cv2.resize(mask, (w0, h0))[:, :, np.newaxis]
    return mask


def predict(image, new_background):
    mask = get_mask(image)
    image = (mask * image + 255 * (1 - mask)).astype(np.uint8)
    mask = (mask * 255).astype(np.uint8)
    image = np.concatenate([image, mask], axis=2, dtype=np.uint8)
    mask = mask.repeat(3, axis=2)
    if new_background is not None:
        foreground = PIL.Image.fromarray(image)
        return mask, custom_background(new_background, foreground)
    return mask, image


footer = r"""
<center>
<b>
Demo based on <a href='https://github.com/SkyTNT/anime-segmentation'>SkyTNT Anime Segmentation</a>
</b>
</center>
"""

with gr.Blocks(title="Face Shine") as app:
    gr.HTML("<center><h1>Anime Remove Background</h1></center>")
    with gr.Row():
        with gr.Column():
            input_img = gr.Image(type="numpy", label="Input image")
            new_img = gr.Image(type="pil", label="Custom background")
            run_btn = gr.Button(variant="primary")
        with gr.Column():
            with gr.Accordion(label="Image mask", open=False):
                output_mask = gr.Image(label="mask")
            output_img = gr.Image(type="pil", label="result")

    run_btn.click(predict, [input_img, new_img], [output_mask, output_img])

    with gr.Row():
        examples_data = [[f"examples/{x:02d}.jpg"] for x in range(1, 4)]
        examples = gr.Dataset(components=[input_img], samples=examples_data)
        examples.click(lambda x: x[0], [examples], [input_img])

    with gr.Row():
        gr.HTML(footer)

app.launch(share=False, debug=True, enable_queue=True, show_error=True)