RICHERGIRL commited on
Commit
18799a1
·
verified ·
1 Parent(s): 81e3b01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -12
app.py CHANGED
@@ -36,15 +36,20 @@ css = """
36
  """
37
 
38
  def remove_mask_background(mask_img):
39
- """Remove white/transparent background from mask image"""
40
  mask_img = mask_img.convert("RGBA")
41
  datas = mask_img.getdata()
42
  new_data = []
 
43
  for item in datas:
44
- if (item[0] > 240 and item[1] > 240 and item[2] > 240) or item[3] == 0:
45
- new_data.append((255, 255, 255, 0))
 
 
 
46
  else:
47
  new_data.append(item)
 
48
  mask_img.putdata(new_data)
49
  return mask_img
50
 
@@ -63,7 +68,7 @@ def get_random_compliment():
63
  return random.choice(compliments)
64
 
65
  def apply_mask(person_img, mask_img, seed, randomize_seed):
66
- """Improved mask application using facial landmarks"""
67
  if person_img is None or mask_img is None:
68
  return None, None, "Empty image"
69
 
@@ -74,7 +79,7 @@ def apply_mask(person_img, mask_img, seed, randomize_seed):
74
  # Convert to numpy array
75
  img_np = np.array(person_img)
76
 
77
- # Process the mask
78
  processed_mask = remove_mask_background(mask_img)
79
 
80
  # Detect facial landmarks
@@ -101,11 +106,18 @@ def apply_mask(person_img, mask_img, seed, randomize_seed):
101
  top_left_x = center_x - scale_w // 2
102
  top_left_y = center_y - scale_h // 2
103
 
104
- # Composite images with transparency
105
- background_pil = Image.fromarray(img_np).convert("RGBA")
106
- background_pil.paste(resized_mask, (top_left_x, top_left_y), resized_mask)
 
 
 
 
 
 
 
107
 
108
- return background_pil.convert("RGB"), seed, get_random_compliment()
109
 
110
  return person_img, seed, "Couldn't apply mask properly"
111
 
@@ -114,7 +126,7 @@ def apply_mask(person_img, mask_img, seed, randomize_seed):
114
  return person_img, seed, f"Error: {e}"
115
 
116
  def capture_and_process(mask_img, seed, randomize_seed):
117
- """Capture from webcam and apply mask"""
118
  temp_file = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
119
  temp_path = temp_file.name
120
 
@@ -147,9 +159,9 @@ with gr.Blocks(css=css) as MaskSuggest:
147
 
148
  with gr.Column(elem_id="col-mid"):
149
  gr.Markdown("### Step 2. Upload a mask image")
150
- mask_img_input = gr.Image(label="Mask image", sources='upload', type="pil")
151
  mask_img_example = gr.Examples(
152
- examples=["mask1.jpg", "mask2.jpg", "mask3.jpeg"],
153
  inputs=mask_img_input,
154
  examples_per_page=12
155
  )
 
36
  """
37
 
38
  def remove_mask_background(mask_img):
39
+ """Remove all backgrounds (white, black, transparent) from mask image"""
40
  mask_img = mask_img.convert("RGBA")
41
  datas = mask_img.getdata()
42
  new_data = []
43
+
44
  for item in datas:
45
+ # Remove white (RGB > 240), black (RGB < 15), and existing transparent pixels
46
+ if (item[0] > 240 and item[1] > 240 and item[2] > 240) or \
47
+ (item[0] < 15 and item[1] < 15 and item[2] < 15) or \
48
+ (item[3] == 0):
49
+ new_data.append((255, 255, 255, 0)) # Fully transparent
50
  else:
51
  new_data.append(item)
52
+
53
  mask_img.putdata(new_data)
54
  return mask_img
55
 
 
68
  return random.choice(compliments)
69
 
70
  def apply_mask(person_img, mask_img, seed, randomize_seed):
71
+ """Improved mask application using facial landmarks with perfect transparency"""
72
  if person_img is None or mask_img is None:
73
  return None, None, "Empty image"
74
 
 
79
  # Convert to numpy array
80
  img_np = np.array(person_img)
81
 
82
+ # Process the mask with aggressive background removal
83
  processed_mask = remove_mask_background(mask_img)
84
 
85
  # Detect facial landmarks
 
106
  top_left_x = center_x - scale_w // 2
107
  top_left_y = center_y - scale_h // 2
108
 
109
+ # Create new RGBA images for perfect transparency handling
110
+ background_pil = Image.fromarray(cv2.cvtColor(img_np, cv2.COLOR_RGB2RGBA))
111
+ resized_mask = resized_mask.convert("RGBA")
112
+
113
+ # Create transparent result image
114
+ result_img = Image.new("RGBA", background_pil.size)
115
+ result_img.paste(background_pil, (0, 0))
116
+
117
+ # Paste mask with proper alpha channel
118
+ result_img.paste(resized_mask, (top_left_x, top_left_y), resized_mask)
119
 
120
+ return result_img.convert("RGB"), seed, get_random_compliment()
121
 
122
  return person_img, seed, "Couldn't apply mask properly"
123
 
 
126
  return person_img, seed, f"Error: {e}"
127
 
128
  def capture_and_process(mask_img, seed, randomize_seed):
129
+ """Capture from webcam and apply mask with transparency"""
130
  temp_file = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
131
  temp_path = temp_file.name
132
 
 
159
 
160
  with gr.Column(elem_id="col-mid"):
161
  gr.Markdown("### Step 2. Upload a mask image")
162
+ mask_img_input = gr.Image(label="Mask image (PNG with transparency)", type="pil")
163
  mask_img_example = gr.Examples(
164
+ examples=["mask1.png", "mask2.png", "mask3.png"],
165
  inputs=mask_img_input,
166
  examples_per_page=12
167
  )