leonelhs commited on
Commit
1f5b114
1 Parent(s): 8f59889

init space

Browse files
.gitattributes CHANGED
@@ -2,13 +2,11 @@
2
  *.arrow filter=lfs diff=lfs merge=lfs -text
3
  *.bin filter=lfs diff=lfs merge=lfs -text
4
  *.bz2 filter=lfs diff=lfs merge=lfs -text
5
- *.ckpt filter=lfs diff=lfs merge=lfs -text
6
  *.ftz filter=lfs diff=lfs merge=lfs -text
7
  *.gz filter=lfs diff=lfs merge=lfs -text
8
  *.h5 filter=lfs diff=lfs merge=lfs -text
9
  *.joblib filter=lfs diff=lfs merge=lfs -text
10
  *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
- *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
  *.model filter=lfs diff=lfs merge=lfs -text
13
  *.msgpack filter=lfs diff=lfs merge=lfs -text
14
  *.npy filter=lfs diff=lfs merge=lfs -text
@@ -16,16 +14,14 @@
16
  *.onnx filter=lfs diff=lfs merge=lfs -text
17
  *.ot filter=lfs diff=lfs merge=lfs -text
18
  *.parquet filter=lfs diff=lfs merge=lfs -text
19
- *.pb filter=lfs diff=lfs merge=lfs -text
20
  *.pickle filter=lfs diff=lfs merge=lfs -text
21
  *.pkl filter=lfs diff=lfs merge=lfs -text
 
22
  *.pt filter=lfs diff=lfs merge=lfs -text
23
  *.pth filter=lfs diff=lfs merge=lfs -text
24
  *.rar filter=lfs diff=lfs merge=lfs -text
25
- *.safetensors filter=lfs diff=lfs merge=lfs -text
26
  saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
  *.tar.* filter=lfs diff=lfs merge=lfs -text
28
- *.tar filter=lfs diff=lfs merge=lfs -text
29
  *.tflite filter=lfs diff=lfs merge=lfs -text
30
  *.tgz filter=lfs diff=lfs merge=lfs -text
31
  *.wasm filter=lfs diff=lfs merge=lfs -text
 
2
  *.arrow filter=lfs diff=lfs merge=lfs -text
3
  *.bin filter=lfs diff=lfs merge=lfs -text
4
  *.bz2 filter=lfs diff=lfs merge=lfs -text
 
5
  *.ftz filter=lfs diff=lfs merge=lfs -text
6
  *.gz filter=lfs diff=lfs merge=lfs -text
7
  *.h5 filter=lfs diff=lfs merge=lfs -text
8
  *.joblib filter=lfs diff=lfs merge=lfs -text
9
  *.lfs.* filter=lfs diff=lfs merge=lfs -text
 
10
  *.model filter=lfs diff=lfs merge=lfs -text
11
  *.msgpack filter=lfs diff=lfs merge=lfs -text
12
  *.npy filter=lfs diff=lfs merge=lfs -text
 
14
  *.onnx filter=lfs diff=lfs merge=lfs -text
15
  *.ot filter=lfs diff=lfs merge=lfs -text
16
  *.parquet filter=lfs diff=lfs merge=lfs -text
 
17
  *.pickle filter=lfs diff=lfs merge=lfs -text
18
  *.pkl filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
  *.pt filter=lfs diff=lfs merge=lfs -text
21
  *.pth filter=lfs diff=lfs merge=lfs -text
22
  *.rar filter=lfs diff=lfs merge=lfs -text
 
23
  saved_model/**/* filter=lfs diff=lfs merge=lfs -text
24
  *.tar.* filter=lfs diff=lfs merge=lfs -text
 
25
  *.tflite filter=lfs diff=lfs merge=lfs -text
26
  *.tgz filter=lfs diff=lfs merge=lfs -text
27
  *.wasm filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # poetry
98
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102
+ #poetry.lock
103
+
104
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
105
+ __pypackages__/
106
+
107
+ # Celery stuff
108
+ celerybeat-schedule
109
+ celerybeat.pid
110
+
111
+ # SageMath parsed files
112
+ *.sage.py
113
+
114
+ # Environments
115
+ .env
116
+ .venv
117
+ env/
118
+ venv/
119
+ ENV/
120
+ env.bak/
121
+ venv.bak/
122
+
123
+ # Spyder project settings
124
+ .spyderproject
125
+ .spyproject
126
+
127
+ # Rope project settings
128
+ .ropeproject
129
+
130
+ # mkdocs documentation
131
+ /site
132
+
133
+ # mypy
134
+ .mypy_cache/
135
+ .dmypy.json
136
+ dmypy.json
137
+
138
+ # Pyre type checker
139
+ .pyre/
140
+
141
+ # pytype static type analyzer
142
+ .pytype/
143
+
144
+ # Cython debug symbols
145
+ cython_debug/
146
+
147
+ # PyCharm
148
+ .idea/
149
+ examples/
150
+ task_make
151
+ task_upload
152
+ setup.py
153
+
154
+
app.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ from remove_background import app
2
+
3
+ app.launch(share=False, debug=True, enable_queue=True, show_error=True)
remove_background/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ from .interface import app
remove_background/__main__.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
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 ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ onnx
2
+ onnxruntime-gpu
3
+ opencv-python
4
+ numpy
5
+ pillow~=9.5.0