-
Notifications
You must be signed in to change notification settings - Fork 5
/
visualize.py
74 lines (63 loc) · 2.8 KB
/
visualize.py
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
import open3d as o3d
import numpy as np
import os
from os.path import join
import tqdm
from sai3d_utils import get_points_from_ply
def export_mesh(name, v, f, c=None):
mesh = o3d.geometry.TriangleMesh()
mesh.vertices = o3d.utility.Vector3dVector(v)
mesh.triangles = o3d.utility.Vector3iVector(f)
if c is not None:
mesh.vertex_colors = o3d.utility.Vector3dVector(c)
o3d.io.write_triangle_mesh(name, mesh)
def get_labels_from_eval_format(scene_id, res_dir):
"""Get instance id of each point from the evaluation format of ScanNet
When the instance id is 0, it means the point is not in any instance.
If one point is in multiple instances, the instance is the one with highest confidence.
"""
masks_data_path = join(res_dir,f'{scene_id}.txt')
labels = None
with open(masks_data_path,'r') as f:
label_id = 1
for line in tqdm.tqdm(reversed(f.readlines()), desc='get instance ids from eval format'):
rel_mask_path = line.split(' ')[0]
mask_path = join(res_dir,rel_mask_path)
ids = np.array(open(mask_path).read().splitlines(),dtype=np.int64)
if labels is None:
labels = np.zeros_like(ids)
labels[ids > 0] = label_id
label_id += 1
return labels
def save_scannet_eval_format_to_mesh(scene_id,
res_dir,
data_dir ='../data/ScanNet'):
"""Save the class-agnostic instance segmentation results
from ScanNet eval format into mesh for visualization.
We assign random colors to each object label.
Args:
scene_id: the id of scece to visualize
res_dir: the directory of the results for evaluation,
like "/home/data/ScanNet/results/demo_scannet_..depth2"
data_dir: the directory of the ScanNet dataset
"""
labels = get_labels_from_eval_format(scene_id, res_dir)
points_num = labels.shape[0]
colors = np.ones((points_num,3))
for label in np.unique(labels):
if(label == 0):continue
colors[labels == label] = np.random.rand(3)
ply_path = join(data_dir, 'scans', scene_id, f'{scene_id}_vh_clean_2.ply')
mesh = o3d.io.read_triangle_mesh(ply_path)
v = np.array(mesh.vertices)
f = np.array(mesh.triangles)
c_label = colors
os.makedirs(join(data_dir, 'vis_mesh'), exist_ok=True)
save_path = join(data_dir, 'vis_mesh', f'{scene_id}.ply')
export_mesh(save_path, v, f, c_label)
print('save to', save_path)
if __name__ == '__main__':
scene_id = 'scene0011_00'
res_dir = "/home/code/SAI3D/data/ScanNet/results/demo_scannet_5view_merge200_2-norm_semantic-sam_connect(0.9,0.5,5)_depth2"
data_dir = "/home/code//SAI3D/data/ScanNet"
save_scannet_eval_format_to_mesh(scene_id, res_dir, data_dir)