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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -9
app.py CHANGED
@@ -34,17 +34,20 @@ def segment_dress(image_np):
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
 
 
 
 
 
 
 
46
  def change_dress_color(image_path, color):
47
- """Change the dress color naturally while keeping textures."""
48
  if image_path is None:
49
  return None
50
 
@@ -59,7 +62,7 @@ def change_dress_color(image_path, color):
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,7 +70,11 @@ 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
- # 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])
@@ -75,6 +82,9 @@ def change_dress_color(image_path, color):
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
@@ -82,7 +92,7 @@ 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",
 
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((5, 5), np.uint8)
38
  mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) # Close small gaps
39
+ mask = cv2.GaussianBlur(mask, (21, 21), 10) # Smooth edges for natural blending
 
 
 
40
 
41
  return mask
42
 
43
+ def get_ambient_light(img_np):
44
+ """Estimate ambient lighting from non-dress areas for realistic blending."""
45
+ lab = cv2.cvtColor(img_np, cv2.COLOR_RGB2LAB)
46
+ L_channel = lab[:, :, 0] # Lightness channel
47
+ return np.median(L_channel) # Median light level in the image
48
+
49
  def change_dress_color(image_path, color):
50
+ """Change the dress color naturally while keeping textures and adjusting to lighting."""
51
  if image_path is None:
52
  return None
53
 
 
62
  color_map = {
63
  "Red": (0, 0, 255), "Blue": (255, 0, 0), "Green": (0, 255, 0), "Yellow": (0, 255, 255),
64
  "Purple": (128, 0, 128), "Orange": (0, 165, 255), "Cyan": (255, 255, 0), "Magenta": (255, 0, 255),
65
+ "White": (255, 255, 255), "Black": (0, 0, 0)
66
  }
67
  new_color_bgr = np.array(color_map.get(color, (0, 0, 255)), dtype=np.uint8) # Default to Red
68
 
 
70
  img_lab = cv2.cvtColor(img_np, cv2.COLOR_RGB2LAB)
71
  new_color_lab = cv2.cvtColor(np.uint8([[new_color_bgr]]), cv2.COLOR_BGR2LAB)[0][0]
72
 
73
+ # Adjust color to match ambient lighting
74
+ ambient_light = get_ambient_light(img_np)
75
+ img_lab[..., 0] = np.clip(img_lab[..., 0] * (ambient_light / 128), 0, 255) # Normalize lighting
76
+
77
+ # Preserve texture by modifying only A & B channels
78
  blend_factor = 0.6 # Controls intensity of color change
79
  img_lab[..., 1] = np.where(mask > 128, img_lab[..., 1] * (1 - blend_factor) + new_color_lab[1] * blend_factor, img_lab[..., 1])
80
  img_lab[..., 2] = np.where(mask > 128, img_lab[..., 2] * (1 - blend_factor) + new_color_lab[2] * blend_factor, img_lab[..., 2])
 
82
  # Convert back to RGB
83
  img_recolored = cv2.cvtColor(img_lab, cv2.COLOR_LAB2RGB)
84
 
85
+ # Use Poisson blending for seamless integration with the environment
86
+ img_recolored = cv2.seamlessClone(img_recolored, img_np, mask, (img_np.shape[1]//2, img_np.shape[0]//2), cv2.MIXED_CLONE)
87
+
88
  return Image.fromarray(img_recolored)
89
 
90
  # Gradio Interface
 
92
  fn=change_dress_color,
93
  inputs=[
94
  gr.Image(type="filepath", label="Upload Dress Image"),
95
+ gr.Radio(["Red", "Blue", "Green", "Yellow", "Purple", "Orange", "Cyan", "Magenta", "White", "Black"], label="Choose New Dress Color")
96
  ],
97
  outputs=gr.Image(type="pil", label="Color Changed Dress"),
98
  title="Dress Color Changer",