Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
|
|
|
| 3 |
|
| 4 |
def overlay_image(background, overlay):
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def virtual_try_on(user_image, shirt_image):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
# Gradio interface
|
| 28 |
iface = gr.Interface(
|
| 29 |
fn=virtual_try_on,
|
| 30 |
inputs=[
|
| 31 |
-
gr.Image(
|
| 32 |
-
gr.Image(
|
| 33 |
],
|
| 34 |
outputs=[
|
| 35 |
-
gr.Image(label="Original
|
| 36 |
-
gr.Image(label="Try-On
|
| 37 |
],
|
| 38 |
-
title="Virtual Try-On
|
| 39 |
-
description="
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
)
|
| 42 |
|
| 43 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
|
| 5 |
def overlay_image(background, overlay):
|
| 6 |
+
try:
|
| 7 |
+
# Ensure both images have alpha channel
|
| 8 |
+
background = background.convert("RGBA")
|
| 9 |
+
overlay = overlay.convert("RGBA")
|
| 10 |
+
|
| 11 |
+
# Resize overlay to match background
|
| 12 |
+
overlay = overlay.resize(background.size, Image.Resampling.LANCZOS)
|
| 13 |
+
|
| 14 |
+
# Composite images
|
| 15 |
+
return Image.alpha_composite(background, overlay)
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print(f"Error in overlay: {str(e)}")
|
| 18 |
+
return None
|
| 19 |
|
| 20 |
def virtual_try_on(user_image, shirt_image):
|
| 21 |
+
try:
|
| 22 |
+
# Open and verify images
|
| 23 |
+
user_img = Image.fromarray(user_image) if isinstance(user_image, np.ndarray) else user_image
|
| 24 |
+
shirt_img = Image.open(shirt_image) if isinstance(shirt_image, str) else shirt_image
|
| 25 |
+
|
| 26 |
+
result = overlay_image(user_img.convert("RGBA"), shirt_img.convert("RGBA"))
|
| 27 |
+
|
| 28 |
+
if result is None:
|
| 29 |
+
raise gr.Error("Failed to process images. Please check your inputs.")
|
| 30 |
+
|
| 31 |
+
return [user_img, result]
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"Error in try-on: {str(e)}")
|
| 34 |
+
raise gr.Error(f"Processing error: {str(e)}")
|
| 35 |
|
|
|
|
| 36 |
iface = gr.Interface(
|
| 37 |
fn=virtual_try_on,
|
| 38 |
inputs=[
|
| 39 |
+
gr.Image(label="Your Photo", type="pil"),
|
| 40 |
+
gr.Image(label="T-Shirt Design", type="filepath")
|
| 41 |
],
|
| 42 |
outputs=[
|
| 43 |
+
gr.Image(label="Original"),
|
| 44 |
+
gr.Image(label="Virtual Try-On")
|
| 45 |
],
|
| 46 |
+
title="YOURBRAND Virtual Try-On",
|
| 47 |
+
description="Upload your photo and a t-shirt design to see how it looks!",
|
| 48 |
+
examples=[
|
| 49 |
+
["sample_person.jpg", "sample_shirt.png"]
|
| 50 |
+
]
|
| 51 |
)
|
| 52 |
|
| 53 |
+
iface.launch(debug=True)
|