|
import gradio as gr |
|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
import matplotlib.colors as mcolors |
|
from gradio_client import Client, handle_file |
|
from PIL import Image |
|
import requests |
|
from io import BytesIO |
|
import cv2 |
|
|
|
def get_segmentation_mask(image_url): |
|
client = Client("facebook/sapiens-seg") |
|
result = client.predict(image=handle_file(image_url), model_name="1b", api_name="/process_image") |
|
return np.load(result[1]) |
|
|
|
|
|
def process_image(image, categories_to_hide): |
|
|
|
image = Image.open(image.name).convert("RGBA") |
|
|
|
|
|
image.save("temp_image.png") |
|
mask_data = get_segmentation_mask("temp_image.png") |
|
|
|
|
|
grouped_mapping = { |
|
"Background": [0], |
|
"Clothes": [1, 12, 22, 8, 9, 17, 18], |
|
"Face": [2, 23, 24, 25, 26, 27], |
|
"Hair": [3], |
|
"Skin": [4, 5, 6, 7, 10, 11, 13, 14, 15, 16, 19, 20, 21] |
|
} |
|
|
|
|
|
image_array = np.array(image, dtype=np.uint8) |
|
|
|
|
|
transparent_image = np.zeros_like(image_array, dtype=np.uint8) |
|
|
|
|
|
mask_combined = np.zeros_like(mask_data, dtype=bool) |
|
|
|
for category in categories_to_hide: |
|
for idx in grouped_mapping.get(category, []): |
|
mask_combined |= (mask_data == idx) |
|
|
|
|
|
if "Clothes" in categories_to_hide: |
|
clothing_mask = np.isin(mask_data, grouped_mapping["Clothes"]).astype(np.uint8) |
|
|
|
|
|
height, width = clothing_mask.shape |
|
kernel_size = max(20, int(0.02 * min(height, width))) |
|
kernel = np.ones((kernel_size, kernel_size), np.uint8) |
|
|
|
|
|
closed_clothing_mask = cv2.morphologyEx(clothing_mask, cv2.MORPH_CLOSE, kernel, iterations=1) |
|
|
|
|
|
dilated_clothing_mask = cv2.dilate(closed_clothing_mask, kernel, iterations=1) |
|
|
|
|
|
mask_combined |= (dilated_clothing_mask == 1) |
|
|
|
|
|
|
|
transparent_image[mask_combined] = image_array[mask_combined] |
|
|
|
|
|
result_image = Image.fromarray(transparent_image, mode="RGBA") |
|
|
|
return result_image |
|
|
|
|
|
demo = gr.Interface( |
|
fn=process_image, |
|
inputs=[ |
|
gr.File(label="Upload an Image"), |
|
gr.CheckboxGroup([ |
|
"Background", "Clothes", "Face", "Hair", "Skin" |
|
], label="Select Categories to Preserve") |
|
], |
|
outputs=gr.Image(label="Masked Image", type="pil"), |
|
title="Segmentation Mask Editor", |
|
description="Upload an image, generate a segmentation mask, and select categories to preserve while making the rest transparent." |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|