gaur3009 commited on
Commit
65014ef
·
verified ·
1 Parent(s): 9582f0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -3
app.py CHANGED
@@ -41,7 +41,7 @@ def segment_dress(image_np):
41
  return mask
42
 
43
  def change_dress_color(image_path, color):
44
- """Change the dress color based on the detected dress mask."""
45
  if image_path is None:
46
  return None
47
 
@@ -66,13 +66,16 @@ def change_dress_color(image_path, color):
66
  img_lab = cv2.cvtColor(img_np, cv2.COLOR_RGB2LAB)
67
  new_color_lab = cv2.cvtColor(np.uint8([[new_color_bgr]]), cv2.COLOR_BGR2LAB)[0][0]
68
 
69
- # Apply the new color while preserving texture
70
  img_lab[..., 1] = np.where(mask == 255, new_color_lab[1], img_lab[..., 1]) # Modify A-channel
71
  img_lab[..., 2] = np.where(mask == 255, new_color_lab[2], img_lab[..., 2]) # Modify B-channel
72
 
73
  # Convert back to RGB
74
  img_recolored = cv2.cvtColor(img_lab, cv2.COLOR_LAB2RGB)
75
 
 
 
 
76
  return Image.fromarray(img_recolored)
77
 
78
  # Gradio Interface
@@ -84,7 +87,7 @@ demo = gr.Interface(
84
  ],
85
  outputs=gr.Image(type="pil", label="Color Changed Dress"),
86
  title="Dress Color Changer",
87
- description="Upload an image of a dress and select a new color to change its appearance."
88
  )
89
 
90
  if __name__ == "__main__":
 
41
  return mask
42
 
43
  def change_dress_color(image_path, color):
44
+ """Change the dress color naturally while keeping textures."""
45
  if image_path is None:
46
  return None
47
 
 
66
  img_lab = cv2.cvtColor(img_np, cv2.COLOR_RGB2LAB)
67
  new_color_lab = cv2.cvtColor(np.uint8([[new_color_bgr]]), cv2.COLOR_BGR2LAB)[0][0]
68
 
69
+ # Preserve texture by only modifying the A & B channels
70
  img_lab[..., 1] = np.where(mask == 255, new_color_lab[1], img_lab[..., 1]) # Modify A-channel
71
  img_lab[..., 2] = np.where(mask == 255, new_color_lab[2], img_lab[..., 2]) # Modify B-channel
72
 
73
  # Convert back to RGB
74
  img_recolored = cv2.cvtColor(img_lab, cv2.COLOR_LAB2RGB)
75
 
76
+ # Apply Poisson blending for realistic color application
77
+ img_recolored = cv2.seamlessClone(img_recolored, img_np, mask, (img_np.shape[1]//2, img_np.shape[0]//2), cv2.NORMAL_CLONE)
78
+
79
  return Image.fromarray(img_recolored)
80
 
81
  # Gradio Interface
 
87
  ],
88
  outputs=gr.Image(type="pil", label="Color Changed Dress"),
89
  title="Dress Color Changer",
90
+ description="Upload an image of a dress and select a new color to change its appearance naturally."
91
  )
92
 
93
  if __name__ == "__main__":