gaur3009 commited on
Commit
7da70bd
·
verified ·
1 Parent(s): 2762b4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -26
app.py CHANGED
@@ -97,43 +97,57 @@ def change_dress_color(img, color):
97
  if img is None:
98
  return None
99
 
100
- img_np = np.array(img)
 
 
 
 
 
 
 
 
 
101
 
 
 
102
  # Get dress segmentation mask
103
  dress_mask = segment_dress(img_np)
104
-
105
- if dress_mask is None:
106
- return img # No dress detected
107
 
108
  # Further refine mask with GrabCut
109
  dress_mask = apply_grabcut(img_np, dress_mask)
110
-
111
- # Convert the selected color to BGR
112
- color_map = {
113
- "Red": (0, 0, 255), "Blue": (255, 0, 0), "Green": (0, 255, 0), "Yellow": (0, 255, 255),
114
- "Purple": (128, 0, 128), "Orange": (0, 165, 255), "Cyan": (255, 255, 0), "Magenta": (255, 0, 255),
115
- "White": (255, 255, 255), "Black": (0, 0, 0)
116
- }
117
- new_color_bgr = np.array(color_map.get(color, (0, 0, 255)), dtype=np.uint8)
118
-
119
  # Apply recoloring with blending
120
  img_recolored = recolor_dress(img_np, dress_mask, new_color_bgr)
121
 
122
  return Image.fromarray(img_recolored)
123
 
124
- # Gradio Interface
125
- demo = gr.Interface(
126
- fn=change_dress_color,
127
- inputs=[
128
- gr.Image(type = "pil", label="Upload Dress Image"),
129
- gr.Dropdown(choices=["Red", "Blue", "Green", "Yellow", "Purple", "Orange", "Cyan", "Magenta", "White", "Black"],
130
- label="Choose New Dress Color",
131
- value="Red")
132
- ],
133
- outputs=gr.Image(type="pil", label="Color Changed Dress"),
134
- title="AI-Powered Dress Color Changer",
135
- description="Upload an image of a dress and select a new color. The AI will change the dress color naturally while keeping the fabric texture."
136
- )
 
 
 
 
 
 
 
 
 
 
 
137
 
138
  if __name__ == "__main__":
139
  demo.launch()
 
97
  if img is None:
98
  return None
99
 
100
+ # Convert color name to BGR using a safer method
101
+ color_map = {
102
+ "Red": (0, 0, 255), "Blue": (255, 0, 0), "Green": (0, 255, 0),
103
+ "Yellow": (0, 255, 255), "Purple": (128, 0, 128), "Orange": (0, 165, 255),
104
+ "Cyan": (255, 255, 0), "Magenta": (255, 0, 255), "White": (255, 255, 255),
105
+ "Black": (0, 0, 0)
106
+ }
107
+
108
+ # Safely get color with fallback to red
109
+ new_color_bgr = color_map.get(color, (0, 0, 255))
110
 
111
+ img_np = np.array(img)
112
+
113
  # Get dress segmentation mask
114
  dress_mask = segment_dress(img_np)
115
+
116
+ if dress_mask is None or np.sum(dress_mask) == 0:
117
+ return img # Return original if no mask found
118
 
119
  # Further refine mask with GrabCut
120
  dress_mask = apply_grabcut(img_np, dress_mask)
121
+
 
 
 
 
 
 
 
 
122
  # Apply recoloring with blending
123
  img_recolored = recolor_dress(img_np, dress_mask, new_color_bgr)
124
 
125
  return Image.fromarray(img_recolored)
126
 
127
+ # Create Gradio Blocks interface instead of simple Interface
128
+ with gr.Blocks() as demo:
129
+ gr.Markdown("# AI-Powered Dress Color Changer")
130
+ gr.Markdown("Upload an image of a dress and select a new color. The AI will change the dress color naturally while keeping the fabric texture.")
131
+
132
+ with gr.Row():
133
+ with gr.Column():
134
+ input_image = gr.Image(type="pil", label="Upload Dress Image")
135
+ color_choice = gr.Dropdown(
136
+ choices=["Red", "Blue", "Green", "Yellow", "Purple",
137
+ "Orange", "Cyan", "Magenta", "White", "Black"],
138
+ value="Red",
139
+ label="Choose New Dress Color"
140
+ )
141
+ submit_btn = gr.Button("Change Color")
142
+
143
+ with gr.Column():
144
+ output_image = gr.Image(type="pil", label="Result")
145
+
146
+ submit_btn.click(
147
+ fn=change_dress_color,
148
+ inputs=[input_image, color_choice],
149
+ outputs=output_image
150
+ )
151
 
152
  if __name__ == "__main__":
153
  demo.launch()