sagar007 commited on
Commit
bde346e
1 Parent(s): 8b8d774

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -10
app.py CHANGED
@@ -2,18 +2,20 @@ import gradio as gr
2
  import torch
3
  from PIL import Image
4
  import numpy as np
5
- from sam2.sam2_image_predictor import SAM2ImagePredictor
6
  from huggingface_hub import hf_hub_download
7
 
8
  # Download the model weights
9
  model_path = hf_hub_download(repo_id="facebook/sam2-hiera-large", filename="sam2_hiera_large.pth")
10
 
11
- # Initialize the SAM2 predictor
12
- predictor = SAM2ImagePredictor.from_pretrained(model_path)
 
 
13
 
14
  def segment_image(input_image, x, y):
15
- # Convert gradio image to PIL Image
16
- input_image = Image.fromarray(input_image.astype('uint8'), 'RGB')
17
 
18
  # Prepare the image for the model
19
  predictor.set_image(input_image)
@@ -23,15 +25,18 @@ def segment_image(input_image, x, y):
23
  input_label = np.array([1]) # 1 for foreground
24
 
25
  # Generate the mask
26
- with torch.inference_mode():
27
- masks, _, _ = predictor.predict(point_coords=input_point, point_labels=input_label)
 
 
 
28
 
29
  # Convert the mask to an image
30
- mask = masks[0].cpu().numpy()
31
  mask_image = Image.fromarray((mask * 255).astype(np.uint8))
32
 
33
  # Apply the mask to the original image
34
- result = Image.composite(input_image, Image.new('RGB', input_image.size, 'black'), mask_image)
35
 
36
  return result
37
 
@@ -39,7 +44,7 @@ def segment_image(input_image, x, y):
39
  iface = gr.Interface(
40
  fn=segment_image,
41
  inputs=[
42
- gr.Image(type="numpy"),
43
  gr.Slider(0, 1000, label="X coordinate"),
44
  gr.Slider(0, 1000, label="Y coordinate")
45
  ],
 
2
  import torch
3
  from PIL import Image
4
  import numpy as np
5
+ from sam2 import build_sam2, SamPredictor
6
  from huggingface_hub import hf_hub_download
7
 
8
  # Download the model weights
9
  model_path = hf_hub_download(repo_id="facebook/sam2-hiera-large", filename="sam2_hiera_large.pth")
10
 
11
+ # Initialize the SAM2 model
12
+ device = "cpu" # Use CPU
13
+ model = build_sam2(checkpoint=model_path).to(device)
14
+ predictor = SamPredictor(model)
15
 
16
  def segment_image(input_image, x, y):
17
+ # Convert gradio image to numpy array
18
+ input_image = np.array(input_image)
19
 
20
  # Prepare the image for the model
21
  predictor.set_image(input_image)
 
25
  input_label = np.array([1]) # 1 for foreground
26
 
27
  # Generate the mask
28
+ masks, _, _ = predictor.predict(
29
+ point_coords=input_point,
30
+ point_labels=input_label,
31
+ multimask_output=False,
32
+ )
33
 
34
  # Convert the mask to an image
35
+ mask = masks[0]
36
  mask_image = Image.fromarray((mask * 255).astype(np.uint8))
37
 
38
  # Apply the mask to the original image
39
+ result = Image.composite(Image.fromarray(input_image), Image.new('RGB', mask_image.size, 'black'), mask_image)
40
 
41
  return result
42
 
 
44
  iface = gr.Interface(
45
  fn=segment_image,
46
  inputs=[
47
+ gr.Image(type="pil"),
48
  gr.Slider(0, 1000, label="X coordinate"),
49
  gr.Slider(0, 1000, label="Y coordinate")
50
  ],