Spaces:
Runtime error
Runtime error
meetjannik
commited on
Commit
β’
348cb84
1
Parent(s):
f2f193c
update appearance and add examples
Browse files- app.py +42 -19
- {imgs β examples}/cat.jpg +0 -0
- examples/dog.jpg +0 -0
- examples/groceries.jpg +0 -0
- examples/truck.jpg +0 -0
app.py
CHANGED
@@ -39,36 +39,59 @@ def show_anns(anns):
|
|
39 |
img[:,:,i] = color_mask[i]
|
40 |
ax.imshow(np.dstack((img, m*0.35)))
|
41 |
|
|
|
42 |
# demo function
|
43 |
def segment_image(input_image):
|
44 |
|
45 |
-
|
46 |
-
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
|
62 |
|
63 |
with gr.Blocks() as demo:
|
64 |
|
65 |
-
gr.
|
|
|
|
|
|
|
66 |
|
67 |
with gr.Row():
|
68 |
-
image_input = gr.Image()
|
69 |
-
image_output = gr.Image()
|
70 |
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
segment_image_button.click(segment_image, inputs=[image_input], outputs=image_output)
|
73 |
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
img[:,:,i] = color_mask[i]
|
40 |
ax.imshow(np.dstack((img, m*0.35)))
|
41 |
|
42 |
+
|
43 |
# demo function
|
44 |
def segment_image(input_image):
|
45 |
|
46 |
+
if input_image is not None:
|
47 |
+
|
48 |
+
# generate masks
|
49 |
+
masks = mask_generator.generate(input_image)
|
50 |
|
51 |
+
# add masks to image
|
52 |
+
plt.clf()
|
53 |
+
ppi = 100
|
54 |
+
height, width, _ = input_image.shape
|
55 |
+
plt.figure(figsize=(width / ppi, height / ppi)) # convert pixel to inches
|
56 |
+
plt.imshow(input_image)
|
57 |
+
show_anns(masks)
|
58 |
+
plt.axis('off')
|
59 |
|
60 |
+
# save and get figure
|
61 |
+
plt.savefig('output_figure.png', bbox_inches='tight')
|
62 |
+
output_image = cv2.imread('output_figure.png')
|
63 |
+
return Image.fromarray(output_image)
|
64 |
|
65 |
|
66 |
with gr.Blocks() as demo:
|
67 |
|
68 |
+
with gr.Row():
|
69 |
+
gr.Markdown("## Segment Anything (by Meta AI Research)")
|
70 |
+
with gr.Row():
|
71 |
+
gr.Markdown("The Segment Anything Model (SAM) produces high quality object masks from input prompts such as points or boxes, and it can be used to generate masks for all objects in an image. It has been trained on a dataset of 11 million images and 1.1 billion masks, and has strong zero-shot performance on a variety of segmentation tasks.")
|
72 |
|
73 |
with gr.Row():
|
|
|
|
|
74 |
|
75 |
+
with gr.Column():
|
76 |
+
image_input = gr.Image()
|
77 |
+
segment_image_button = gr.Button('Generate Mask')
|
78 |
+
|
79 |
+
with gr.Column():
|
80 |
+
image_output = gr.Image()
|
81 |
+
|
82 |
segment_image_button.click(segment_image, inputs=[image_input], outputs=image_output)
|
83 |
|
84 |
+
gr.Examples(
|
85 |
+
examples=[
|
86 |
+
['./examples/dog.jpg'],
|
87 |
+
['./examples/groceries.jpg'],
|
88 |
+
['./examples/truck.jpg']
|
89 |
+
|
90 |
+
],
|
91 |
+
inputs=[image_input],
|
92 |
+
outputs=[image_output],
|
93 |
+
fn=segment_image,
|
94 |
+
#cache_examples=True
|
95 |
+
)
|
96 |
+
|
97 |
+
demo.launch()
|
{imgs β examples}/cat.jpg
RENAMED
File without changes
|
examples/dog.jpg
ADDED
examples/groceries.jpg
ADDED
examples/truck.jpg
ADDED