daidedou commited on
Commit ·
76f2c8c
1
Parent(s): d6ea51d
added remove background
Browse files- .gitignore +4 -0
- app.py +22 -4
- requirements.txt +3 -1
.gitignore
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.gitattributes
|
| 2 |
+
examples/*
|
| 3 |
+
.vscode/*
|
| 4 |
+
__pycache__/*
|
app.py
CHANGED
|
@@ -7,8 +7,17 @@ import svgwrite
|
|
| 7 |
import base64
|
| 8 |
import requests
|
| 9 |
from io import BytesIO
|
|
|
|
| 10 |
import argparse
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# -------------------------------------------------
|
| 14 |
# Example Images
|
|
@@ -110,16 +119,23 @@ def generate(
|
|
| 110 |
background_mode,
|
| 111 |
rounded,
|
| 112 |
clipping,
|
|
|
|
| 113 |
):
|
| 114 |
-
|
| 115 |
if image is None:
|
| 116 |
return None, None, None
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
border_color = rgb_to_hex(border_r, border_g, border_b)
|
|
|
|
| 119 |
padding_color = None
|
| 120 |
-
|
| 121 |
if use_padding:
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
mosaic, _ = make_vit_mosaic(
|
| 125 |
image,
|
|
@@ -211,6 +227,7 @@ with gr.Blocks() as demo:
|
|
| 211 |
|
| 212 |
rounded = gr.Checkbox(value=True, label="Enable rounded corners")
|
| 213 |
clipping = gr.Checkbox(value=True, label="True rounded clipping")
|
|
|
|
| 214 |
|
| 215 |
supersample = gr.Slider(1, 4, value=2, step=1)
|
| 216 |
scale_mode = gr.Radio(["keep", "downscale"], value="keep")
|
|
@@ -224,7 +241,7 @@ with gr.Blocks() as demo:
|
|
| 224 |
|
| 225 |
gr.Markdown("### 📥 Selected Image")
|
| 226 |
|
| 227 |
-
input_image = gr.Image(type="pil", height=250)
|
| 228 |
credit_display = gr.Markdown("")
|
| 229 |
|
| 230 |
gr.Markdown("### 🖼 Mosaic Preview")
|
|
@@ -270,6 +287,7 @@ with gr.Blocks() as demo:
|
|
| 270 |
background_mode,
|
| 271 |
rounded,
|
| 272 |
clipping,
|
|
|
|
| 273 |
],
|
| 274 |
outputs=[output_image, download_png, download_svg],
|
| 275 |
)
|
|
|
|
| 7 |
import base64
|
| 8 |
import requests
|
| 9 |
from io import BytesIO
|
| 10 |
+
from rembg import new_session, remove
|
| 11 |
import argparse
|
| 12 |
|
| 13 |
+
# -------------------------------------------------
|
| 14 |
+
# Background removal session (CPU friendly)
|
| 15 |
+
# -------------------------------------------------
|
| 16 |
+
|
| 17 |
+
print("Loading rembg model...")
|
| 18 |
+
rembg_session = new_session("u2net")
|
| 19 |
+
print("rembg model loaded")
|
| 20 |
+
|
| 21 |
|
| 22 |
# -------------------------------------------------
|
| 23 |
# Example Images
|
|
|
|
| 119 |
background_mode,
|
| 120 |
rounded,
|
| 121 |
clipping,
|
| 122 |
+
remove_bg,
|
| 123 |
):
|
|
|
|
| 124 |
if image is None:
|
| 125 |
return None, None, None
|
| 126 |
+
|
| 127 |
+
if remove_bg:
|
| 128 |
+
image = remove(image, session=rembg_session)
|
| 129 |
|
| 130 |
border_color = rgb_to_hex(border_r, border_g, border_b)
|
| 131 |
+
|
| 132 |
padding_color = None
|
|
|
|
| 133 |
if use_padding:
|
| 134 |
+
# If the image has transparency -> keep padding transparent
|
| 135 |
+
if image.mode == "RGBA":
|
| 136 |
+
padding_color = None
|
| 137 |
+
else:
|
| 138 |
+
padding_color = rgb_to_hex(pad_r, pad_g, pad_b)
|
| 139 |
|
| 140 |
mosaic, _ = make_vit_mosaic(
|
| 141 |
image,
|
|
|
|
| 227 |
|
| 228 |
rounded = gr.Checkbox(value=True, label="Enable rounded corners")
|
| 229 |
clipping = gr.Checkbox(value=True, label="True rounded clipping")
|
| 230 |
+
remove_bg = gr.Checkbox(value=False, label="Remove Background (rembg)")
|
| 231 |
|
| 232 |
supersample = gr.Slider(1, 4, value=2, step=1)
|
| 233 |
scale_mode = gr.Radio(["keep", "downscale"], value="keep")
|
|
|
|
| 241 |
|
| 242 |
gr.Markdown("### 📥 Selected Image")
|
| 243 |
|
| 244 |
+
input_image = gr.Image(type="pil", image_mode="RGBA", height=250)
|
| 245 |
credit_display = gr.Markdown("")
|
| 246 |
|
| 247 |
gr.Markdown("### 🖼 Mosaic Preview")
|
|
|
|
| 287 |
background_mode,
|
| 288 |
rounded,
|
| 289 |
clipping,
|
| 290 |
+
remove_bg,
|
| 291 |
],
|
| 292 |
outputs=[output_image, download_png, download_svg],
|
| 293 |
)
|
requirements.txt
CHANGED
|
@@ -4,4 +4,6 @@ pillow~=10.4.0
|
|
| 4 |
svgwrite==1.4.3
|
| 5 |
requests==2.31.0
|
| 6 |
huggingface-hub==0.34.3
|
| 7 |
-
pydantic==2.10.6
|
|
|
|
|
|
|
|
|
| 4 |
svgwrite==1.4.3
|
| 5 |
requests==2.31.0
|
| 6 |
huggingface-hub==0.34.3
|
| 7 |
+
pydantic==2.10.6
|
| 8 |
+
rembg
|
| 9 |
+
onnxruntime
|