gaur3009 commited on
Commit
1f7ad23
·
verified ·
1 Parent(s): b8fc2e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -26
app.py CHANGED
@@ -33,11 +33,13 @@ def segment_dress(image_np):
33
  # Resize mask to original image size
34
  mask = cv2.resize(mask, (image_np.shape[1], image_np.shape[0]), interpolation=cv2.INTER_NEAREST)
35
 
36
- # Refine the mask to reduce noise
37
- kernel = np.ones((5, 5), np.uint8)
38
  mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) # Close small gaps
39
- mask = cv2.dilate(mask, kernel, iterations=1) # Expand the detected dress area
40
- mask = cv2.medianBlur(mask, 5) # Reduce noise and smooth edges
 
 
41
 
42
  return mask
43
 
@@ -49,17 +51,15 @@ def change_dress_color(image_path, color):
49
  img = Image.open(image_path).convert("RGB")
50
  img_np = np.array(img)
51
  mask = segment_dress(img_np)
52
-
53
  if mask is None:
54
  return img # No dress detected
55
 
56
  # Convert the selected color to BGR
57
  color_map = {
58
- "Red": (0, 0, 255),
59
- "Blue": (255, 0, 0),
60
- "Green": (0, 255, 0),
61
- "Yellow": (0, 255, 255),
62
- "Purple": (128, 0, 128)
63
  }
64
  new_color_bgr = np.array(color_map.get(color, (0, 0, 255)), dtype=np.uint8) # Default to Red
65
 
@@ -67,29 +67,22 @@ def change_dress_color(image_path, color):
67
  img_lab = cv2.cvtColor(img_np, cv2.COLOR_RGB2LAB)
68
  new_color_lab = cv2.cvtColor(np.uint8([[new_color_bgr]]), cv2.COLOR_BGR2LAB)[0][0]
69
 
70
- # Adaptive color blending - preserves details and avoids over-saturation
71
- l_channel, a_channel, b_channel = cv2.split(img_lab)
72
-
73
- a_channel = np.where(mask == 255, new_color_lab[1], a_channel) # Modify A-channel
74
- b_channel = np.where(mask == 255, new_color_lab[2], b_channel) # Modify B-channel
75
-
76
- img_recolored = cv2.merge([l_channel, a_channel, b_channel])
77
-
78
- # Convert back to RGB
79
- img_recolored = cv2.cvtColor(img_recolored, cv2.COLOR_LAB2RGB)
80
-
81
- # Apply Poisson blending for natural-looking color transition
82
- center = (img_np.shape[1]//2, img_np.shape[0]//2)
83
- img_final = cv2.seamlessClone(img_recolored, img_np, mask, center, cv2.NORMAL_CLONE)
84
 
85
- return Image.fromarray(img_final)
 
 
 
86
 
87
  # Gradio Interface
88
  demo = gr.Interface(
89
  fn=change_dress_color,
90
  inputs=[
91
  gr.Image(type="filepath", label="Upload Dress Image"),
92
- gr.Radio(["Red", "Blue", "Green", "Yellow", "Purple"], label="Choose New Dress Color")
93
  ],
94
  outputs=gr.Image(type="pil", label="Color Changed Dress"),
95
  title="Dress Color Changer",
 
33
  # Resize mask to original image size
34
  mask = cv2.resize(mask, (image_np.shape[1], image_np.shape[0]), interpolation=cv2.INTER_NEAREST)
35
 
36
+ # Apply morphological operations for better segmentation
37
+ kernel = np.ones((7, 7), np.uint8)
38
  mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) # Close small gaps
39
+ mask = cv2.dilate(mask, kernel, iterations=2) # Expand the detected dress area
40
+
41
+ # Apply Gaussian blur for smoother transitions
42
+ mask = cv2.GaussianBlur(mask, (15, 15), 5)
43
 
44
  return mask
45
 
 
51
  img = Image.open(image_path).convert("RGB")
52
  img_np = np.array(img)
53
  mask = segment_dress(img_np)
54
+
55
  if mask is None:
56
  return img # No dress detected
57
 
58
  # Convert the selected color to BGR
59
  color_map = {
60
+ "Red": (0, 0, 255), "Blue": (255, 0, 0), "Green": (0, 255, 0), "Yellow": (0, 255, 255),
61
+ "Purple": (128, 0, 128), "Orange": (0, 165, 255), "Cyan": (255, 255, 0), "Magenta": (255, 0, 255),
62
+ "White": (255, 255, 255)
 
 
63
  }
64
  new_color_bgr = np.array(color_map.get(color, (0, 0, 255)), dtype=np.uint8) # Default to Red
65
 
 
67
  img_lab = cv2.cvtColor(img_np, cv2.COLOR_RGB2LAB)
68
  new_color_lab = cv2.cvtColor(np.uint8([[new_color_bgr]]), cv2.COLOR_BGR2LAB)[0][0]
69
 
70
+ # Preserve texture by modifying A & B channels with blend factor
71
+ blend_factor = 0.6 # Controls intensity of color change
72
+ img_lab[..., 1] = np.where(mask > 128, img_lab[..., 1] * (1 - blend_factor) + new_color_lab[1] * blend_factor, img_lab[..., 1])
73
+ img_lab[..., 2] = np.where(mask > 128, img_lab[..., 2] * (1 - blend_factor) + new_color_lab[2] * blend_factor, img_lab[..., 2])
 
 
 
 
 
 
 
 
 
 
74
 
75
+ # Convert back to RGB
76
+ img_recolored = cv2.cvtColor(img_lab, cv2.COLOR_LAB2RGB)
77
+
78
+ return Image.fromarray(img_recolored)
79
 
80
  # Gradio Interface
81
  demo = gr.Interface(
82
  fn=change_dress_color,
83
  inputs=[
84
  gr.Image(type="filepath", label="Upload Dress Image"),
85
+ gr.Radio(["Red", "Blue", "Green", "Yellow", "Purple", "Orange", "Cyan", "Magenta", "White"], label="Choose New Dress Color")
86
  ],
87
  outputs=gr.Image(type="pil", label="Color Changed Dress"),
88
  title="Dress Color Changer",