File size: 14,468 Bytes
b7c176b 762c88c e9b69e6 762c88c 9ba7304 762c88c 86e6120 762c88c 9ba7304 762c88c 9ba7304 762c88c 9ba7304 762c88c 9ba7304 762c88c 9ba7304 762c88c 9ba7304 762c88c 9ba7304 762c88c 9ba7304 762c88c 9ba7304 762c88c 9ba7304 762c88c 9ba7304 762c88c 9ba7304 762c88c 9ba7304 762c88c 9ba7304 762c88c 9ba7304 762c88c 9ba7304 762c88c 9ba7304 762c88c 0e83cff 307f924 97432d9 0e83cff 5f7649b 307f924 97432d9 5f7649b 307f924 0e83cff 762c88c 9ba7304 762c88c 9ba7304 762c88c a2308ba 762c88c 9ba7304 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
import spaces
from pickle import FALSE
import gradio as gr
import numpy as np
import plotly.graph_objects as go
from sam2point import dataset
import sam2point.configs as configs
from demo_utils import run_demo, create_box
samples = {
"3D Indoor Scene - S3DIS": ["Conference Room", "Restroom", "Lobby", "Office1", "Office2"],
"3D Indoor Scene - ScanNet": ["Scene1", "Scene2", "Scene3", "Scene4", "Scene5", "Scene6"],
"3D Raw LiDAR - KITTI": ["Scene1", "Scene2", "Scene3", "Scene4", "Scene5", "Scene6"],
"3D Outdoor Scene - Semantic3D": ["Scene1", "Scene2", "Scene3", "Scene4", "Scene5", "Scene6", "Scene7"],
"3D Object - Objaverse": ["Plant", "Lego", "Lock", "Eleplant", "Knife Rest", "Skateboard", "Popcorn Machine", "Stove", "Bus Shelter", "Thor Hammer", "Horse"],
}
PATH = {
"S3DIS": ['Area_1_conferenceRoom_1.txt', 'Area_2_WC_1.txt', 'Area_4_lobby_2.txt', 'Area_5_office_3.txt', 'Area_6_office_9.txt'],
"ScanNet": ['scene0005_01.pth', 'scene0010_01.pth', 'scene0016_02.pth', 'scene0019_01.pth', 'scene0000_00.pth', 'scene0002_00.pth'],
"Objaverse": ["plant.npy", "human.npy", "lock.npy", "elephant.npy", "knife_rest.npy", "skateboard.npy", "popcorn_machine.npy", "stove.npy", "bus_shelter.npy", "thor_hammer.npy", "horse.npy"],
"KITTI": ["scene1.npy", "scene2.npy", "scene3.npy", "scene4.npy", "scene5.npy", "scene6.npy"],
"Semantic3D": ["scene1.npy", "scene2.npy", "patch19.npy", "patch0.npy", "patch1.npy", "patch50.npy", "patch62.npy"]
}
prompt_types = ["Point", "Box", "Mask"]
def load_3d_scene(name, sample_idx=-1, type_=None, prompt=None, final=False, new_color=None):
DATASET = name.split('-')[1].replace(" ", "")
path = 'data/' + DATASET + '/' + PATH[DATASET][sample_idx]
asp, SIZE = 1., 1
print(path)
if DATASET == 'S3DIS':
point, color = dataset.load_S3DIS_sample(path, sample=True)
alpha = 1
elif DATASET == 'ScanNet':
point, color = dataset.load_ScanNet_sample(path)
alpha = 1
elif DATASET == 'Objaverse':
point, color = dataset.load_Objaverse_sample(path)
alpha = 1
SIZE = 2
elif DATASET == 'KITTI':
point, color = dataset.load_KITTI_sample(path)
asp = 0.3
alpha = 0.7
elif DATASET == 'Semantic3D':
point, color = dataset.load_Semantic3D_sample(path, sample_idx, sample=True)
alpha = 0.2
print("Loading Dataset:", DATASET, "Point Cloud Size:", point.shape, "Path:", path)
##### Initial Show #####
if not type_:
if point.shape[0] > 100000: # sample points for speeding up
indices = np.random.choice(point.shape[0], 100000, replace=False)
point = point[indices]
color = color[indices]
fig = go.Figure(
data=[
go.Scatter3d(
x=point[:,0], y=point[:,1], z=point[:,2],
mode='markers',
marker=dict(size=SIZE, color=color, opacity=alpha),
name=""
)
],
layout=dict(
scene=dict(
xaxis=dict(visible=False),
yaxis=dict(visible=False),
zaxis=dict(visible=False),
aspectratio=dict(x=1, y=1, z=asp),
camera=dict(eye=dict(x=1.5, y=1.5, z=1.5))
)
)
)
return fig
##### Final Results #####
if final:
color = new_color
green = np.array([[0.1, 0.1, 0.1]])
add_green = go.Scatter3d(
x=green[:,0], y=green[:,1], z=green[:,2],
mode='markers',
marker=dict(size=0.0001, color='green', opacity=1),
name="Segmentation Results"
)
if type_ == "box":
if point.shape[0] > 100000:
indices = np.random.choice(point.shape[0], 100000, replace=False)
point = point[indices]
color = color[indices]
scatter = go.Scatter3d(
x=point[:,0], y=point[:,1], z=point[:,2],
mode='markers',
marker=dict(size=SIZE, color=color, opacity=alpha),
name="3D Object/Scene"
)
if final: scatter = [scatter, add_green] + create_box(prompt)
else: scatter = [scatter] + create_box(prompt)
elif type_ == "point":
prompt = np.array([prompt])
new = go.Scatter3d(
x=prompt[:,0], y=prompt[:,1], z=prompt[:,2],
mode='markers',
marker=dict(size=5, color='red', opacity=1),
name="Point Prompt"
)
if point.shape[0] > 100000:
indices = np.random.choice(point.shape[0], 100000, replace=False)
point = point[indices]
color = color[indices]
scatter = go.Scatter3d(
x=point[:,0], y=point[:,1], z=point[:,2],
mode='markers',
marker=dict(size=SIZE, color=color, opacity=alpha),
name="3D Object/Scene"
)
if final: scatter = [scatter, new, add_green]
else: scatter = [scatter, new]
elif type_ == 'mask' and not final:
color = np.clip(prompt * 255, 0, 255).astype(np.uint8)
if point.shape[0] > 100000:
indices = np.random.choice(point.shape[0], 100000, replace=False)
point = point[indices]
color = color[indices]
scatter = go.Scatter3d(
x=point[:,0], y=point[:,1], z=point[:,2],
mode='markers',
marker=dict(size=SIZE, color=color, opacity=alpha),
name="3D Object/Scene"
)
red = np.array([[0.1, 0.1, 0.1]])
add_red = go.Scatter3d(
x=red[:,0], y=red[:,1], z=red[:,2],
mode='markers',
marker=dict(size=0.0001, color='red', opacity=1),
name="Mask Prompt"
)
scatter = [scatter, add_red]
elif type_ == 'mask' and final:
if point.shape[0] > 100000:
indices = np.random.choice(point.shape[0], 100000, replace=False)
point = point[indices]
color = color[indices]
scatter = go.Scatter3d(
x=point[:,0], y=point[:,1], z=point[:,2],
mode='markers',
marker=dict(size=SIZE, color=color, opacity=alpha),
name="3D Object/Scene"
)
scatter = [scatter, add_green]
else:
print("Wrong Prompt Type")
exit(1)
fig = go.Figure(
data=scatter,
layout=dict(
scene=dict(
xaxis=dict(visible=False),
yaxis=dict(visible=False),
zaxis=dict(visible=False),
aspectratio=dict(x=1, y=1, z=asp),
camera=dict(eye=dict(x=1.5, y=1.5, z=1.5))
)
)
)
return fig
@spaces.GPU()
def show_prompt_in_3d(name, sample_idx, prompt_type, prompt_idx):
if name == None or sample_idx == None or prompt_type == None or prompt_idx == None:
return gr.Plot(), gr.Textbox(label="Response", value="Please ensure all options are selected.", visible=True)
DATASET = name.split('-')[1].replace(" ", "")
TYPE = prompt_type.lower()
theta = 0. if DATASET in "S3DIS ScanNet" else 0.5
mode = "bilinear" if DATASET in "S3DIS ScanNet" else 'nearest'
prompt = run_demo(DATASET, TYPE, sample_idx, prompt_idx, 0.02, theta, mode, ret_prompt=True)
fig = load_3d_scene(name, sample_idx, TYPE, prompt)
return fig, gr.Textbox(label="Response", value="Prompt has been shown in 3D Object/Scene!", visible=True)
@spaces.GPU()
def start_segmentation(name=None, sample_idx=None, prompt_type=None, prompt_idx=None, vx=0.02):
if name == None or sample_idx == None or prompt_type == None or prompt_idx == None:
return gr.Plot(), gr.Textbox(label="Response", value="Please ensure all options are selected.", visible=True)
DATASET = name.split('-')[1].replace(" ", "")
TYPE = prompt_type.lower()
theta = 0. if DATASET in "S3DIS ScanNet" else 0.5
mode = "bilinear" if DATASET in "S3DIS ScanNet" else 'nearest'
new_color, prompt = run_demo(DATASET, TYPE, sample_idx, prompt_idx, vx, theta, mode, ret_prompt=False)
fig = load_3d_scene(name, sample_idx, TYPE, prompt, final=True, new_color=new_color)
return fig, gr.Textbox(label="Response", value="Segmentation completed successfully!", visible=True)
def update1(datasets):
if 'Objaverse' in datasets:
return gr.Radio(label="Select 3D Object", choices=samples[datasets]), gr.Textbox(label="Response", value="", visible=True)
return gr.Radio(label="Select 3D Scene", choices=samples[datasets]), gr.Textbox(label="Response", value="", visible=True)
def update2(name, sample_idx, prompt_type):
if name == None or sample_idx == None or prompt_type == None:
return gr.Radio(label="Select Prompt Example", choices=[]), gr.Textbox(label="Response", value="", visible=True)
DATASET = name.split('-')[1].replace(" ", "")
TYPE = prompt_type.lower() + '_prompts'
if DATASET == 'S3DIS':
info = configs.S3DIS_samples[sample_idx][TYPE]
elif DATASET == 'ScanNet':
info = configs.ScanNet_samples[sample_idx][TYPE]
elif DATASET == 'Objaverse':
info = configs.Objaverse_samples[sample_idx][TYPE]
elif DATASET == 'KITTI':
info = configs.KITTI_samples[sample_idx][TYPE]
elif DATASET == 'Semantic3D':
info = configs.Semantic3D_samples[sample_idx][TYPE]
cur = ['Example ' + str(i) for i in range(1, len(info) + 1)]
return gr.Radio(label="Select Prompt Example", choices=cur), gr.Textbox(label="Response", value="", visible=True)
def update3(name, sample_idx, prompt_type, prompt_idx):
if name == None or sample_idx == None or prompt_type == None:
return gr.Textbox(label="Response", value="", visible=True), gr.Slider(minimum=0.01, maximum=0.15, step=0.001, label="Voxel Size", value=0.02)
DATASET = name.split('-')[1].replace(" ", "")
TYPE = configs.VOXEL[prompt_type.lower()]
if DATASET in "S3DIS ScanNet":
vx_ = 0.02
elif DATASET == 'Objaverse':
vx_ = configs.Objaverse_samples[sample_idx][TYPE][prompt_idx]
elif DATASET == 'KITTI':
vx_ = configs.KITTI_samples[sample_idx][TYPE][prompt_idx]
elif DATASET == 'Semantic3D':
vx_ = configs.Semantic3D_samples[sample_idx][TYPE][prompt_idx]
return gr.Textbox(label="Response", value="", visible=True), gr.Slider(minimum=0.01, maximum=0.15, step=0.001, label="Voxel Size", value=vx_)
def main():
title = """<h1 style="text-align: center;">
<div style="width: 1.2em; height: 1.2em; display: inline-block;"><img src="https://github.com/ZiyuGuo99/ZiyuGuo99.github.io/blob/main/assets/img/logo.png?raw=true" style='width: 100%; height: 100%; object-fit: contain;' /></div>
<span style="font-variant: small-caps; font-weight: bold;">Sam2Point</span>
</h1>
<h3 align="center"><span style="font-variant: small-caps; ">Segment Any 3D as Videos in Zero-shot and Promptable Manners
</span></h3>
<div style="text-align: center;">
<div style="display: flex; align-items: center; justify-content: center; gap: 0.5rem; margin-bottom: 0.5rem; font-size: 1rem; flex-wrap: wrap;">
<a href="https://github.com/ZiyuGuo99/SAM2Point/blob/main/SAM2Point.pdf" target="_blank">[Paper]</a>
<a href="https://github.com/ZiyuGuo99/SAM2Point" target="_blank">[Code]</a>
<a href="https://sam2point.github.io/" target="_blank">[Webpage]</a>
</div>
</div>
<p style="text-align: center;">
Select an example and a 3D prompt to start segmentation using <span style="font-variant: small-caps;">Sam2Point</span>.
</p>
<p style="text-align: center;">
Custom 3D input and prompts will be supported soon.
</p>
"""
with gr.Blocks(
css="""
.contain { display: flex; flex-direction: column; }
.gradio-container { height: 100vh !important; }
#col_container { height: 100%; }
pre {
white-space: pre-wrap; /* Since CSS 2.1 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}""",
js="""
function refresh() {
const url = new URL(window.location);
if (url.searchParams.get('__theme') !== 'light') {
url.searchParams.set('__theme', 'light');
window.location.href = url.href;
}
}""",
title="SAM2Point: Segment Any 3D as Videos in Zero-shot and Promptable Manners",
theme=gr.themes.Soft()
) as app:
gr.HTML(title)
with gr.Row():
with gr.Column(elem_id="col_container"):
sample_dropdown = gr.Dropdown(label="Select 3D Data Type", choices=samples, type="value")
scene_dropdown = gr.Radio(label="Select 3D Object/Scene", choices=[], type="index")
show_button = gr.Button("Show 3D Scene/Object")
prompt_type_dropdown = gr.Radio(label="Select Prompt Type", choices=prompt_types)
prompt_sample_dropdown = gr.Radio(label="Select Prompt Example", choices=[], type="index")
show_prompt_button = gr.Button("Show Prompt in 3D Scene/Object")
with gr.Column():
start_segment_button = gr.Button("Start Segmentation")
plot1 = gr.Plot()
response = gr.Textbox(label="Response")
sample_dropdown.change(update1, sample_dropdown, [scene_dropdown, response])
sample_dropdown.change(update2, [sample_dropdown, scene_dropdown, prompt_type_dropdown], [prompt_sample_dropdown, response])
scene_dropdown.change(update2, [sample_dropdown, scene_dropdown, prompt_type_dropdown], [prompt_sample_dropdown, response])
prompt_type_dropdown.change(update2, [sample_dropdown, scene_dropdown, prompt_type_dropdown], [prompt_sample_dropdown, response])
show_button.click(load_3d_scene, inputs=[sample_dropdown, scene_dropdown], outputs=plot1)
show_prompt_button.click(show_prompt_in_3d, inputs=[sample_dropdown, scene_dropdown, prompt_type_dropdown, prompt_sample_dropdown], outputs=[plot1, response])
start_segment_button.click(start_segmentation, inputs=[sample_dropdown, scene_dropdown, prompt_type_dropdown, prompt_sample_dropdown], outputs=[plot1, response])
app.queue(max_size=20, api_open=False)
app.launch(max_threads=400)
if __name__ == "__main__":
main() |