Spaces:
Runtime error
Runtime error
Nik Ska
commited on
Commit
β’
7b073a2
1
Parent(s):
f384034
initial commit
Browse files- .DS_Store +0 -0
- README.md +5 -4
- app.py +77 -0
- examples/01.jpg +0 -0
- examples/02.jpg +0 -0
- examples/03.jpg +0 -0
- requirements.txt +5 -0
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
README.md
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.35.2
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: ARKA Remove BG
|
3 |
+
emoji: π
|
4 |
+
colorFrom: purple
|
5 |
+
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.35.2
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
license: mit
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
from PIL import ImageOps
|
8 |
+
|
9 |
+
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider']
|
10 |
+
model_path = huggingface_hub.hf_hub_download(
|
11 |
+
"skytnt/anime-seg", "isnetis.onnx")
|
12 |
+
rmbg_model = rt.InferenceSession(model_path, providers=providers)
|
13 |
+
|
14 |
+
|
15 |
+
def custom_background(background, foreground):
|
16 |
+
foreground = ImageOps.contain(foreground, background.size)
|
17 |
+
x = (background.size[0] - foreground.size[0]) // 2
|
18 |
+
y = (background.size[1] - foreground.size[1]) // 2
|
19 |
+
background.paste(foreground, (x, y), foreground)
|
20 |
+
return background
|
21 |
+
|
22 |
+
|
23 |
+
def get_mask(img, s=1024):
|
24 |
+
img = (img / 255).astype(np.float32)
|
25 |
+
h, w = h0, w0 = img.shape[:-1]
|
26 |
+
h, w = (s, int(s * w / h)) if h > w else (int(s * h / w), s)
|
27 |
+
ph, pw = s - h, s - w
|
28 |
+
img_input = np.zeros([s, s, 3], dtype=np.float32)
|
29 |
+
img_input[ph // 2:ph // 2 + h, pw //
|
30 |
+
2:pw // 2 + w] = cv2.resize(img, (w, h))
|
31 |
+
img_input = np.transpose(img_input, (2, 0, 1))
|
32 |
+
img_input = img_input[np.newaxis, :]
|
33 |
+
mask = rmbg_model.run(None, {'img': img_input})[0][0]
|
34 |
+
mask = np.transpose(mask, (1, 2, 0))
|
35 |
+
mask = mask[ph // 2:ph // 2 + h, pw // 2:pw // 2 + w]
|
36 |
+
mask = cv2.resize(mask, (w0, h0))[:, :, np.newaxis]
|
37 |
+
return mask
|
38 |
+
|
39 |
+
|
40 |
+
def predict(image, new_background):
|
41 |
+
mask = get_mask(image)
|
42 |
+
image = (mask * image + 255 * (1 - mask)).astype(np.uint8)
|
43 |
+
mask = (mask * 255).astype(np.uint8)
|
44 |
+
image = np.concatenate([image, mask], axis=2, dtype=np.uint8)
|
45 |
+
mask = mask.repeat(3, axis=2)
|
46 |
+
if new_background is not None:
|
47 |
+
foreground = PIL.Image.fromarray(image)
|
48 |
+
return mask, custom_background(new_background, foreground)
|
49 |
+
return mask, image
|
50 |
+
|
51 |
+
|
52 |
+
footer = r"""
|
53 |
+
<center>
|
54 |
+
<b>
|
55 |
+
DEMO FOR BG REMOVAL
|
56 |
+
</b>
|
57 |
+
</center>
|
58 |
+
"""
|
59 |
+
|
60 |
+
with gr.Blocks(title="Face Shine") as app:
|
61 |
+
gr.HTML("<center><h1>ARKA Remove Background</h1></center>")
|
62 |
+
with gr.Row():
|
63 |
+
with gr.Column():
|
64 |
+
input_img = gr.Image(type="numpy", label="Input image")
|
65 |
+
new_img = gr.Image(type="pil", label="Custom background")
|
66 |
+
run_btn = gr.Button(variant="primary")
|
67 |
+
with gr.Column():
|
68 |
+
with gr.Accordion(label="Image mask", open=False):
|
69 |
+
output_mask = gr.Image(label="mask")
|
70 |
+
output_img = gr.Image(type="pil", label="result")
|
71 |
+
|
72 |
+
run_btn.click(predict, [input_img, new_img], [output_mask, output_img])
|
73 |
+
|
74 |
+
with gr.Row():
|
75 |
+
gr.HTML(footer)
|
76 |
+
|
77 |
+
app.launch(share=False, debug=True, enable_queue=True, show_error=True)
|
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 |
+
pillow~=9.5.0
|