Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -38,7 +38,6 @@ model = SF3D.from_pretrained(
|
|
38 |
)
|
39 |
model.eval().cuda()
|
40 |
|
41 |
-
# Load example files
|
42 |
example_files = [
|
43 |
os.path.join("demo_files/examples", f) for f in os.listdir("demo_files/examples")
|
44 |
]
|
@@ -52,7 +51,6 @@ def run_model(input_image):
|
|
52 |
trimesh_mesh, _glob_dict = model.generate_mesh(model_batch, 1024)
|
53 |
trimesh_mesh = trimesh_mesh[0]
|
54 |
|
55 |
-
# Create new temporary file
|
56 |
tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".glb")
|
57 |
trimesh_mesh.export(tmp_file.name, file_type="glb", include_normals=True)
|
58 |
|
@@ -80,7 +78,6 @@ def create_batch(input_image: Image) -> dict[str, Any]:
|
|
80 |
"intrinsic_cond": intrinsic.unsqueeze(0),
|
81 |
"intrinsic_normed_cond": intrinsic_normed_cond.unsqueeze(0),
|
82 |
}
|
83 |
-
# Add batch dimension
|
84 |
batched = {k: v.unsqueeze(0) for k, v in batch_elem.items()}
|
85 |
return batched
|
86 |
|
@@ -89,7 +86,6 @@ def checkerboard(squares: int, size: int, min_value: float = 0.5):
|
|
89 |
base = np.zeros((squares, squares)) + min_value
|
90 |
base[1::2, ::2] = 1
|
91 |
base[::2, 1::2] = 1
|
92 |
-
|
93 |
repeat_mult = size // squares
|
94 |
return (
|
95 |
base.repeat(repeat_mult, axis=0)
|
@@ -100,22 +96,12 @@ def checkerboard(squares: int, size: int, min_value: float = 0.5):
|
|
100 |
def remove_background(input_image: Image) -> Image:
|
101 |
return rembg.remove(input_image, session=rembg_session)
|
102 |
|
103 |
-
def resize_foreground(
|
104 |
-
image: Image,
|
105 |
-
ratio: float,
|
106 |
-
) -> Image:
|
107 |
image = np.array(image)
|
108 |
assert image.shape[-1] == 4
|
109 |
alpha = np.where(image[..., 3] > 0)
|
110 |
-
y1, y2, x1, x2 = (
|
111 |
-
alpha[0].min(),
|
112 |
-
alpha[0].max(),
|
113 |
-
alpha[1].min(),
|
114 |
-
alpha[1].max(),
|
115 |
-
)
|
116 |
-
# Crop the foreground
|
117 |
fg = image[y1:y2, x1:x2]
|
118 |
-
# Pad to square
|
119 |
size = max(fg.shape[0], fg.shape[1])
|
120 |
ph0, pw0 = (size - fg.shape[0]) // 2, (size - fg.shape[1]) // 2
|
121 |
ph1, pw1 = size - fg.shape[0] - ph0, size - fg.shape[1] - pw0
|
@@ -126,7 +112,6 @@ def resize_foreground(
|
|
126 |
constant_values=0,
|
127 |
)
|
128 |
|
129 |
-
# Compute padding according to the ratio
|
130 |
new_size = int(new_image.shape[0] / ratio)
|
131 |
ph0, pw0 = (new_size - size) // 2, (new_size - size) // 2
|
132 |
ph1, pw1 = new_size - size - ph0, new_size - size - pw0
|
@@ -136,9 +121,7 @@ def resize_foreground(
|
|
136 |
mode="constant",
|
137 |
constant_values=0,
|
138 |
)
|
139 |
-
new_image = Image.fromarray(new_image, mode="RGBA").resize(
|
140 |
-
(COND_WIDTH, COND_HEIGHT)
|
141 |
-
)
|
142 |
return new_image
|
143 |
|
144 |
def square_crop(input_image: Image) -> Image:
|
@@ -147,9 +130,7 @@ def square_crop(input_image: Image) -> Image:
|
|
147 |
top = (input_image.size[1] - min_size) // 2
|
148 |
right = (input_image.size[0] + min_size) // 2
|
149 |
bottom = (input_image.size[1] + min_size) // 2
|
150 |
-
return input_image.crop((left, top, right, bottom)).resize(
|
151 |
-
(COND_WIDTH, COND_HEIGHT)
|
152 |
-
)
|
153 |
|
154 |
def show_mask_img(input_image: Image) -> Image:
|
155 |
img_numpy = np.array(input_image)
|
@@ -196,7 +177,6 @@ def requires_bg_remove(image, fr):
|
|
196 |
min_alpha = alpha_channel.min()
|
197 |
|
198 |
if min_alpha == 0:
|
199 |
-
print("Already has alpha")
|
200 |
sqr_crop = square_crop(image)
|
201 |
fr_res = resize_foreground(sqr_crop, fr)
|
202 |
return (
|
@@ -223,7 +203,6 @@ def update_foreground_ratio(img_proc, fr):
|
|
223 |
gr.update(value=show_mask_img(foreground_res)),
|
224 |
)
|
225 |
|
226 |
-
# Define custom theme
|
227 |
class CustomTheme(gr.themes.Base):
|
228 |
def __init__(self):
|
229 |
super().__init__()
|
@@ -241,12 +220,13 @@ class CustomTheme(gr.themes.Base):
|
|
241 |
"https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;700&display=swap",
|
242 |
)
|
243 |
|
244 |
-
# Custom CSS
|
245 |
css = """
|
246 |
-
body {
|
247 |
-
background-color: #191a1e !important;
|
248 |
margin: 0;
|
249 |
padding: 0;
|
|
|
|
|
|
|
250 |
}
|
251 |
|
252 |
:root {
|
@@ -255,12 +235,14 @@ body {
|
|
255 |
--background-fill-tertiary: #191a1e !important;
|
256 |
}
|
257 |
|
258 |
-
|
259 |
-
.gradio-container {
|
260 |
background-color: #191a1e !important;
|
|
|
|
|
|
|
261 |
}
|
262 |
|
263 |
-
/*
|
264 |
.gr-block {
|
265 |
background-color: #191a1e !important;
|
266 |
border: none !important;
|
@@ -275,7 +257,6 @@ footer {
|
|
275 |
overflow: hidden;
|
276 |
}
|
277 |
|
278 |
-
/* Apply fonts */
|
279 |
body, input, button, textarea, select, .gr-button {
|
280 |
font-family: 'Poppins', sans-serif;
|
281 |
color: #FFFFFF;
|
@@ -288,12 +269,15 @@ h1, h2, h3, h4, h5, h6 {
|
|
288 |
color: #FFFFFF;
|
289 |
}
|
290 |
|
291 |
-
/*
|
292 |
.generate-button {
|
293 |
background-color: #5271FF !important;
|
294 |
color: #FFFFFF !important;
|
295 |
border: none;
|
296 |
font-weight: bold;
|
|
|
|
|
|
|
297 |
}
|
298 |
|
299 |
/* Examples container */
|
@@ -302,18 +286,17 @@ h1, h2, h3, h4, h5, h6 {
|
|
302 |
border: none !important;
|
303 |
}
|
304 |
|
305 |
-
/* 3D Model container
|
306 |
.litmodel3d {
|
307 |
background-color: #191a1e !important;
|
308 |
}
|
309 |
"""
|
310 |
|
311 |
-
# Build the Gradio interface
|
312 |
with gr.Blocks(theme=CustomTheme(), css=css) as demo:
|
313 |
img_proc_state = gr.State()
|
314 |
background_remove_state = gr.State()
|
315 |
|
316 |
-
with gr.Row():
|
317 |
with gr.Column():
|
318 |
with gr.Row():
|
319 |
input_img = gr.Image(
|
|
|
38 |
)
|
39 |
model.eval().cuda()
|
40 |
|
|
|
41 |
example_files = [
|
42 |
os.path.join("demo_files/examples", f) for f in os.listdir("demo_files/examples")
|
43 |
]
|
|
|
51 |
trimesh_mesh, _glob_dict = model.generate_mesh(model_batch, 1024)
|
52 |
trimesh_mesh = trimesh_mesh[0]
|
53 |
|
|
|
54 |
tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".glb")
|
55 |
trimesh_mesh.export(tmp_file.name, file_type="glb", include_normals=True)
|
56 |
|
|
|
78 |
"intrinsic_cond": intrinsic.unsqueeze(0),
|
79 |
"intrinsic_normed_cond": intrinsic_normed_cond.unsqueeze(0),
|
80 |
}
|
|
|
81 |
batched = {k: v.unsqueeze(0) for k, v in batch_elem.items()}
|
82 |
return batched
|
83 |
|
|
|
86 |
base = np.zeros((squares, squares)) + min_value
|
87 |
base[1::2, ::2] = 1
|
88 |
base[::2, 1::2] = 1
|
|
|
89 |
repeat_mult = size // squares
|
90 |
return (
|
91 |
base.repeat(repeat_mult, axis=0)
|
|
|
96 |
def remove_background(input_image: Image) -> Image:
|
97 |
return rembg.remove(input_image, session=rembg_session)
|
98 |
|
99 |
+
def resize_foreground(image: Image, ratio: float) -> Image:
|
|
|
|
|
|
|
100 |
image = np.array(image)
|
101 |
assert image.shape[-1] == 4
|
102 |
alpha = np.where(image[..., 3] > 0)
|
103 |
+
y1, y2, x1, x2 = alpha[0].min(), alpha[0].max(), alpha[1].min(), alpha[1].max()
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
fg = image[y1:y2, x1:x2]
|
|
|
105 |
size = max(fg.shape[0], fg.shape[1])
|
106 |
ph0, pw0 = (size - fg.shape[0]) // 2, (size - fg.shape[1]) // 2
|
107 |
ph1, pw1 = size - fg.shape[0] - ph0, size - fg.shape[1] - pw0
|
|
|
112 |
constant_values=0,
|
113 |
)
|
114 |
|
|
|
115 |
new_size = int(new_image.shape[0] / ratio)
|
116 |
ph0, pw0 = (new_size - size) // 2, (new_size - size) // 2
|
117 |
ph1, pw1 = new_size - size - ph0, new_size - size - pw0
|
|
|
121 |
mode="constant",
|
122 |
constant_values=0,
|
123 |
)
|
124 |
+
new_image = Image.fromarray(new_image, mode="RGBA").resize((COND_WIDTH, COND_HEIGHT))
|
|
|
|
|
125 |
return new_image
|
126 |
|
127 |
def square_crop(input_image: Image) -> Image:
|
|
|
130 |
top = (input_image.size[1] - min_size) // 2
|
131 |
right = (input_image.size[0] + min_size) // 2
|
132 |
bottom = (input_image.size[1] + min_size) // 2
|
133 |
+
return input_image.crop((left, top, right, bottom)).resize((COND_WIDTH, COND_HEIGHT))
|
|
|
|
|
134 |
|
135 |
def show_mask_img(input_image: Image) -> Image:
|
136 |
img_numpy = np.array(input_image)
|
|
|
177 |
min_alpha = alpha_channel.min()
|
178 |
|
179 |
if min_alpha == 0:
|
|
|
180 |
sqr_crop = square_crop(image)
|
181 |
fr_res = resize_foreground(sqr_crop, fr)
|
182 |
return (
|
|
|
203 |
gr.update(value=show_mask_img(foreground_res)),
|
204 |
)
|
205 |
|
|
|
206 |
class CustomTheme(gr.themes.Base):
|
207 |
def __init__(self):
|
208 |
super().__init__()
|
|
|
220 |
"https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;700&display=swap",
|
221 |
)
|
222 |
|
|
|
223 |
css = """
|
224 |
+
html, body {
|
|
|
225 |
margin: 0;
|
226 |
padding: 0;
|
227 |
+
background-color: #191a1e !important;
|
228 |
+
height: 100vh;
|
229 |
+
width: 100vw;
|
230 |
}
|
231 |
|
232 |
:root {
|
|
|
235 |
--background-fill-tertiary: #191a1e !important;
|
236 |
}
|
237 |
|
238 |
+
.gradio-container, .gr-app {
|
|
|
239 |
background-color: #191a1e !important;
|
240 |
+
color: #FFFFFF !important;
|
241 |
+
height: 100%;
|
242 |
+
width: 100%;
|
243 |
}
|
244 |
|
245 |
+
/* Set blocks */
|
246 |
.gr-block {
|
247 |
background-color: #191a1e !important;
|
248 |
border: none !important;
|
|
|
257 |
overflow: hidden;
|
258 |
}
|
259 |
|
|
|
260 |
body, input, button, textarea, select, .gr-button {
|
261 |
font-family: 'Poppins', sans-serif;
|
262 |
color: #FFFFFF;
|
|
|
269 |
color: #FFFFFF;
|
270 |
}
|
271 |
|
272 |
+
/* Increase button size */
|
273 |
.generate-button {
|
274 |
background-color: #5271FF !important;
|
275 |
color: #FFFFFF !important;
|
276 |
border: none;
|
277 |
font-weight: bold;
|
278 |
+
font-size: 1.2em; /* Увеличиваем размер шрифта */
|
279 |
+
padding: 0.75em 2em; /* Увеличиваем отступы внутри кнопки */
|
280 |
+
border-radius: 0.3em; /* Небольшое скругление */
|
281 |
}
|
282 |
|
283 |
/* Examples container */
|
|
|
286 |
border: none !important;
|
287 |
}
|
288 |
|
289 |
+
/* 3D Model container if needed */
|
290 |
.litmodel3d {
|
291 |
background-color: #191a1e !important;
|
292 |
}
|
293 |
"""
|
294 |
|
|
|
295 |
with gr.Blocks(theme=CustomTheme(), css=css) as demo:
|
296 |
img_proc_state = gr.State()
|
297 |
background_remove_state = gr.State()
|
298 |
|
299 |
+
with gr.Row():
|
300 |
with gr.Column():
|
301 |
with gr.Row():
|
302 |
input_img = gr.Image(
|