Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,54 @@
|
|
| 1 |
-
import
|
| 2 |
-
import cv2, numpy as np, gradio as gr
|
| 3 |
from PIL import Image
|
| 4 |
-
|
| 5 |
-
# ---------------------- Paths (hair/ first) ----------------------
|
| 6 |
-
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 7 |
-
CANDIDATES = [
|
| 8 |
-
os.path.join(BASE_DIR, "hair"), # <- your folder
|
| 9 |
-
os.path.join(BASE_DIR, "assets", "hairstyles"),
|
| 10 |
-
os.path.join(BASE_DIR, "assets", "Hairstyles"),
|
| 11 |
-
os.path.join(BASE_DIR, "hairstyles"),
|
| 12 |
-
]
|
| 13 |
-
HAIR_DIR = None
|
| 14 |
-
for p in CANDIDATES:
|
| 15 |
-
if os.path.isdir(p):
|
| 16 |
-
HAIR_DIR = p
|
| 17 |
-
break
|
| 18 |
-
if HAIR_DIR is None: # create canonical path if nothing exists yet
|
| 19 |
-
HAIR_DIR = os.path.join(BASE_DIR, "hair")
|
| 20 |
-
os.makedirs(HAIR_DIR, exist_ok=True)
|
| 21 |
-
|
| 22 |
-
META_PATH = os.path.join(HAIR_DIR, "meta.json") # optional per-style anchors
|
| 23 |
-
|
| 24 |
-
# ---------------------- MediaPipe ----------------------
|
| 25 |
-
try:
|
| 26 |
-
import mediapipe as mp
|
| 27 |
-
except Exception as e:
|
| 28 |
-
raise RuntimeError(f"Mediapipe import failed. Check requirements.txt pins. Details: {e}")
|
| 29 |
-
|
| 30 |
-
mp_face_mesh = mp.solutions.face_mesh
|
| 31 |
-
mp_selfie_seg = mp.solutions.selfie_segmentation
|
| 32 |
-
LM = {"left_eye_outer": 33, "right_eye_outer": 263, "mid_forehead": 10}
|
| 33 |
-
|
| 34 |
-
# ---------------------- Helpers ----------------------
|
| 35 |
def load_hairstyles():
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
def load_hairstyles():
|
| 5 |
+
folder = "hairstyles"
|
| 6 |
+
if not os.path.exists(folder):
|
| 7 |
+
return []
|
| 8 |
+
return [
|
| 9 |
+
Image.open(os.path.join(folder, f)).convert("RGBA")
|
| 10 |
+
for f in sorted(os.listdir(folder)) if f.endswith(".png")
|
| 11 |
+
]
|
| 12 |
+
hairstyles = load_hairstyles()
|
| 13 |
+
def apply_hairstyle(user_img, style_index, x_offset, y_offset, scale):
|
| 14 |
+
if user_img is None or not hairstyles:
|
| 15 |
+
return None
|
| 16 |
+
user_img = user_img.convert("RGBA")
|
| 17 |
+
base_w, base_h = user_img.size
|
| 18 |
+
|
| 19 |
+
hairstyle = hairstyles[style_index]
|
| 20 |
+
|
| 21 |
+
# Resize the hairstyle based on scale
|
| 22 |
+
new_size = (int(base_w * scale), int(hairstyle.height * (base_w * scale / hairstyle.width)))
|
| 23 |
+
hairstyle = hairstyle.resize(new_size)
|
| 24 |
+
|
| 25 |
+
# Create a blank transparent image to position the hairstyle
|
| 26 |
+
composite = Image.new("RGBA", user_img.size)
|
| 27 |
+
paste_x = int((base_w - new_size[0]) / 2 + x_offset)
|
| 28 |
+
paste_y = int(y_offset)
|
| 29 |
+
composite.paste(hairstyle, (paste_x, paste_y), hairstyle)
|
| 30 |
+
|
| 31 |
+
# Overlay it
|
| 32 |
+
result = Image.alpha_composite(user_img, composite)
|
| 33 |
+
return result.convert("RGB")
|
| 34 |
+
|
| 35 |
+
with gr.Blocks() as demo:
|
| 36 |
+
gr.Markdown("## 💇 Salon Virtual Hairstyle Try-On (Adjustable)")
|
| 37 |
+
with gr.Row():
|
| 38 |
+
with gr.Column():
|
| 39 |
+
image_input = gr.Image(type="pil", label="📷 Upload an Image")
|
| 40 |
+
style_slider = gr.Slider(0, max(len(hairstyles)-1, 0), step=1, label="🎨 Select Hairstyle")
|
| 41 |
+
x_offset = gr.Slider(-200, 200, value=0, step=1, label="⬅️➡️ Move Left / Right")
|
| 42 |
+
y_offset = gr.Slider(-200, 200, value=0, step=1, label="⬆️⬇️ Move Up / Down")
|
| 43 |
+
scale = gr.Slider(0.3, 2.0, value=1.0, step=0.05, label="📏 Scale Hairstyle")
|
| 44 |
+
apply_btn = gr.Button("✨ Apply Hairstyle")
|
| 45 |
+
with gr.Column():
|
| 46 |
+
result_output = gr.Image(label="🔍 Result Preview")
|
| 47 |
+
|
| 48 |
+
apply_btn.click(
|
| 49 |
+
fn=apply_hairstyle,
|
| 50 |
+
inputs=[image_input, style_slider, x_offset, y_offset, scale],
|
| 51 |
+
outputs=result_output
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
demo.launch()
|