Spaces:
Running
Running
Commit
·
3dbd6ff
1
Parent(s):
e29e4d5
add SAM
Browse files- app.py +57 -1
- images/street_1.jpg +0 -0
- images/street_2.jpg +0 -0
- images/street_3.jpg +0 -0
- images/tom_1.jpg +0 -0
- images/tom_2.jpg +0 -0
- images/tom_3.jpg +0 -0
app.py
CHANGED
|
@@ -9,6 +9,30 @@ from PIL import Image
|
|
| 9 |
import numpy as np
|
| 10 |
import gradio as gr
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def inference_mask1(prompt,
|
| 13 |
img,
|
| 14 |
img_):
|
|
@@ -56,6 +80,15 @@ examples = [
|
|
| 56 |
['./images/ydt_2.jpg', './images/ydt_1.jpg', './images/ydt_3.jpg'],
|
| 57 |
]
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
demo_mask = gr.Interface(fn=inference_mask1,
|
| 60 |
inputs=[gr.ImageMask(brush_radius=8, label="prompt (提示图)"), gr.Image(label="img1 (测试图1)"), gr.Image(label="img2 (测试图2)")],
|
| 61 |
#outputs=[gr.Image(shape=(448, 448), label="output1 (输出图1)"), gr.Image(shape=(448, 448), label="output2 (输出图2)")],
|
|
@@ -77,6 +110,29 @@ demo_mask = gr.Interface(fn=inference_mask1,
|
|
| 77 |
)
|
| 78 |
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
title = "SegGPT: Segmenting Everything In Context<br> \
|
| 81 |
<div align='center'> \
|
| 82 |
<h2><a href='https://arxiv.org/abs/2304.03284' target='_blank' rel='noopener'>[paper]</a> \
|
|
@@ -87,7 +143,7 @@ title = "SegGPT: Segmenting Everything In Context<br> \
|
|
| 87 |
</div> \
|
| 88 |
"
|
| 89 |
|
| 90 |
-
demo = gr.TabbedInterface([
|
| 91 |
|
| 92 |
#demo.launch(share=True, auth=("baai", "vision"))
|
| 93 |
demo.launch(enable_queue=False)
|
|
|
|
| 9 |
import numpy as np
|
| 10 |
import gradio as gr
|
| 11 |
|
| 12 |
+
|
| 13 |
+
def inference_mask1_sam(prompt,
|
| 14 |
+
img,
|
| 15 |
+
img_):
|
| 16 |
+
|
| 17 |
+
files = {
|
| 18 |
+
"useSam" : 1,
|
| 19 |
+
"pimage" : resizeImg(prompt["image"]),
|
| 20 |
+
"pmask" : resizeImg(prompt["mask"]),
|
| 21 |
+
"img" : resizeImg(img),
|
| 22 |
+
"img_" : resizeImg(img_)
|
| 23 |
+
}
|
| 24 |
+
r = requests.post("http://120.92.79.209/painter/run", json = files)
|
| 25 |
+
a = json.loads(r.text)
|
| 26 |
+
|
| 27 |
+
res = []
|
| 28 |
+
|
| 29 |
+
for i in range(len(a)):
|
| 30 |
+
#out = Image.open(io.BytesIO(base64.b64decode(a[i])))
|
| 31 |
+
#out = out.resize((224, 224))
|
| 32 |
+
#res.append(np.uint8(np.array(out)))
|
| 33 |
+
res.append(np.uint8(np.array(Image.open(io.BytesIO(base64.b64decode(a[i]))))))
|
| 34 |
+
return res[1:] # remove prompt image
|
| 35 |
+
|
| 36 |
def inference_mask1(prompt,
|
| 37 |
img,
|
| 38 |
img_):
|
|
|
|
| 80 |
['./images/ydt_2.jpg', './images/ydt_1.jpg', './images/ydt_3.jpg'],
|
| 81 |
]
|
| 82 |
|
| 83 |
+
examples_sam = [
|
| 84 |
+
['./images/hmbb_1.jpg', './images/hmbb_2.jpg', './images/hmbb_3.jpg'],
|
| 85 |
+
['./images/street_1.jpg', './images/street_2.jpg', './images/street_3.jpg'],
|
| 86 |
+
['./images/tom_1.jpg', './images/tom_2.jpg', './images/tom_3.jpg'],
|
| 87 |
+
['./images/earth_1.jpg', './images/earth_2.jpg', './images/earth_3.jpg'],
|
| 88 |
+
['./images/ydt_2.jpg', './images/ydt_1.jpg', './images/ydt_3.jpg'],
|
| 89 |
+
]
|
| 90 |
+
|
| 91 |
+
|
| 92 |
demo_mask = gr.Interface(fn=inference_mask1,
|
| 93 |
inputs=[gr.ImageMask(brush_radius=8, label="prompt (提示图)"), gr.Image(label="img1 (测试图1)"), gr.Image(label="img2 (测试图2)")],
|
| 94 |
#outputs=[gr.Image(shape=(448, 448), label="output1 (输出图1)"), gr.Image(shape=(448, 448), label="output2 (输出图2)")],
|
|
|
|
| 110 |
)
|
| 111 |
|
| 112 |
|
| 113 |
+
|
| 114 |
+
demo_mask_sam = gr.Interface(fn=inference_mask1_sam,
|
| 115 |
+
inputs=[gr.ImageMask(brush_radius=4, label="prompt (提示图)"), gr.Image(label="img1 (测试图1)"), gr.Image(label="img2 (测试图2)")],
|
| 116 |
+
#outputs=[gr.Image(shape=(448, 448), label="output1 (输出图1)"), gr.Image(shape=(448, 448), label="output2 (输出图2)")],
|
| 117 |
+
# outputs=[gr.Image(label="output1 (输出图1)").style(height=256, width=256), gr.Image(label="output2 (输出图2)").style(height=256, width=256)],
|
| 118 |
+
#outputs=gr.Gallery(label="outputs (输出图)"),
|
| 119 |
+
outputs=[gr.Image(label="SAM output (mask)").style(height=256, width=256),gr.Image(label="output1 (输出图1)").style(height=256, width=256), gr.Image(label="output2 (输出图2)").style(height=256, width=256)],
|
| 120 |
+
# outputs=[gr.Image(label="output3 (输出图1)").style(height=256, width=256), gr.Image(label="output4 (输出图2)").style(height=256, width=256)],
|
| 121 |
+
examples=examples_sam,
|
| 122 |
+
#title="SegGPT for Any Segmentation<br>(Painter Inside)",
|
| 123 |
+
description="<p> \
|
| 124 |
+
Choose an example below 🔥 🔥 🔥 <br>\
|
| 125 |
+
Or, upload by yourself: <br>\
|
| 126 |
+
1. Upload images to be tested to 'img1' and 'img2'. <br>2. Upload a prompt image to 'prompt' and draw <strong>a point or line on the target<strong>. <br>\
|
| 127 |
+
<br> \
|
| 128 |
+
💎 SAM segment the target with any point or scribble, then SegGPT segments all other images. <br>\
|
| 129 |
+
💎 Examples below were never trained and are randomly selected for testing in the wild. <br>\
|
| 130 |
+
💎 Current UI interface only unleashes a small part of the capabilities of SegGPT, i.e., 1-shot case. \
|
| 131 |
+
</p>",
|
| 132 |
+
cache_examples=False,
|
| 133 |
+
allow_flagging="never",
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
title = "SegGPT: Segmenting Everything In Context<br> \
|
| 137 |
<div align='center'> \
|
| 138 |
<h2><a href='https://arxiv.org/abs/2304.03284' target='_blank' rel='noopener'>[paper]</a> \
|
|
|
|
| 143 |
</div> \
|
| 144 |
"
|
| 145 |
|
| 146 |
+
demo = gr.TabbedInterface([demo_mask_sam, demo_mask], ['SAM+SegGPT (一触百通)', 'General 1-shot'], title=title)
|
| 147 |
|
| 148 |
#demo.launch(share=True, auth=("baai", "vision"))
|
| 149 |
demo.launch(enable_queue=False)
|
images/street_1.jpg
ADDED
|
images/street_2.jpg
ADDED
|
images/street_3.jpg
ADDED
|
images/tom_1.jpg
ADDED
|
images/tom_2.jpg
ADDED
|
images/tom_3.jpg
ADDED
|