Spaces:
Runtime error
Runtime error
added some segmentation functions
Browse files- app.py +62 -4
- requirements.txt +7 -0
app.py
CHANGED
|
@@ -1,7 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import matplotlib
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import numpy as np
|
| 6 |
+
import torch
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
+
from PIL import Image
|
|
|
|
| 10 |
|
| 11 |
+
from segment_anything import SamAutomaticMaskGenerator, SamPredictor, sam_model_registry
|
| 12 |
+
|
| 13 |
+
matplotlib.pyplot.switch_backend('Agg') # for matplotlib to work in gradio
|
| 14 |
+
|
| 15 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # use GPU if available
|
| 16 |
+
|
| 17 |
+
#setup model
|
| 18 |
+
sam_checkpoint = "sam_vit_h_4b8939.pth"
|
| 19 |
+
device = "cuda"
|
| 20 |
+
model_type = "default"
|
| 21 |
+
sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
|
| 22 |
+
sam.to(device=device)
|
| 23 |
+
mask_generator = SamAutomaticMaskGenerator(sam)
|
| 24 |
+
predictor = SamPredictor(sam)
|
| 25 |
+
|
| 26 |
+
def show_anns(anns):
|
| 27 |
+
if len(anns) == 0:
|
| 28 |
+
return
|
| 29 |
+
sorted_anns = sorted(anns, key=(lambda x: x['area']), reverse=True)
|
| 30 |
+
ax = plt.gca()
|
| 31 |
+
ax.set_autoscale_on(False)
|
| 32 |
+
polygons = []
|
| 33 |
+
color = []
|
| 34 |
+
for ann in sorted_anns:
|
| 35 |
+
m = ann['segmentation']
|
| 36 |
+
img = np.ones((m.shape[0], m.shape[1], 3))
|
| 37 |
+
color_mask = np.random.random((1, 3)).tolist()[0]
|
| 38 |
+
for i in range(3):
|
| 39 |
+
img[:,:,i] = color_mask[i]
|
| 40 |
+
ax.imshow(np.dstack((img, m*0.35)))
|
| 41 |
+
|
| 42 |
+
def segment_image(image):
|
| 43 |
+
masks = mask_generator.generate(image)
|
| 44 |
+
plt.clf()
|
| 45 |
+
ppi = 100
|
| 46 |
+
height, width, _ = image.shape
|
| 47 |
+
plt.figure(figsize=(width / ppi, height / ppi), dpi=ppi)
|
| 48 |
+
plt.imshow(image)
|
| 49 |
+
show_anns(masks)
|
| 50 |
+
plt.axis('off')
|
| 51 |
+
plt.savefig('output.png', bbox_inches='tight', pad_inches=0)
|
| 52 |
+
output = cv2.imread('output.png')
|
| 53 |
+
return Image.fromarray(output)
|
| 54 |
+
|
| 55 |
+
with gr.blocks() as demo:
|
| 56 |
+
gr.MArkdown("## Segment-anything Demo")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
image_input = gr.Image()
|
| 60 |
+
image_output = gr.Image()
|
| 61 |
+
|
| 62 |
+
segment_image_button = gr.Button("Segment Image")
|
| 63 |
+
segment_image_button.click(segment_image, image_input, image_output)
|
| 64 |
+
|
| 65 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
opencv-python
|
| 3 |
+
matplotlib
|
| 4 |
+
numpy
|
| 5 |
+
torch
|
| 6 |
+
torchvision
|
| 7 |
+
git+https://github.com/facebookresearch/segment-anything.git
|