gaur3009 commited on
Commit
e49358d
·
verified ·
1 Parent(s): fa3d84c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -22
app.py CHANGED
@@ -7,12 +7,12 @@ from torchvision import transforms
7
  from cloth_segmentation.networks.u2net import U2NET # Import U²-Net
8
 
9
  # Load U²-Net model
10
- model_path = "cloth_segmentation/networks/u2net.pth"
11
  model = U2NET(3, 1)
12
 
13
  # Load the state dictionary
14
  state_dict = torch.load(model_path, map_location=torch.device('cpu'))
15
- state_dict = {k.replace('module.', ''): v for k, v in state_dict.items()}
16
  model.load_state_dict(state_dict)
17
  model.eval()
18
 
@@ -33,11 +33,10 @@ 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 mask using morphological operations
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=2) # Expand the detected dress area
40
- mask = cv2.GaussianBlur(mask, (5, 5), 0) # Smooth edges
41
 
42
  return mask
43
 
@@ -53,28 +52,29 @@ def change_dress_color(image_path, color):
53
  if mask is None:
54
  return img # No dress detected
55
 
56
- # Convert image to HSV for color modification
57
- img_hsv = cv2.cvtColor(img_np, cv2.COLOR_RGB2HSV)
58
-
59
- # Define new color in HSV (only modifying the Hue)
60
  color_map = {
61
- "Red": 0, # Hue value for Red
62
- "Blue": 120, # Hue value for Blue
63
- "Green": 60, # Hue value for Green
64
- "Yellow": 30, # Hue value for Yellow
65
- "Purple": 150 # Hue value for Purple
66
  }
67
- new_hue = color_map.get(color, 0)
68
-
69
- # Modify only the Hue channel where the mask is applied
70
- img_hsv[..., 0] = np.where(mask > 128, new_hue, img_hsv[..., 0])
 
 
 
 
 
71
 
72
  # Convert back to RGB
73
- img_recolored = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2RGB)
74
 
75
- # Apply Poisson blending for natural integration
76
- center = (img_np.shape[1] // 2, img_np.shape[0] // 2)
77
- img_recolored = cv2.seamlessClone(img_recolored, img_np, mask, center, cv2.MIXED_CLONE)
78
 
79
  return Image.fromarray(img_recolored)
80
 
 
7
  from cloth_segmentation.networks.u2net import U2NET # Import U²-Net
8
 
9
  # Load U²-Net model
10
+ model_path = "cloth_segmentation/networks/u2net.pth" # Ensure this path is correct
11
  model = U2NET(3, 1)
12
 
13
  # Load the state dictionary
14
  state_dict = torch.load(model_path, map_location=torch.device('cpu'))
15
+ state_dict = {k.replace('module.', ''): v for k, v in state_dict.items()} # Remove 'module.' prefix
16
  model.load_state_dict(state_dict)
17
  model.eval()
18
 
 
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
  return mask
42
 
 
52
  if mask is None:
53
  return img # No dress detected
54
 
55
+ # Convert the selected color to BGR
 
 
 
56
  color_map = {
57
+ "Red": (0, 0, 255),
58
+ "Blue": (255, 0, 0),
59
+ "Green": (0, 255, 0),
60
+ "Yellow": (0, 255, 255),
61
+ "Purple": (128, 0, 128)
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