Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ from PIL import Image
|
|
6 |
from torchvision import transforms
|
7 |
from cloth_segmentation.networks.u2net import U2NET
|
8 |
import matplotlib.colors as mcolors
|
|
|
9 |
|
10 |
# Load U²-Net
|
11 |
model_path = "cloth_segmentation/networks/u2net.pth"
|
@@ -85,36 +86,50 @@ def recolor_dress(image_np, dress_mask, target_color):
|
|
85 |
|
86 |
return (image_np * (1 - adaptive_feather[..., None] / 255) + img_recolored * (adaptive_feather[..., None] / 255)).astype(np.uint8)
|
87 |
|
88 |
-
# Main function
|
89 |
def change_dress_color(img, color_prompt):
|
90 |
if img is None or not color_prompt:
|
91 |
return img
|
92 |
|
93 |
-
img_np = np.array(img)
|
94 |
-
target_bgr = get_bgr_from_color_name(color_prompt)
|
95 |
-
|
96 |
try:
|
|
|
|
|
|
|
97 |
dress_mask = segment_dress(img_np)
|
98 |
if np.sum(dress_mask) < 1000:
|
99 |
return img
|
|
|
100 |
dress_mask = apply_grabcut(img_np, dress_mask)
|
101 |
img_recolored = recolor_dress(img_np, dress_mask, target_bgr)
|
102 |
return Image.fromarray(img_recolored)
|
|
|
103 |
except Exception as e:
|
104 |
print(f"Error: {e}")
|
|
|
105 |
return img
|
106 |
|
107 |
-
#
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
inputs=[
|
111 |
gr.Image(type="pil", label="Upload Image"),
|
112 |
-
gr.Textbox(label="
|
113 |
],
|
114 |
-
outputs=gr.Image(type="pil", label="
|
115 |
-
title="🎨 AI Dress Recolorer
|
116 |
-
description="Upload an image and
|
|
|
|
|
|
|
|
|
117 |
)
|
118 |
|
|
|
119 |
if __name__ == "__main__":
|
120 |
-
|
|
|
6 |
from torchvision import transforms
|
7 |
from cloth_segmentation.networks.u2net import U2NET
|
8 |
import matplotlib.colors as mcolors
|
9 |
+
import traceback
|
10 |
|
11 |
# Load U²-Net
|
12 |
model_path = "cloth_segmentation/networks/u2net.pth"
|
|
|
86 |
|
87 |
return (image_np * (1 - adaptive_feather[..., None] / 255) + img_recolored * (adaptive_feather[..., None] / 255)).astype(np.uint8)
|
88 |
|
89 |
+
# Main function with enhanced error handling
|
90 |
def change_dress_color(img, color_prompt):
|
91 |
if img is None or not color_prompt:
|
92 |
return img
|
93 |
|
|
|
|
|
|
|
94 |
try:
|
95 |
+
img_np = np.array(img)
|
96 |
+
target_bgr = get_bgr_from_color_name(color_prompt)
|
97 |
+
|
98 |
dress_mask = segment_dress(img_np)
|
99 |
if np.sum(dress_mask) < 1000:
|
100 |
return img
|
101 |
+
|
102 |
dress_mask = apply_grabcut(img_np, dress_mask)
|
103 |
img_recolored = recolor_dress(img_np, dress_mask, target_bgr)
|
104 |
return Image.fromarray(img_recolored)
|
105 |
+
|
106 |
except Exception as e:
|
107 |
print(f"Error: {e}")
|
108 |
+
traceback.print_exc()
|
109 |
return img
|
110 |
|
111 |
+
# Create a simple function that wraps the main functionality
|
112 |
+
def process_image(image, color_name):
|
113 |
+
if image is None:
|
114 |
+
return None
|
115 |
+
return change_dress_color(image, color_name)
|
116 |
+
|
117 |
+
# Create Gradio interface with explicit input/output definitions
|
118 |
+
iface = gr.Interface(
|
119 |
+
fn=process_image,
|
120 |
inputs=[
|
121 |
gr.Image(type="pil", label="Upload Image"),
|
122 |
+
gr.Textbox(label="Dress Color", placeholder="Enter color name (e.g. blue, lavender, crimson)")
|
123 |
],
|
124 |
+
outputs=gr.Image(type="pil", label="Result"),
|
125 |
+
title="🎨 AI Dress Recolorer",
|
126 |
+
description="Upload an image of a person wearing a dress and enter a color name to recolor the dress",
|
127 |
+
examples=[
|
128 |
+
["examples/dress1.jpg", "red"],
|
129 |
+
["examples/dress2.jpg", "blue"]
|
130 |
+
]
|
131 |
)
|
132 |
|
133 |
+
# Launch the application
|
134 |
if __name__ == "__main__":
|
135 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|