leonelhs commited on
Commit
8318e85
1 Parent(s): dccc7ae

fix predict

Browse files
remove_background/__init__.py DELETED
@@ -1 +0,0 @@
1
- from .interface import app
 
 
remove_background/__main__.py DELETED
@@ -1,9 +0,0 @@
1
- from remove_background import app
2
-
3
-
4
- def main():
5
- app.queue().launch(share=True, debug=True)
6
-
7
-
8
- if __name__ == "__main__":
9
- main()
 
 
 
 
 
 
 
 
 
 
remove_background/interface.py DELETED
@@ -1,81 +0,0 @@
1
- import PIL.Image
2
- import gradio as gr
3
- import huggingface_hub
4
- import onnxruntime as rt
5
- import numpy as np
6
- import cv2
7
-
8
- providers = ['CUDAExecutionProvider', 'CPUExecutionProvider']
9
- model_path = huggingface_hub.hf_hub_download("skytnt/anime-seg", "isnetis.onnx")
10
- rmbg_model = rt.InferenceSession(model_path, providers=providers)
11
-
12
-
13
- def custom_background(background, foreground):
14
- x = (background.size[0] - foreground.size[0]) / 2
15
- y = (background.size[1] - foreground.size[1]) / 2
16
- box = (x, y, foreground.size[0] + x, foreground.size[1] + y)
17
- crop = background.crop(box)
18
- final_image = crop.copy()
19
- # put the foreground in the centre of the background
20
- paste_box = (0, final_image.size[1] - foreground.size[1], final_image.size[0], final_image.size[1])
21
- final_image.paste(foreground, paste_box, mask=foreground)
22
- return final_image
23
-
24
-
25
- def get_mask(img, s=1024):
26
- img = (img / 255).astype(np.float32)
27
- h, w = h0, w0 = img.shape[:-1]
28
- h, w = (s, int(s * w / h)) if h > w else (int(s * h / w), s)
29
- ph, pw = s - h, s - w
30
- img_input = np.zeros([s, s, 3], dtype=np.float32)
31
- img_input[ph // 2:ph // 2 + h, pw // 2:pw // 2 + w] = cv2.resize(img, (w, h))
32
- img_input = np.transpose(img_input, (2, 0, 1))
33
- img_input = img_input[np.newaxis, :]
34
- mask = rmbg_model.run(None, {'img': img_input})[0][0]
35
- mask = np.transpose(mask, (1, 2, 0))
36
- mask = mask[ph // 2:ph // 2 + h, pw // 2:pw // 2 + w]
37
- mask = cv2.resize(mask, (w0, h0))[:, :, np.newaxis]
38
- return mask
39
-
40
-
41
- def predict(img, new_background):
42
- mask = get_mask(img)
43
- img = (mask * img + 255 * (1 - mask)).astype(np.uint8)
44
- mask = (mask * 255).astype(np.uint8)
45
- img = np.concatenate([img, mask], axis=2, dtype=np.uint8)
46
- mask = mask.repeat(3, axis=2)
47
- if new_background is not None:
48
- foreground = PIL.Image.fromarray(img)
49
- return mask, custom_background(new_background, foreground)
50
- return mask, img
51
-
52
-
53
- footer = r"""
54
- <center>
55
- <b>
56
- Demo based on <a href='https://github.com/SkyTNT/anime-segmentation'>SkyTNT Anime Segmentation</a>
57
- </b>
58
- </center>
59
- """
60
-
61
- with gr.Blocks(title="Face Shine") as app:
62
- gr.HTML("<center><h1>Anime Remove Background</h1></center>")
63
- with gr.Row():
64
- with gr.Column():
65
- input_img = gr.Image(type="numpy", label="Input image")
66
- new_img = gr.Image(type="pil", label="Custom background")
67
- run_btn = gr.Button(variant="primary")
68
- with gr.Column():
69
- with gr.Accordion(label="Image mask", open=False):
70
- output_mask = gr.Image(label="mask")
71
- output_img = gr.Image(type="pil", label="result")
72
-
73
- run_btn.click(predict, [input_img, new_img], [output_mask, output_img])
74
-
75
- with gr.Row():
76
- examples_data = [[f"examples/{x:02d}.jpg"] for x in range(1, 4)]
77
- examples = gr.Dataset(components=[input_img], samples=examples_data)
78
- examples.click(lambda x: x[0], [examples], [input_img])
79
-
80
- with gr.Row():
81
- gr.HTML(footer)