Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import torch
|
| 4 |
+
import cv2
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from torchvision import transforms
|
| 7 |
+
from cloth-segmentation.networks.u2net import U2NET # Import U²-Net
|
| 8 |
+
|
| 9 |
+
# Load U²-Net model
|
| 10 |
+
model_path = "cloth-segmentation/models/u2net.pth" # Ensure this path is correct
|
| 11 |
+
model = U2NET(3, 1)
|
| 12 |
+
model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
| 13 |
+
model.eval()
|
| 14 |
+
|
| 15 |
+
def segment_dress(image_np):
|
| 16 |
+
"""Segment the dress from the image using U²-Net."""
|
| 17 |
+
transform_pipeline = transforms.Compose([
|
| 18 |
+
transforms.ToTensor(),
|
| 19 |
+
transforms.Resize((320, 320))
|
| 20 |
+
])
|
| 21 |
+
image = Image.fromarray(image_np).convert("RGB")
|
| 22 |
+
input_tensor = transform_pipeline(image).unsqueeze(0)
|
| 23 |
+
|
| 24 |
+
with torch.no_grad():
|
| 25 |
+
output = model(input_tensor)[0][0].squeeze().cpu().numpy()
|
| 26 |
+
|
| 27 |
+
mask = (output > 0.5).astype(np.uint8) # Thresholding for binary mask
|
| 28 |
+
mask = cv2.resize(mask, (image_np.shape[1], image_np.shape[0])) # Resize mask to original
|
| 29 |
+
return mask
|
| 30 |
+
|
| 31 |
+
def change_dress_color(image_path, color):
|
| 32 |
+
"""Change the dress color based on the detected dress mask."""
|
| 33 |
+
if image_path is None:
|
| 34 |
+
return None
|
| 35 |
+
|
| 36 |
+
img = Image.open(image_path).convert("RGB")
|
| 37 |
+
img_np = np.array(img)
|
| 38 |
+
mask = segment_dress(img_np)
|
| 39 |
+
|
| 40 |
+
if mask is None:
|
| 41 |
+
return img # No dress detected
|
| 42 |
+
|
| 43 |
+
# Convert the selected color to HSV
|
| 44 |
+
color_map = {
|
| 45 |
+
"Red": (0, 255, 255),
|
| 46 |
+
"Blue": (120, 255, 255),
|
| 47 |
+
"Green": (60, 255, 255),
|
| 48 |
+
"Yellow": (30, 255, 255),
|
| 49 |
+
"Purple": (150, 255, 255)
|
| 50 |
+
}
|
| 51 |
+
hsv_color = np.uint8([[color_map.get(color, (0, 255, 255))]]) # Default to Red
|
| 52 |
+
|
| 53 |
+
# Convert to BGR
|
| 54 |
+
new_color_bgr = cv2.cvtColor(hsv_color, cv2.COLOR_HSV2BGR)[0][0]
|
| 55 |
+
|
| 56 |
+
# Apply the color change
|
| 57 |
+
img_hsv = cv2.cvtColor(img_np, cv2.COLOR_RGB2HSV)
|
| 58 |
+
img_hsv[..., 0] = mask * new_color_bgr[0] + (1 - mask) * img_hsv[..., 0] # Adjust hue
|
| 59 |
+
img_hsv[..., 1] = mask * new_color_bgr[1] + (1 - mask) * img_hsv[..., 1] # Adjust saturation
|
| 60 |
+
img_hsv[..., 2] = mask * new_color_bgr[2] + (1 - mask) * img_hsv[..., 2] # Adjust value
|
| 61 |
+
|
| 62 |
+
img_recolored = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2RGB)
|
| 63 |
+
|
| 64 |
+
return Image.fromarray(img_recolored)
|
| 65 |
+
|
| 66 |
+
# Gradio Interface
|
| 67 |
+
demo = gr.Interface(
|
| 68 |
+
fn=change_dress_color,
|
| 69 |
+
inputs=[
|
| 70 |
+
gr.Image(type="filepath", label="Upload Dress Image"),
|
| 71 |
+
gr.Radio(["Red", "Blue", "Green", "Yellow", "Purple"], label="Choose New Dress Color")
|
| 72 |
+
],
|
| 73 |
+
outputs=gr.Image(type="pil", label="Color Changed Dress"),
|
| 74 |
+
title="Dress Color Changer",
|
| 75 |
+
description="Upload an image of a dress and select a new color to change its appearance."
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
demo.launch()
|