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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -11
app.py CHANGED
@@ -33,10 +33,11 @@ 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
- # 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
 
@@ -66,17 +67,22 @@ 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
- # 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(
 
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
 
 
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(