Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import matplotlib.colors as mcolors
|
| 5 |
+
|
| 6 |
+
def process_mask(file, category_to_hide):
|
| 7 |
+
# Load the .npy file
|
| 8 |
+
data = np.load(file.name)
|
| 9 |
+
|
| 10 |
+
# Define grouped categories
|
| 11 |
+
grouped_mapping = {
|
| 12 |
+
"Background": [0],
|
| 13 |
+
"Clothes": [1, 12, 22, 8, 9, 17, 18], # Includes Shoes, Socks, Slippers
|
| 14 |
+
"Face": [2, 23, 24, 25, 26, 27], # Face Neck, Lips, Teeth, Tongue
|
| 15 |
+
"Hair": [3], # Hair
|
| 16 |
+
"Skin (Hands, Feet, Body)": [4, 5, 6, 7, 10, 11, 13, 14, 15, 16, 19, 20, 21] # Hands, Feet, Arms, Legs, Torso
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
# Assign colors for the categories
|
| 20 |
+
group_colors = {
|
| 21 |
+
"Background": "black",
|
| 22 |
+
"Clothes": "magenta",
|
| 23 |
+
"Face": "orange",
|
| 24 |
+
"Hair": "brown",
|
| 25 |
+
"Skin (Hands, Feet, Body)": "cyan"
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
# Create a new mask with grouped categories
|
| 29 |
+
grouped_mask = np.zeros((*data.shape, 3), dtype=np.uint8)
|
| 30 |
+
|
| 31 |
+
for category, indices in grouped_mapping.items():
|
| 32 |
+
if category == category_to_hide:
|
| 33 |
+
continue # Skip applying colors for the selected category to hide
|
| 34 |
+
for idx in indices:
|
| 35 |
+
mask = data == idx
|
| 36 |
+
rgb = mcolors.to_rgb(group_colors[category]) # Convert color to RGB
|
| 37 |
+
grouped_mask[mask] = [int(c * 255) for c in rgb]
|
| 38 |
+
|
| 39 |
+
# Save the mask image
|
| 40 |
+
fig, ax = plt.subplots(figsize=(6, 6))
|
| 41 |
+
ax.imshow(grouped_mask)
|
| 42 |
+
ax.axis("off")
|
| 43 |
+
plt.tight_layout()
|
| 44 |
+
|
| 45 |
+
# Save to file for Gradio output
|
| 46 |
+
output_path = "output_mask.png"
|
| 47 |
+
plt.savefig(output_path, bbox_inches='tight', pad_inches=0)
|
| 48 |
+
plt.close()
|
| 49 |
+
|
| 50 |
+
return output_path
|
| 51 |
+
|
| 52 |
+
# Define Gradio Interface
|
| 53 |
+
demo = gr.Interface(
|
| 54 |
+
fn=process_mask,
|
| 55 |
+
inputs=[
|
| 56 |
+
gr.File(label="Upload .npy Segmentation File"),
|
| 57 |
+
gr.Radio([
|
| 58 |
+
"Background", "Clothes", "Face", "Hair", "Skin (Hands, Feet, Body)"
|
| 59 |
+
], label="Select Category to Hide")
|
| 60 |
+
],
|
| 61 |
+
outputs=gr.Image(label="Modified Segmentation Mask"),
|
| 62 |
+
title="Segmentation Mask Editor",
|
| 63 |
+
description="Upload a .npy segmentation file and select a category to mask (hide with black)."
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
demo.launch()
|