aryswisnu commited on
Commit
0502b57
1 Parent(s): 7ac90a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -6
app.py CHANGED
@@ -44,7 +44,7 @@ def get_masks(prompts, img, threhsold):
44
  return masks
45
 
46
 
47
- def extract_image(img, pos_prompts, neg_prompts, threshold, alpha_value=0.5):
48
  positive_masks = get_masks(pos_prompts, img, threshold)
49
  negative_masks = get_masks(neg_prompts, img, threshold)
50
 
@@ -61,15 +61,19 @@ def extract_image(img, pos_prompts, neg_prompts, threshold, alpha_value=0.5):
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"
 
44
  return masks
45
 
46
 
47
+ def extract_image(img, pos_prompts, neg_prompts, threshold, alpha_value):
48
  positive_masks = get_masks(pos_prompts, img, threshold)
49
  negative_masks = get_masks(neg_prompts, img, threshold)
50
 
 
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=alpha_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, alpha_mask, inverse_alpha_mask
77
 
78
 
79
  title = "Interactive demo: zero-shot image segmentation with CLIPSeg"