Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -15,52 +15,8 @@ torch.jit.script = lambda f: f
|
|
15 |
from model.cloth_masker import AutoMasker, vis_mask
|
16 |
from model.pipeline import CatVTONPipeline
|
17 |
from utils import init_weight_dtype, resize_and_crop, resize_and_padding
|
18 |
-
from PIL import Image
|
19 |
|
20 |
-
|
21 |
-
"""
|
22 |
-
Add a watermark to an image
|
23 |
-
Args:
|
24 |
-
main_image (PIL.Image): The main image
|
25 |
-
logo_path (str): Path to the logo image
|
26 |
-
position (str): Position of watermark ('bottom-right', 'bottom-left', 'top-right', 'top-left')
|
27 |
-
size_percentage (int): Size of watermark relative to main image (in percentage)
|
28 |
-
Returns:
|
29 |
-
PIL.Image: Image with watermark
|
30 |
-
"""
|
31 |
-
# Open and resize the logo
|
32 |
-
logo = Image.open(logo_path).convert('RGBA')
|
33 |
-
|
34 |
-
# Calculate the size for the logo
|
35 |
-
main_width, main_height = main_image.size
|
36 |
-
logo_width = int(main_width * size_percentage / 100)
|
37 |
-
logo_height = int(logo.size[1] * (logo_width / logo.size[0]))
|
38 |
-
logo = logo.resize((logo_width, logo_height), Image.Resampling.LANCZOS)
|
39 |
-
|
40 |
-
# Convert main image to RGBA if it isn't already
|
41 |
-
if main_image.mode != 'RGBA':
|
42 |
-
main_image = main_image.convert('RGBA')
|
43 |
-
|
44 |
-
# Create a new blank image with the same size as main image
|
45 |
-
watermarked = Image.new('RGBA', main_image.size, (0, 0, 0, 0))
|
46 |
-
watermarked.paste(main_image, (0, 0))
|
47 |
-
|
48 |
-
# Calculate position
|
49 |
-
if position == 'bottom-right':
|
50 |
-
position = (main_width - logo_width - 10, main_height - logo_height - 10)
|
51 |
-
elif position == 'bottom-left':
|
52 |
-
position = (10, main_height - logo_height - 10)
|
53 |
-
elif position == 'top-right':
|
54 |
-
position = (main_width - logo_width - 10, 10)
|
55 |
-
elif position == 'top-left':
|
56 |
-
position = (10, 10)
|
57 |
-
|
58 |
-
# Paste the logo
|
59 |
-
watermarked.paste(logo, position, logo)
|
60 |
-
|
61 |
-
# Convert back to RGB
|
62 |
-
return watermarked.convert('RGB')
|
63 |
-
|
64 |
def parse_args():
|
65 |
parser = argparse.ArgumentParser(description="Simple example of a training script.")
|
66 |
parser.add_argument(
|
@@ -230,9 +186,8 @@ def submit_function(
|
|
230 |
masked_person = vis_mask(person_image, mask)
|
231 |
save_result_image = image_grid([person_image, masked_person, cloth_image, result_image], 1, 4)
|
232 |
save_result_image.save(result_save_path)
|
233 |
-
final_image = None
|
234 |
if show_type == "result only":
|
235 |
-
|
236 |
else:
|
237 |
width, height = person_image.size
|
238 |
if show_type == "input & result":
|
@@ -245,11 +200,7 @@ def submit_function(
|
|
245 |
new_result_image = Image.new("RGB", (width + condition_width + 5, height))
|
246 |
new_result_image.paste(conditions, (0, 0))
|
247 |
new_result_image.paste(result_image, (condition_width + 5, 0))
|
248 |
-
|
249 |
-
|
250 |
-
# Add watermark
|
251 |
-
watermarked_image = add_watermark(final_image, 'logo.png', 'bottom-right', 20)
|
252 |
-
return watermarked_image
|
253 |
|
254 |
|
255 |
def person_example_fn(image_path):
|
@@ -286,109 +237,12 @@ HEADER = """
|
|
286 |
· Thanks to <a href="https://huggingface.co/zero-gpu-explorers">ZeroGPU</a> for providing A100 for our <a href="https://huggingface.co/spaces/zhengchong/CatVTON">HuggingFace Space</a>. <br>
|
287 |
· SafetyChecker is set to filter NSFW content, but it may block normal results too. Please adjust the <span>`seed`</span> for normal outcomes.<br>
|
288 |
"""
|
289 |
-
def app_gradio():
|
290 |
-
custom_css = """
|
291 |
-
@media (max-width: 768px) {
|
292 |
-
.gr-column {
|
293 |
-
width: 100% !important;
|
294 |
-
padding: 0.5rem;
|
295 |
-
}
|
296 |
-
.gr-row {
|
297 |
-
flex-direction: column !important;
|
298 |
-
}
|
299 |
-
.container {
|
300 |
-
margin: 0.5rem !important;
|
301 |
-
padding: 1rem !important;
|
302 |
-
}
|
303 |
-
button.primary-btn {
|
304 |
-
padding: 0.8rem 1rem;
|
305 |
-
font-size: 1rem;
|
306 |
-
}
|
307 |
-
}
|
308 |
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
padding: 0.5rem;
|
313 |
-
}
|
314 |
-
button.primary-btn {
|
315 |
-
font-size: 0.8rem;
|
316 |
-
padding: 0.6rem 0.8rem;
|
317 |
-
}
|
318 |
-
.gr-form {
|
319 |
-
margin: 0.5rem;
|
320 |
-
}
|
321 |
-
}
|
322 |
-
button.primary-btn {
|
323 |
-
background: linear-gradient(135deg, #2541b2 0%, #1a237e 100%);
|
324 |
-
transition: all 0.3s ease;
|
325 |
-
border: none;
|
326 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
327 |
-
color: white !important;
|
328 |
-
}
|
329 |
-
button.primary-btn:hover {
|
330 |
-
transform: translateY(-2px);
|
331 |
-
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
|
332 |
-
}
|
333 |
-
.gr-button {
|
334 |
-
background: linear-gradient(135deg, #2541b2 0%, #1a237e 100%);
|
335 |
-
color: white !important;
|
336 |
-
border: none;
|
337 |
-
transition: all 0.3s ease;
|
338 |
-
}
|
339 |
-
.gr-button:hover {
|
340 |
-
opacity: 0.9;
|
341 |
-
transform: translateY(-2px);
|
342 |
-
}
|
343 |
-
body {
|
344 |
-
background: linear-gradient(135deg, #f8f9fa 0%, #e8eaf6 100%);
|
345 |
-
}
|
346 |
-
.container {
|
347 |
-
border-radius: 12px;
|
348 |
-
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
349 |
-
}
|
350 |
-
.gr-form {
|
351 |
-
border-radius: 8px;
|
352 |
-
background: white;
|
353 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
|
354 |
-
}
|
355 |
-
.gr-radio-group {
|
356 |
-
background: white;
|
357 |
-
padding: 12px;
|
358 |
-
border-radius: 8px;
|
359 |
-
}
|
360 |
-
.gr-accordion {
|
361 |
-
border-radius: 8px;
|
362 |
-
overflow: hidden;
|
363 |
-
}
|
364 |
-
/* Force white text in buttons */
|
365 |
-
button.primary-btn span {
|
366 |
-
color: white !important;
|
367 |
-
}
|
368 |
-
.gr-button span {
|
369 |
-
color: white !important;
|
370 |
-
}
|
371 |
-
"""
|
372 |
-
|
373 |
-
with gr.Blocks(title="Deradh Virtual Try-On", css=custom_css) as demo:
|
374 |
-
gr.Markdown(
|
375 |
-
"""
|
376 |
-
<div style="text-align: center; background: linear-gradient(135deg, #2541b2 0%, #1a237e 100%); padding: 2.5rem; color: white; border-radius: 0 0 20px 20px; margin-bottom: 2rem; box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
|
377 |
-
<h1 style="color: white; font-size: 2.5rem; font-weight: 600; margin-bottom: 1rem;">Deradh Virtual Try-On Experience</h1>
|
378 |
-
<div style="margin: 1rem 0;">
|
379 |
-
<a href="https://deradh.com" style="color: white; text-decoration: none; padding: 0.5rem 1rem; border: 2px solid white; border-radius: 25px; transition: all 0.3s ease;">
|
380 |
-
Visit Deradh.com
|
381 |
-
</a>
|
382 |
-
</div>
|
383 |
-
</div>
|
384 |
-
<div style="text-align: center; padding: 1rem; color: #6ed7fe; font-size: 1.2rem; font-weight: 500; margin-bottom: 2rem;">
|
385 |
-
Experience the future of fashion with our AI-powered virtual try-on technology, Every user will get 2-3 Free trials per day
|
386 |
-
</div>
|
387 |
-
"""
|
388 |
-
)
|
389 |
-
|
390 |
with gr.Row():
|
391 |
-
with gr.Column(scale=1, min_width=
|
392 |
with gr.Row():
|
393 |
image_path = gr.Image(
|
394 |
type="filepath",
|
@@ -396,98 +250,55 @@ def app_gradio():
|
|
396 |
visible=False,
|
397 |
)
|
398 |
person_image = gr.ImageEditor(
|
399 |
-
interactive=True,
|
400 |
-
label="Upload Your Photo",
|
401 |
-
type="filepath"
|
402 |
)
|
403 |
|
404 |
with gr.Row():
|
405 |
-
with gr.Column(scale=1, min_width=
|
406 |
cloth_image = gr.Image(
|
407 |
-
interactive=True,
|
408 |
-
label="Select Garment",
|
409 |
-
type="filepath"
|
410 |
)
|
411 |
-
with gr.Column(scale=1, min_width=
|
412 |
gr.Markdown(
|
413 |
-
''
|
414 |
-
<div style="color: white; background: linear-gradient(135deg, #2541b2 0%, #1a237e 100%);
|
415 |
-
padding: 1.2rem; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
|
416 |
-
<h3 style="margin: 0 0 0.8rem 0; color: white;">For Best Performance:</h3>
|
417 |
-
<ol style="margin: 0; padding-left: 1.2rem;">
|
418 |
-
<li>Stand in front of a plain, contrasting background.</li>
|
419 |
-
<li>Ensure your entire body is visible in the frame.</li>
|
420 |
-
<li>Upload the highest quality image possible.</li>
|
421 |
-
<li>Avoid cluttered or low-light environments.</li>
|
422 |
-
<li>Wear minimal accessories for accurate results.</li>
|
423 |
-
</ol>
|
424 |
-
</div>
|
425 |
-
'''
|
426 |
)
|
427 |
cloth_type = gr.Radio(
|
428 |
-
label="
|
429 |
choices=["upper", "lower", "overall"],
|
430 |
-
|
431 |
)
|
432 |
|
433 |
-
|
|
|
434 |
gr.Markdown(
|
435 |
-
''
|
436 |
-
<div style="text-align: center; color: #1a237e; font-weight: 500; margin: 1rem 0;
|
437 |
-
padding: 0.8rem; background: white; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.05);">
|
438 |
-
Important: Please wait after clicking Try On - Processing may take a moment
|
439 |
-
</div>
|
440 |
-
'''
|
441 |
)
|
442 |
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
# box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
|
448 |
-
# <h3 style="margin: 0 0 0.8rem 0; color: white;">Advanced Settings:</h3>
|
449 |
-
# <ul style="margin: 0; padding-left: 1.2rem;">
|
450 |
-
# <li>Increase Steps for higher quality</li>
|
451 |
-
# <li>Adjust CFG for color intensity</li>
|
452 |
-
# <li>Change Seed for different variations</li>
|
453 |
-
# </ul>
|
454 |
-
# </div>
|
455 |
-
# '''
|
456 |
-
# )
|
457 |
-
with gr.Accordion("Developer Options", open=False):
|
458 |
num_inference_steps = gr.Slider(
|
459 |
-
label="
|
460 |
-
minimum=10,
|
461 |
-
maximum=100,
|
462 |
-
step=5,
|
463 |
-
value=50
|
464 |
)
|
|
|
465 |
guidance_scale = gr.Slider(
|
466 |
-
label="
|
467 |
-
minimum=0.0,
|
468 |
-
maximum=7.5,
|
469 |
-
step=0.5,
|
470 |
-
value=2.5
|
471 |
)
|
|
|
472 |
seed = gr.Slider(
|
473 |
-
label="
|
474 |
-
minimum=-1,
|
475 |
-
maximum=10000,
|
476 |
-
step=1,
|
477 |
-
value=42
|
478 |
)
|
479 |
show_type = gr.Radio(
|
480 |
-
label="
|
481 |
choices=["result only", "input & result", "input & mask & result"],
|
482 |
-
value="input & result",
|
483 |
)
|
484 |
|
485 |
-
with gr.Column(scale=2, min_width=
|
486 |
-
result_image = gr.Image(
|
487 |
-
interactive=False,
|
488 |
-
label="Virtual Try-On Result"
|
489 |
-
)
|
490 |
with gr.Row():
|
|
|
491 |
root_path = "resource/demo/example"
|
492 |
with gr.Column():
|
493 |
men_exm = gr.Examples(
|
@@ -497,7 +308,7 @@ def app_gradio():
|
|
497 |
],
|
498 |
examples_per_page=4,
|
499 |
inputs=image_path,
|
500 |
-
label="
|
501 |
)
|
502 |
women_exm = gr.Examples(
|
503 |
examples=[
|
@@ -506,7 +317,10 @@ def app_gradio():
|
|
506 |
],
|
507 |
examples_per_page=4,
|
508 |
inputs=image_path,
|
509 |
-
label="
|
|
|
|
|
|
|
510 |
)
|
511 |
with gr.Column():
|
512 |
condition_upper_exm = gr.Examples(
|
@@ -516,7 +330,7 @@ def app_gradio():
|
|
516 |
],
|
517 |
examples_per_page=4,
|
518 |
inputs=cloth_image,
|
519 |
-
label="
|
520 |
)
|
521 |
condition_overall_exm = gr.Examples(
|
522 |
examples=[
|
@@ -525,7 +339,7 @@ def app_gradio():
|
|
525 |
],
|
526 |
examples_per_page=4,
|
527 |
inputs=cloth_image,
|
528 |
-
label="
|
529 |
)
|
530 |
condition_person_exm = gr.Examples(
|
531 |
examples=[
|
@@ -534,13 +348,14 @@ def app_gradio():
|
|
534 |
],
|
535 |
examples_per_page=4,
|
536 |
inputs=cloth_image,
|
537 |
-
label="
|
|
|
|
|
|
|
538 |
)
|
539 |
|
540 |
image_path.change(
|
541 |
-
person_example_fn,
|
542 |
-
inputs=image_path,
|
543 |
-
outputs=person_image
|
544 |
)
|
545 |
|
546 |
submit.click(
|
@@ -556,8 +371,6 @@ def app_gradio():
|
|
556 |
],
|
557 |
result_image,
|
558 |
)
|
559 |
-
|
560 |
-
|
561 |
demo.queue().launch(share=True, show_error=True)
|
562 |
|
563 |
|
|
|
15 |
from model.cloth_masker import AutoMasker, vis_mask
|
16 |
from model.pipeline import CatVTONPipeline
|
17 |
from utils import init_weight_dtype, resize_and_crop, resize_and_padding
|
|
|
18 |
|
19 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
def parse_args():
|
21 |
parser = argparse.ArgumentParser(description="Simple example of a training script.")
|
22 |
parser.add_argument(
|
|
|
186 |
masked_person = vis_mask(person_image, mask)
|
187 |
save_result_image = image_grid([person_image, masked_person, cloth_image, result_image], 1, 4)
|
188 |
save_result_image.save(result_save_path)
|
|
|
189 |
if show_type == "result only":
|
190 |
+
return result_image
|
191 |
else:
|
192 |
width, height = person_image.size
|
193 |
if show_type == "input & result":
|
|
|
200 |
new_result_image = Image.new("RGB", (width + condition_width + 5, height))
|
201 |
new_result_image.paste(conditions, (0, 0))
|
202 |
new_result_image.paste(result_image, (condition_width + 5, 0))
|
203 |
+
return new_result_image
|
|
|
|
|
|
|
|
|
204 |
|
205 |
|
206 |
def person_example_fn(image_path):
|
|
|
237 |
· Thanks to <a href="https://huggingface.co/zero-gpu-explorers">ZeroGPU</a> for providing A100 for our <a href="https://huggingface.co/spaces/zhengchong/CatVTON">HuggingFace Space</a>. <br>
|
238 |
· SafetyChecker is set to filter NSFW content, but it may block normal results too. Please adjust the <span>`seed`</span> for normal outcomes.<br>
|
239 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
|
241 |
+
def app_gradio():
|
242 |
+
with gr.Blocks(title="CatVTON") as demo:
|
243 |
+
gr.Markdown(HEADER)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
with gr.Row():
|
245 |
+
with gr.Column(scale=1, min_width=350):
|
246 |
with gr.Row():
|
247 |
image_path = gr.Image(
|
248 |
type="filepath",
|
|
|
250 |
visible=False,
|
251 |
)
|
252 |
person_image = gr.ImageEditor(
|
253 |
+
interactive=True, label="Person Image", type="filepath"
|
|
|
|
|
254 |
)
|
255 |
|
256 |
with gr.Row():
|
257 |
+
with gr.Column(scale=1, min_width=230):
|
258 |
cloth_image = gr.Image(
|
259 |
+
interactive=True, label="Condition Image", type="filepath"
|
|
|
|
|
260 |
)
|
261 |
+
with gr.Column(scale=1, min_width=120):
|
262 |
gr.Markdown(
|
263 |
+
'<span style="color: #808080; font-size: small;">Two ways to provide Mask:<br>1. Upload the person image and use the `🖌️` above to draw the Mask (higher priority)<br>2. Select the `Try-On Cloth Type` to generate automatically </span>'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
)
|
265 |
cloth_type = gr.Radio(
|
266 |
+
label="Try-On Cloth Type",
|
267 |
choices=["upper", "lower", "overall"],
|
268 |
+
value="upper",
|
269 |
)
|
270 |
|
271 |
+
|
272 |
+
submit = gr.Button("Submit")
|
273 |
gr.Markdown(
|
274 |
+
'<center><span style="color: #FF0000">!!! Click only Once, Wait for Delay !!!</span></center>'
|
|
|
|
|
|
|
|
|
|
|
275 |
)
|
276 |
|
277 |
+
gr.Markdown(
|
278 |
+
'<span style="color: #808080; font-size: small;">Advanced options can adjust details:<br>1. `Inference Step` may enhance details;<br>2. `CFG` is highly correlated with saturation;<br>3. `Random seed` may improve pseudo-shadow.</span>'
|
279 |
+
)
|
280 |
+
with gr.Accordion("Advanced Options", open=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
281 |
num_inference_steps = gr.Slider(
|
282 |
+
label="Inference Step", minimum=10, maximum=100, step=5, value=50
|
|
|
|
|
|
|
|
|
283 |
)
|
284 |
+
# Guidence Scale
|
285 |
guidance_scale = gr.Slider(
|
286 |
+
label="CFG Strenth", minimum=0.0, maximum=7.5, step=0.5, value=2.5
|
|
|
|
|
|
|
|
|
287 |
)
|
288 |
+
# Random Seed
|
289 |
seed = gr.Slider(
|
290 |
+
label="Seed", minimum=-1, maximum=10000, step=1, value=42
|
|
|
|
|
|
|
|
|
291 |
)
|
292 |
show_type = gr.Radio(
|
293 |
+
label="Show Type",
|
294 |
choices=["result only", "input & result", "input & mask & result"],
|
295 |
+
value="input & mask & result",
|
296 |
)
|
297 |
|
298 |
+
with gr.Column(scale=2, min_width=500):
|
299 |
+
result_image = gr.Image(interactive=False, label="Result")
|
|
|
|
|
|
|
300 |
with gr.Row():
|
301 |
+
# Photo Examples
|
302 |
root_path = "resource/demo/example"
|
303 |
with gr.Column():
|
304 |
men_exm = gr.Examples(
|
|
|
308 |
],
|
309 |
examples_per_page=4,
|
310 |
inputs=image_path,
|
311 |
+
label="Person Examples ①",
|
312 |
)
|
313 |
women_exm = gr.Examples(
|
314 |
examples=[
|
|
|
317 |
],
|
318 |
examples_per_page=4,
|
319 |
inputs=image_path,
|
320 |
+
label="Person Examples ②",
|
321 |
+
)
|
322 |
+
gr.Markdown(
|
323 |
+
'<span style="color: #808080; font-size: small;">*Person examples come from the demos of <a href="https://huggingface.co/spaces/levihsu/OOTDiffusion">OOTDiffusion</a> and <a href="https://www.outfitanyone.org">OutfitAnyone</a>. </span>'
|
324 |
)
|
325 |
with gr.Column():
|
326 |
condition_upper_exm = gr.Examples(
|
|
|
330 |
],
|
331 |
examples_per_page=4,
|
332 |
inputs=cloth_image,
|
333 |
+
label="Condition Upper Examples",
|
334 |
)
|
335 |
condition_overall_exm = gr.Examples(
|
336 |
examples=[
|
|
|
339 |
],
|
340 |
examples_per_page=4,
|
341 |
inputs=cloth_image,
|
342 |
+
label="Condition Overall Examples",
|
343 |
)
|
344 |
condition_person_exm = gr.Examples(
|
345 |
examples=[
|
|
|
348 |
],
|
349 |
examples_per_page=4,
|
350 |
inputs=cloth_image,
|
351 |
+
label="Condition Reference Person Examples",
|
352 |
+
)
|
353 |
+
gr.Markdown(
|
354 |
+
'<span style="color: #808080; font-size: small;">*Condition examples come from the Internet. </span>'
|
355 |
)
|
356 |
|
357 |
image_path.change(
|
358 |
+
person_example_fn, inputs=image_path, outputs=person_image
|
|
|
|
|
359 |
)
|
360 |
|
361 |
submit.click(
|
|
|
371 |
],
|
372 |
result_image,
|
373 |
)
|
|
|
|
|
374 |
demo.queue().launch(share=True, show_error=True)
|
375 |
|
376 |
|