Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,10 @@ import gradio as gr
|
|
| 2 |
from PIL import Image
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Load all hairstyles from the folder
|
| 6 |
def load_hairstyles():
|
| 7 |
folder = "hairstyles"
|
| 8 |
if not os.path.exists(folder):
|
| 9 |
return []
|
| 10 |
-
|
| 11 |
return [
|
| 12 |
Image.open(os.path.join(folder, f)).convert("RGBA")
|
| 13 |
for f in sorted(os.listdir(folder)) if f.endswith(".png")
|
|
@@ -15,28 +13,47 @@ def load_hairstyles():
|
|
| 15 |
|
| 16 |
hairstyles = load_hairstyles()
|
| 17 |
|
| 18 |
-
|
| 19 |
-
def apply_hairstyle(user_img, style_index):
|
| 20 |
if user_img is None or not hairstyles:
|
| 21 |
return None
|
| 22 |
|
| 23 |
user_img = user_img.convert("RGBA")
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
return result.convert("RGB")
|
| 27 |
|
| 28 |
-
# Gradio UI
|
| 29 |
with gr.Blocks() as demo:
|
| 30 |
-
gr.Markdown("## π Salon Virtual Hairstyle Try-On")
|
| 31 |
|
| 32 |
with gr.Row():
|
| 33 |
with gr.Column():
|
| 34 |
image_input = gr.Image(type="pil", label="π· Upload an Image")
|
| 35 |
style_slider = gr.Slider(0, max(len(hairstyles)-1, 0), step=1, label="π¨ Select Hairstyle")
|
|
|
|
|
|
|
|
|
|
| 36 |
apply_btn = gr.Button("β¨ Apply Hairstyle")
|
| 37 |
with gr.Column():
|
| 38 |
-
result_output = gr.Image(label="π Preview
|
| 39 |
|
| 40 |
-
apply_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
demo.launch()
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
import os
|
| 4 |
|
|
|
|
| 5 |
def load_hairstyles():
|
| 6 |
folder = "hairstyles"
|
| 7 |
if not os.path.exists(folder):
|
| 8 |
return []
|
|
|
|
| 9 |
return [
|
| 10 |
Image.open(os.path.join(folder, f)).convert("RGBA")
|
| 11 |
for f in sorted(os.listdir(folder)) if f.endswith(".png")
|
|
|
|
| 13 |
|
| 14 |
hairstyles = load_hairstyles()
|
| 15 |
|
| 16 |
+
def apply_hairstyle(user_img, style_index, x_offset, y_offset, scale):
|
|
|
|
| 17 |
if user_img is None or not hairstyles:
|
| 18 |
return None
|
| 19 |
|
| 20 |
user_img = user_img.convert("RGBA")
|
| 21 |
+
base_w, base_h = user_img.size
|
| 22 |
+
|
| 23 |
+
hairstyle = hairstyles[style_index]
|
| 24 |
+
|
| 25 |
+
# Resize the hairstyle based on scale
|
| 26 |
+
new_size = (int(base_w * scale), int(hairstyle.height * (base_w * scale / hairstyle.width)))
|
| 27 |
+
hairstyle = hairstyle.resize(new_size)
|
| 28 |
+
|
| 29 |
+
# Create a blank transparent image to position the hairstyle
|
| 30 |
+
composite = Image.new("RGBA", user_img.size)
|
| 31 |
+
paste_x = int((base_w - new_size[0]) / 2 + x_offset)
|
| 32 |
+
paste_y = int(y_offset)
|
| 33 |
+
composite.paste(hairstyle, (paste_x, paste_y), hairstyle)
|
| 34 |
+
|
| 35 |
+
# Overlay it
|
| 36 |
+
result = Image.alpha_composite(user_img, composite)
|
| 37 |
return result.convert("RGB")
|
| 38 |
|
|
|
|
| 39 |
with gr.Blocks() as demo:
|
| 40 |
+
gr.Markdown("## π Salon Virtual Hairstyle Try-On (Adjustable)")
|
| 41 |
|
| 42 |
with gr.Row():
|
| 43 |
with gr.Column():
|
| 44 |
image_input = gr.Image(type="pil", label="π· Upload an Image")
|
| 45 |
style_slider = gr.Slider(0, max(len(hairstyles)-1, 0), step=1, label="π¨ Select Hairstyle")
|
| 46 |
+
x_offset = gr.Slider(-200, 200, value=0, step=1, label="β¬
οΈβ‘οΈ Move Left / Right")
|
| 47 |
+
y_offset = gr.Slider(-200, 200, value=0, step=1, label="β¬οΈβ¬οΈ Move Up / Down")
|
| 48 |
+
scale = gr.Slider(0.3, 2.0, value=1.0, step=0.05, label="π Scale Hairstyle")
|
| 49 |
apply_btn = gr.Button("β¨ Apply Hairstyle")
|
| 50 |
with gr.Column():
|
| 51 |
+
result_output = gr.Image(label="π Result Preview")
|
| 52 |
|
| 53 |
+
apply_btn.click(
|
| 54 |
+
fn=apply_hairstyle,
|
| 55 |
+
inputs=[image_input, style_slider, x_offset, y_offset, scale],
|
| 56 |
+
outputs=result_output
|
| 57 |
+
)
|
| 58 |
|
| 59 |
demo.launch()
|