Update app.py
Browse files
app.py
CHANGED
@@ -12,11 +12,12 @@ def get_segmentation_mask(image_url):
|
|
12 |
result = client.predict(image=handle_file(image_url), model_name="1b", api_name="/process_image")
|
13 |
return np.load(result[1]) # Result[1] contains the .npy mask
|
14 |
|
|
|
15 |
def process_image(image, categories_to_hide):
|
16 |
# Convert uploaded image to a PIL Image
|
17 |
image = Image.open(image.name).convert("RGBA")
|
18 |
|
19 |
-
# Save temporarily and get the mask
|
20 |
image.save("temp_image.png")
|
21 |
mask_data = get_segmentation_mask("temp_image.png")
|
22 |
|
@@ -24,7 +25,7 @@ def process_image(image, categories_to_hide):
|
|
24 |
grouped_mapping = {
|
25 |
"Background": [0],
|
26 |
"Clothes": [1, 12, 22, 8, 9, 17, 18], # Includes Shoes, Socks, Slippers
|
27 |
-
"Face": [2, 23, 24, 25, 26, 27], # Face Neck, Lips, Teeth, Tongue
|
28 |
"Hair": [3], # Hair
|
29 |
"Skin": [4, 5, 6, 7, 10, 11, 13, 14, 15, 16, 19, 20, 21] # Hands, Feet, Arms, Legs, Torso
|
30 |
}
|
@@ -35,12 +36,28 @@ def process_image(image, categories_to_hide):
|
|
35 |
# Create an empty transparent image
|
36 |
transparent_image = np.zeros_like(image_array, dtype=np.uint8)
|
37 |
|
38 |
-
#
|
39 |
mask_combined = np.zeros_like(mask_data, dtype=bool)
|
|
|
40 |
for category in categories_to_hide:
|
41 |
for idx in grouped_mapping.get(category, []):
|
42 |
mask_combined |= (mask_data == idx)
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
# Apply the mask (preserve only selected regions)
|
45 |
transparent_image[mask_combined] = image_array[mask_combined]
|
46 |
|
|
|
12 |
result = client.predict(image=handle_file(image_url), model_name="1b", api_name="/process_image")
|
13 |
return np.load(result[1]) # Result[1] contains the .npy mask
|
14 |
|
15 |
+
|
16 |
def process_image(image, categories_to_hide):
|
17 |
# Convert uploaded image to a PIL Image
|
18 |
image = Image.open(image.name).convert("RGBA")
|
19 |
|
20 |
+
# Save temporarily and get the segmentation mask
|
21 |
image.save("temp_image.png")
|
22 |
mask_data = get_segmentation_mask("temp_image.png")
|
23 |
|
|
|
25 |
grouped_mapping = {
|
26 |
"Background": [0],
|
27 |
"Clothes": [1, 12, 22, 8, 9, 17, 18], # Includes Shoes, Socks, Slippers
|
28 |
+
"Face": [2, 23, 24, 25, 26, 27], # Face, Neck, Lips, Teeth, Tongue
|
29 |
"Hair": [3], # Hair
|
30 |
"Skin": [4, 5, 6, 7, 10, 11, 13, 14, 15, 16, 19, 20, 21] # Hands, Feet, Arms, Legs, Torso
|
31 |
}
|
|
|
36 |
# Create an empty transparent image
|
37 |
transparent_image = np.zeros_like(image_array, dtype=np.uint8)
|
38 |
|
39 |
+
# Create a binary mask for selected categories
|
40 |
mask_combined = np.zeros_like(mask_data, dtype=bool)
|
41 |
+
|
42 |
for category in categories_to_hide:
|
43 |
for idx in grouped_mapping.get(category, []):
|
44 |
mask_combined |= (mask_data == idx)
|
45 |
+
|
46 |
+
# Expand clothing boundaries if clothes are in `categories_to_hide`
|
47 |
+
if "Clothes" in categories_to_hide:
|
48 |
+
clothing_mask = np.isin(mask_data, grouped_mapping["Clothes"]).astype(np.uint8)
|
49 |
+
|
50 |
+
# Determine kernel size (5% of the smaller image dimension)
|
51 |
+
height, width = clothing_mask.shape
|
52 |
+
kernel_size = max(1, int(0.05 * min(height, width))) # Ensure at least 1 pixel
|
53 |
+
kernel = np.ones((kernel_size, kernel_size), np.uint8)
|
54 |
+
|
55 |
+
# Dilate the clothing mask
|
56 |
+
dilated_clothing_mask = cv2.dilate(clothing_mask, kernel, iterations=1)
|
57 |
+
|
58 |
+
# Update mask_combined with the expanded clothing mask
|
59 |
+
mask_combined |= (dilated_clothing_mask == 1)
|
60 |
+
|
61 |
# Apply the mask (preserve only selected regions)
|
62 |
transparent_image[mask_combined] = image_array[mask_combined]
|
63 |
|