Spaces:
Runtime error
Runtime error
Update app.py
Browse files35 unmodified lines36373839404142434445464748495012 unmodified lines636465666768694 unmodified lines7475767778798020 unmodified lines1011021031041051061071081091101112 unmodified lines11411511611711811912026 unmodified lines14714814915015115215315415535 unmodified lines"""def remove_mask_background(mask_img):"""Remove white/transparent background from mask image"""mask_img = mask_img.convert("RGBA")datas = mask_img.getdata()new_data = []for item in datas:if (item[0] > 240 and item[1] > 240 and item[2] > 240) or item[3] == 0:new_data.append((255, 255, 255, 0))else:new_data.append(item)mask_img.putdata(new_data)return mask_img12 unmodified linesreturn random.choice(compliments)def apply_mask(person_img, mask_img, seed, randomize_seed):"""Improved mask application using facial landmarks"""if person_img is None or mask_img is None:return None, None, "Empty image"4 unmodified lines# Convert to numpy arrayimg_np = np.array(person_img)# Process the maskprocessed_mask = remove_mask_background(mask_img)# Detect facial landmarks20 unmodified linestop_left_x = center_x - scale_w // 2top_left_y = center_y - scale_h // 2# Composite images with transparencybackground_pil = Image.fromarray(img_np).convert("RGBA")background_pil.paste(resized_mask, (top_left_x, top_left_y), resized_mask)return background_pil.convert("RGB"), seed, get_random_compliment()return person_img, seed, "Couldn't apply mask properly"2 unmodified linesreturn person_img, seed, f"Error: {e}"def capture_and_process(mask_img, seed, randomize_seed):"""Capture from webcam and apply mask"""temp_file = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)temp_path = temp_file.name26 unmodified lineswith gr.Column(elem_id="col-mid"):gr.Markdown("### Step 2. Upload a mask image")mask_img_input = gr.Image(label="Mask image", sources='upload', type="pil")mask_img_example = gr.Examples(examples=["mask1.jpg", "mask2.jpg", "mask3.jpeg"],inputs=mask_img_input,examples_per_page=12)35 unmodified lines363738394041424344454647484950515253545512 unmodified lines686970717273744 unmodified lines7980818283848520 unmodified lines1061071081091101111121131141151161171181191201211221232 unmodified lines12612712812913013113226 unmodified lines15916016116216316416516616735 unmodified lines"""def remove_mask_background(mask_img):"""Remove all backgrounds (white, black, transparent) from mask image"""mask_img = mask_img.convert("RGBA")datas = mask_img.getdata()new_data = []for item in datas:# Remove white (RGB > 240), black (RGB < 15), and existing transparent pixelsif (item[0] > 240 and item[1] > 240 and item[2] > 240) or \(item[0] < 15 and item[1] < 15 and item[2] < 15) or \(item[3] == 0):new_data.append((255, 255, 255, 0)) # Fully transparentelse:new_data.append(item)mask_img.putdata(new_data)return mask_img12 unmodified linesreturn random.choice(compliments)def apply_mask(person_img, mask_img, seed, randomize_seed):"""Improved mask application using facial landmarks with perfect transparency"""if person_img is None or mask_img is None:return None, None, "Empty image"4 unmodified lines# Convert to numpy arrayimg_np = np.array(person_img)# Process the mask with aggressive background removalprocessed_mask = remove_mask_background(mask_img)# Detect facial landmarks20 unmodified linestop_left_x = center_x - scale_w // 2top_left_y = center_y - scale_h // 2# Create new RGBA images for perfect transparency handlingbackground_pil = Image.fromarray(cv2.cvtColor(img_np, cv2.COLOR_RGB2RGBA))resized_mask = resized_mask.convert("RGBA")# Create transparent result imageresult_img = Image.new("RGBA", background_pil.size)result_img.paste(background_pil, (0, 0))# Paste mask with proper alpha channelresult_img.paste(resized_mask, (top_left_x, top_left_y), resized_mask)return result_img.convert("RGB"), seed, get_random_compliment()return person_img, seed, "Couldn't apply mask properly"2 unmodified linesreturn person_img, seed, f"Error: {e}"def capture_and_process(mask_img, seed, randomize_seed):"""Capture from webcam and apply mask with transparency"""temp_file = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)temp_path = temp_file.name26 unmodified lineswith gr.Column(elem_id="col-mid"):gr.Markdown("### Step 2. Upload a mask image")mask_img_input = gr.Image(label="Mask image (PNG with transparency)", type="pil")mask_img_example = gr.Examples(examples=["mask1.png", "mask2.png", "mask3.png"],inputs=mask_img_input,examples_per_page=12)
app.py+24-12