ariG23498 HF staff commited on
Commit
e3fa94a
β€’
1 Parent(s): fdbd2a4
Files changed (1) hide show
  1. app.py +22 -8
app.py CHANGED
@@ -7,17 +7,31 @@ import gradio as gr
7
 
8
 
9
  def run_lang_sam(input_image, text_prompt, model):
10
- image = input_image.convert("RGB").resize((256, 256))
11
- masks, _, _, _ = model.predict(
12
- image,
13
- text_prompt
14
- )
 
 
15
  masks_int = masks.to(torch.uint8)
16
  masks_max, _ = masks_int.max(dim=0, keepdim=True)
17
  unified_mask = masks_max.squeeze(0).to(torch.bool)
18
- return Image.fromarray(
19
- (unified_mask[..., None].numpy() * np.array(image)).astype(np.uint8)
20
- )
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
 
23
  def setup_gradio_interface(model):
 
7
 
8
 
9
  def run_lang_sam(input_image, text_prompt, model):
10
+ height = width = 256
11
+ image = input_image.convert("RGB").resize((height, width))
12
+
13
+ # Get the mask using the model
14
+ masks, _, _, _ = model.predict(image, text_prompt)
15
+
16
+ # Convert masks to integer format and find the maximum mask
17
  masks_int = masks.to(torch.uint8)
18
  masks_max, _ = masks_int.max(dim=0, keepdim=True)
19
  unified_mask = masks_max.squeeze(0).to(torch.bool)
20
+
21
+ # Create a colored layer for the mask (choose your color in RGB format)
22
+ color = (255, 0, 0) # Red color, for example
23
+ colored_mask = np.zeros((256, 256, 3), dtype=np.uint8)
24
+ colored_mask[unified_mask] = color # Apply the color to the mask area
25
+
26
+ # Convert the colored mask to PIL for blending
27
+ colored_mask_pil = Image.fromarray(colored_mask)
28
+
29
+ # Blend the colored mask with the original image
30
+ # You can adjust the alpha to change the transparency of the colored mask
31
+ alpha = 0.5 # Transparency factor (between 0 and 1)
32
+ blended_image = Image.blend(image, colored_mask_pil, alpha=alpha)
33
+
34
+ return blended_image
35
 
36
 
37
  def setup_gradio_interface(model):