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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -12
app.py CHANGED
@@ -38,6 +38,9 @@ def segment_dress(image_np):
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
  return mask
42
 
43
  def change_dress_color(image_path, color):
@@ -62,23 +65,20 @@ def change_dress_color(image_path, color):
62
  }
63
  new_color_bgr = np.array(color_map.get(color, (0, 0, 255)), dtype=np.uint8) # Default to Red
64
 
65
- # Convert image to LAB color space for better blending
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
82
  demo = gr.Interface(
83
  fn=change_dress_color,
84
  inputs=[
 
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 to smooth mask edges
42
+ mask = cv2.GaussianBlur(mask, (5, 5), 0)
43
+
44
  return mask
45
 
46
  def change_dress_color(image_path, color):
 
65
  }
66
  new_color_bgr = np.array(color_map.get(color, (0, 0, 255)), dtype=np.uint8) # Default to Red
67
 
68
+ # Convert image to HSV for a more natural recoloring
69
+ img_hsv = cv2.cvtColor(img_np, cv2.COLOR_RGB2HSV)
70
+ target_hue = cv2.cvtColor(np.uint8([[new_color_bgr]]), cv2.COLOR_BGR2HSV)[0][0][0] # Extract Hue
71
+
72
+ # Replace Hue where the mask is active
73
+ img_hsv[..., 0] = np.where(mask == 255, target_hue, img_hsv[..., 0])
74
 
 
 
 
 
75
  # Convert back to RGB
76
+ img_recolored = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2RGB)
77
+
78
+ img_recolored = cv2.seamlessClone(img_recolored, img_np, mask, (img_np.shape[1]//2, img_np.shape[0]//2), cv2.MIXED_CLONE)
 
79
 
80
  return Image.fromarray(img_recolored)
81
 
 
82
  demo = gr.Interface(
83
  fn=change_dress_color,
84
  inputs=[