Update app.py
Browse files
app.py
CHANGED
@@ -53,20 +53,23 @@ 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 |
# blend the original image and the mask using the alpha value
|
57 |
-
|
58 |
-
ax.imshow(img)
|
59 |
-
ax.imshow(final_mask, alpha=alpha_value, cmap="jet")
|
60 |
-
ax.axis("off")
|
61 |
-
plt.tight_layout()
|
62 |
|
63 |
# extract the final image
|
64 |
final_mask = Image.fromarray(final_mask.astype(np.uint8) * 255, "L")
|
65 |
inverse_mask = np.invert(final_mask)
|
66 |
-
output_image = Image.new("RGBA", img.size, (0, 0, 0, 0))
|
67 |
-
output_image.paste(img, mask=final_mask)
|
68 |
|
69 |
-
return output_image,
|
|
|
70 |
|
71 |
|
72 |
title = "Interactive demo: zero-shot image segmentation with CLIPSeg"
|
|
|
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 |
# blend the original image and the mask using the alpha value
|
65 |
+
output_image = Image.blend(img_rgba, mask_rgba, alpha_value)
|
|
|
|
|
|
|
|
|
66 |
|
67 |
# extract the final image
|
68 |
final_mask = Image.fromarray(final_mask.astype(np.uint8) * 255, "L")
|
69 |
inverse_mask = np.invert(final_mask)
|
|
|
|
|
70 |
|
71 |
+
return output_image, final_mask, inverse_mask
|
72 |
+
|
73 |
|
74 |
|
75 |
title = "Interactive demo: zero-shot image segmentation with CLIPSeg"
|