skytnt commited on
Commit
11f0a1f
1 Parent(s): 896ea5c
Files changed (7) hide show
  1. .gitignore +148 -0
  2. README.md +1 -1
  3. app.py +52 -0
  4. examples/01.jpg +0 -0
  5. examples/02.jpg +0 -0
  6. examples/03.jpg +0 -0
  7. requirements.txt +5 -0
.gitignore ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: Anime Remove Background
3
- emoji: 🐢
4
  colorFrom: indigo
5
  colorTo: pink
6
  sdk: gradio
 
1
  ---
2
  title: Anime Remove Background
3
+ emoji: 🪄🖼️
4
  colorFrom: indigo
5
  colorTo: pink
6
  sdk: gradio
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import huggingface_hub
3
+ import onnxruntime as rt
4
+ import numpy as np
5
+ import cv2
6
+
7
+
8
+ def get_mask(img, s=1024):
9
+ img = (img / 255).astype(np.float32)
10
+ h, w = h0, w0 = img.shape[:-1]
11
+ h, w = (s, int(s * w / h)) if h > w else (int(s * h / w), s)
12
+ ph, pw = s - h, s - w
13
+ img_input = np.zeros([s, s, 3], dtype=np.float32)
14
+ img_input[ph // 2:ph // 2 + h, pw // 2:pw // 2 + w] = cv2.resize(img, (w, h), interpolation=cv2.INTER_CUBIC)
15
+ img_input = np.transpose(img_input, (2, 0, 1))
16
+ img_input = img_input[np.newaxis, :]
17
+ mask = rmbg_model.run(None, {'img': img_input})[0][0]
18
+ mask = np.transpose(mask, (1, 2, 0))
19
+ mask = mask[ph // 2:ph // 2 + h, pw // 2:pw // 2 + w]
20
+ mask = cv2.resize(mask, (w0, h0), interpolation=cv2.INTER_CUBIC)[:, :, np.newaxis]
21
+ return mask
22
+
23
+
24
+ def rmbg_fn(img):
25
+ mask = get_mask(img)
26
+ img = (mask * img + 255 * (1 - mask)).astype(np.uint8)
27
+ mask = (mask * 255).astype(np.uint8)
28
+ img = np.concatenate([img, mask], axis=2, dtype=np.uint8)
29
+ mask = mask.repeat(3, axis=2)
30
+ return mask, img
31
+
32
+
33
+ if __name__ == "__main__":
34
+ providers = ['CUDAExecutionProvider', 'CPUExecutionProvider']
35
+ model_path = huggingface_hub.hf_hub_download("skytnt/anime-seg", "isnetis.onnx")
36
+ rmbg_model = rt.InferenceSession(model_path, providers=providers)
37
+ app = gr.Blocks()
38
+ with app:
39
+ gr.Markdown("# Anime Remove Background\n\n"
40
+ "![visitor badge](https://visitor-badge.glitch.me/badge?page_id=skytnt.animeseg)\n\n"
41
+ "demo for [https://github.com/SkyTNT/anime-segmentation/](https://github.com/SkyTNT/anime-segmentation/)")
42
+ with gr.Row():
43
+ with gr.Column():
44
+ input_img = gr.Image(label="input image")
45
+ examples_data = [[f"examples/{x:02d}.jpg"] for x in range(1, 4)]
46
+ examples = gr.Dataset(components=[input_img], samples=examples_data)
47
+ run_btn = gr.Button(variant="primary")
48
+ output_mask = gr.Image(label="mask")
49
+ output_img = gr.Image(label="result", image_mode="RGBA")
50
+ examples.click(lambda x: x[0], [examples], [input_img])
51
+ run_btn.click(rmbg_fn, [input_img], [output_mask, output_img])
52
+ app.launch()
examples/01.jpg ADDED
examples/02.jpg ADDED
examples/03.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ onnx
2
+ onnxruntime-gpu
3
+ opencv-python
4
+ numpy
5
+ gradio