Update app.py
Browse files
app.py
CHANGED
@@ -40,11 +40,16 @@ def get_masks(prompts, img, threhsold):
|
|
40 |
for prompt in prompts:
|
41 |
mask = process_image(img, prompt)
|
42 |
mask = mask > threhsold
|
|
|
|
|
|
|
|
|
|
|
43 |
masks.append(mask)
|
44 |
return masks
|
45 |
|
46 |
|
47 |
-
def extract_image(img, pos_prompts, neg_prompts, threshold
|
48 |
positive_masks = get_masks(pos_prompts, img, threshold)
|
49 |
negative_masks = get_masks(neg_prompts, img, threshold)
|
50 |
|
@@ -53,27 +58,13 @@ def extract_image(img, pos_prompts, neg_prompts, threshold, alpha_value=0.5):
|
|
53 |
neg_mask = np.any(np.stack(negative_masks), axis=0)
|
54 |
final_mask = pos_mask & ~neg_mask
|
55 |
|
56 |
-
# create the final mask image
|
57 |
-
final_mask_img = Image.fromarray((final_mask * 255).astype(np.uint8), "L")
|
58 |
-
|
59 |
-
# create an RGBA version of the original image and the final mask image
|
60 |
-
img_rgba = img.convert("RGBA")
|
61 |
-
mask_rgba = Image.new("RGBA", img.size, (0, 0, 0, 0))
|
62 |
-
mask_rgba.paste(final_mask_img, (0, 0), final_mask_img)
|
63 |
-
|
64 |
-
# apply alpha value to final_mask
|
65 |
-
alpha_mask = Image.fromarray((final_mask * 255 * alpha_value).astype(np.uint8), "L")
|
66 |
-
inverse_alpha_mask = Image.fromarray(((1 - final_mask) * 255 * alpha_value).astype(np.uint8), "L")
|
67 |
-
|
68 |
# extract the final image
|
|
|
|
|
69 |
output_image = Image.new("RGBA", img.size, (0, 0, 0, 0))
|
70 |
-
output_image.paste(img, mask=
|
71 |
-
|
72 |
-
# extract the inverse_mask
|
73 |
-
output_inverse_image = Image.new("RGBA", img.size, (0, 0, 0, 0))
|
74 |
-
output_inverse_image.paste(img, mask=inverse_alpha_mask)
|
75 |
|
76 |
-
return output_image,
|
77 |
|
78 |
|
79 |
title = "Interactive demo: zero-shot image segmentation with CLIPSeg"
|
|
|
40 |
for prompt in prompts:
|
41 |
mask = process_image(img, prompt)
|
42 |
mask = mask > threhsold
|
43 |
+
ax = plt.subplots()
|
44 |
+
ax.imshow(image)
|
45 |
+
ax.imshow(mask, alpha=0.5, cmap="jet")
|
46 |
+
ax.axis("off")
|
47 |
+
plt.tight_layout()
|
48 |
masks.append(mask)
|
49 |
return masks
|
50 |
|
51 |
|
52 |
+
def extract_image(img, pos_prompts, neg_prompts, threshold):
|
53 |
positive_masks = get_masks(pos_prompts, img, threshold)
|
54 |
negative_masks = get_masks(neg_prompts, img, threshold)
|
55 |
|
|
|
58 |
neg_mask = np.any(np.stack(negative_masks), axis=0)
|
59 |
final_mask = pos_mask & ~neg_mask
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
# extract the final image
|
62 |
+
final_mask = Image.fromarray(final_mask.astype(np.uint8) * 255, "L")
|
63 |
+
inverse_mask = np.invert(final_mask)
|
64 |
output_image = Image.new("RGBA", img.size, (0, 0, 0, 0))
|
65 |
+
output_image.paste(img, mask=final_mask)
|
|
|
|
|
|
|
|
|
66 |
|
67 |
+
return output_image, final_mask, inverse_mask
|
68 |
|
69 |
|
70 |
title = "Interactive demo: zero-shot image segmentation with CLIPSeg"
|