Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import torch
|
4 |
+
from transformers import SamModel, SamProcessor
|
5 |
+
import numpy as np
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
|
8 |
+
#Load the SAM model and processor
|
9 |
+
model = SamModel.from_pretrained("Zigeng/SlimSAM-uniform-77")
|
10 |
+
processor = SamProcessor.from_pretrained("Zigeng/SlimSAM-uniform-77")
|
11 |
+
|
12 |
+
#Global variable to store input points
|
13 |
+
input_points = []
|
14 |
+
|
15 |
+
#Helper functions
|
16 |
+
def show_mask(mask, ax, random_color=False):
|
17 |
+
if random_color:
|
18 |
+
color = np.concatenate([np.random.random(3),
|
19 |
+
np.array([0.6])],
|
20 |
+
axis=0)
|
21 |
+
else:
|
22 |
+
color = np.array([30/255, 144/255, 255/255, 0.6])
|
23 |
+
h, w = mask.shape[-2:]
|
24 |
+
mask_image = mask.reshape(h,w,1)*color.reshape(1,1,-1)
|
25 |
+
ax.imshow(mask_image)
|
26 |
+
|
27 |
+
#Function to get pixel coordinates
|
28 |
+
def get_pixel_coordinates(image, evt:gr.SelectData):
|
29 |
+
global input_points
|
30 |
+
x, y = evt.index[0], evt.index[1]
|
31 |
+
input_points = [[[x, y]]]
|
32 |
+
return perform_prediction(image)
|
33 |
+
|
34 |
+
#Function to perform SAM model prediction
|
35 |
+
def perform_prediction(image):
|
36 |
+
global input_points
|
37 |
+
#Preprocess the image
|
38 |
+
inputs = processor(images=image, input_points=input_points, return_tensors="pt")
|
39 |
+
#Perform prediction
|
40 |
+
with torch.no_grad():
|
41 |
+
outputs = model(**inputs)
|
42 |
+
iou = outputs.iou_scores
|
43 |
+
max_iou_index = torch.argmax(iou)
|
44 |
+
|
45 |
+
#Post-process the masks
|
46 |
+
predicted_masks = processor.image_processor.post_process_masks(
|
47 |
+
outputs.pred_masks,
|
48 |
+
inputs['original_sizes'],
|
49 |
+
inputs['reshaped_input_sizes']
|
50 |
+
)
|
51 |
+
predicted_mask = predicted_masks[0]
|
52 |
+
|
53 |
+
#Display the mask on the image
|
54 |
+
mask_image = show_mask_on_image(image, predicted_mask[:, max_iou_index], return_image=True)
|
55 |
+
return mask_image
|
56 |
+
|
57 |
+
#Function to overlay mask on the image
|
58 |
+
def show_mask_on_image(raw_image, mask, return_image=False):
|
59 |
+
if not isinstance(mask, torch.Tensor):
|
60 |
+
mask = torch.Tensor(mask)
|
61 |
+
|
62 |
+
if len(mask.shape) == 4:
|
63 |
+
mask = mask.squeeze()
|
64 |
+
|
65 |
+
fig, axes = plt.subplots(1,1,figsize=(15,15))
|
66 |
+
|
67 |
+
mask = mask.cpu().detach()
|
68 |
+
axes.imshow(np.array(raw_image))
|
69 |
+
show_mask(mask, axes)
|
70 |
+
axes.axis("off")
|
71 |
+
plt.show()
|
72 |
+
|
73 |
+
if return_image:
|
74 |
+
fig = plt.gcf()
|
75 |
+
fig.canvas.draw()
|
76 |
+
#Convert plot to image
|
77 |
+
img = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
|
78 |
+
img = img.reshape(fig.canvas.get_width_height()[::-1]+(3,))
|
79 |
+
img = Image.fromarray(img)
|
80 |
+
plt.close(fig)
|
81 |
+
return img
|
82 |
+
|
83 |
+
#Create the Gradio interface
|
84 |
+
with gr.Blocks() as demo:
|
85 |
+
with gr.Row():
|
86 |
+
img = gr.Image(type="pil", label="Input Image", height=400, width=600)
|
87 |
+
output_image = gr.Image(label="Masked Image")
|
88 |
+
img.select(get_pixel_coordinates, inputs=[img], outputs=[output_image])
|
89 |
+
|
90 |
+
demo.launch(share=False)
|