Spaces:
Running
Running
thr3a
commited on
Commit
•
0b464ee
1
Parent(s):
bad4452
add: 白背景モード実装
Browse files- README.md +1 -1
- app.py +14 -13
- requirements.txt +5 -4
README.md
CHANGED
@@ -4,7 +4,7 @@ emoji: 🪄🖼️
|
|
4 |
colorFrom: indigo
|
5 |
colorTo: pink
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 3.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
|
|
4 |
colorFrom: indigo
|
5 |
colorTo: pink
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.50.2
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
app.py
CHANGED
@@ -4,7 +4,6 @@ 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]
|
@@ -20,17 +19,18 @@ def get_mask(img, s=1024):
|
|
20 |
mask = cv2.resize(mask, (w0, h0))[:, :, np.newaxis]
|
21 |
return mask
|
22 |
|
23 |
-
|
24 |
-
def rmbg_fn(img):
|
25 |
mask = get_mask(img)
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
31 |
mask = mask.repeat(3, axis=2)
|
32 |
-
return mask,
|
33 |
-
|
34 |
|
35 |
if __name__ == "__main__":
|
36 |
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider']
|
@@ -44,11 +44,12 @@ if __name__ == "__main__":
|
|
44 |
with gr.Row():
|
45 |
with gr.Column():
|
46 |
input_img = gr.Image(label="input image")
|
|
|
47 |
examples_data = [[f"examples/{x:02d}.jpg"] for x in range(1, 4)]
|
48 |
examples = gr.Dataset(components=[input_img], samples=examples_data)
|
49 |
-
|
50 |
output_mask = gr.Image(label="mask")
|
51 |
output_img = gr.Image(label="result", image_mode="RGBA")
|
52 |
examples.click(lambda x: x[0], [examples], [input_img])
|
53 |
-
run_btn.click(rmbg_fn, [input_img], [output_mask, output_img])
|
54 |
-
app.launch()
|
|
|
4 |
import numpy as np
|
5 |
import cv2
|
6 |
|
|
|
7 |
def get_mask(img, s=1024):
|
8 |
img = (img / 255).astype(np.float32)
|
9 |
h, w = h0, w0 = img.shape[:-1]
|
|
|
19 |
mask = cv2.resize(mask, (w0, h0))[:, :, np.newaxis]
|
20 |
return mask
|
21 |
|
22 |
+
def rmbg_fn(img, bg_color):
|
|
|
23 |
mask = get_mask(img)
|
24 |
+
if bg_color == "透過":
|
25 |
+
img_with_bg = (mask * img + 255 * (1 - mask)).astype(np.uint8)
|
26 |
+
mask = (mask * 255).astype(np.uint8)
|
27 |
+
img_with_bg = np.concatenate([img_with_bg, mask], axis=2, dtype=np.uint8)
|
28 |
+
else: # 白色背景
|
29 |
+
foreground = mask * img
|
30 |
+
background = 255 * (1 - mask) # 白背景
|
31 |
+
img_with_bg = (foreground + background).astype(np.uint8)
|
32 |
mask = mask.repeat(3, axis=2)
|
33 |
+
return mask, img_with_bg
|
|
|
34 |
|
35 |
if __name__ == "__main__":
|
36 |
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider']
|
|
|
44 |
with gr.Row():
|
45 |
with gr.Column():
|
46 |
input_img = gr.Image(label="input image")
|
47 |
+
bg_color = gr.Radio(choices=["透過", "白色"], value="透過", label="背景色")
|
48 |
examples_data = [[f"examples/{x:02d}.jpg"] for x in range(1, 4)]
|
49 |
examples = gr.Dataset(components=[input_img], samples=examples_data)
|
50 |
+
run_btn = gr.Button(variant="primary")
|
51 |
output_mask = gr.Image(label="mask")
|
52 |
output_img = gr.Image(label="result", image_mode="RGBA")
|
53 |
examples.click(lambda x: x[0], [examples], [input_img])
|
54 |
+
run_btn.click(rmbg_fn, [input_img, bg_color], [output_mask, output_img])
|
55 |
+
app.launch(server_name='0.0.0.0')
|
requirements.txt
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
-
onnx
|
2 |
-
onnxruntime-gpu
|
3 |
-
opencv-python
|
4 |
-
numpy
|
|
|
|
1 |
+
onnx==1.15.0
|
2 |
+
onnxruntime-gpu==1.17.1
|
3 |
+
opencv-python-headless==4.9.0.80
|
4 |
+
numpy==1.26.3
|
5 |
+
|