Uh oh!
There was an error while loading. Please reload this page.
forked from RuijieZhu94/ObjectGS
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_object_mesh.py
More file actions
Latest commit
81 lines (70 loc) · 4.14 KB
/
Copy pathexport_object_mesh.py
File metadata and controls
81 lines (70 loc) · 4.14 KB
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
importtorch
fromsceneimportScene
importos
importsys
importyaml
fromtqdmimporttqdm
fromosimportmakedirs
importtorchvision
fromargparseimportArgumentParser
fromutils.mesh_utilsimportGaussianExtractor, to_cam_open3d, post_process_mesh
fromutils.general_utilsimportparse_cfg
importopen3daso3d
if__name__=="__main__":
# Set up command line argument parser
parser=ArgumentParser(description="Testing script parameters")
parser.add_argument('-m', '--model_path', type=str, required=True)
parser.add_argument("--scene_name", default=None)
parser.add_argument("--iteration", default=-1, type=int)
parser.add_argument("--voxel_size", default=-1.0, type=float, help='Mesh: voxel size for TSDF')
parser.add_argument("--depth_trunc", default=-1.0, type=float, help='Mesh: Max depth range for TSDF')
parser.add_argument("--sdf_trunc", default=-1.0, type=float, help='Mesh: truncation value for TSDF')
parser.add_argument("--num_cluster", default=10, type=int, help='Mesh: number of connected clusters to export')
parser.add_argument("--query_label_id", default=-1, type=int, help='Mesh: id of queried gaussians')
parser.add_argument("--unbounded", action="store_true", help='Mesh: using unbounded mode for meshing')
parser.add_argument("--mesh_res", default=2048, type=int, help='Mesh: resolution for unbounded mesh extraction')
args=parser.parse_args(sys.argv[1:])
withopen(os.path.join(args.model_path, "config.yaml")) asf:
cfg=yaml.load(f, Loader=yaml.FullLoader)
args.scene_name=args.model_path.split('/')[-2]
ifargs.scene_nameisnotNone:
try:
cfg["model_params"]["exp_name"] =os.path.join(cfg["model_params"]["exp_name"], args.scene_name)
cfg["model_params"]["source_path"] =os.path.join(cfg["model_params"]["source_path"], args.scene_name)
except:
print("OverrideError: Cannot override 'exp_name' and 'source_path' in 'model_params'. Exiting.")
sys.exit(1)
lp, op, pp=parse_cfg(cfg)
lp.model_path=args.model_path
print("Rendering "+args.model_path)
modules=__import__('scene')
model_config=lp.model_config
iteration=args.iteration
gaussians=getattr(modules, model_config['name'])(**model_config['kwargs'])
scene=Scene(lp, gaussians, load_iteration=iteration, shuffle=False)
queried_object_mask=gaussians.label_ids.squeeze() ==args.query_label_id
modules=__import__('gaussian_renderer')
gaussExtractor=GaussianExtractor(gaussians, getattr(modules, 'render'), pp, scene.background, queried_object_mask)
# set the active_sh to 0 to export only diffuse texture
train_dir=os.path.join(args.model_path, 'train', "id_{}".format(args.query_label_id), "mesh")
os.makedirs(train_dir, exist_ok=True)
ifgaussExtractor.gaussians.active_sh_degree!=None:
gaussExtractor.gaussians.active_sh_degree=0
gaussExtractor.reconstruction(scene.getTrainCameras())
# extract the mesh and save
ifargs.unbounded:
name='fuse_unbounded.ply'
mesh=gaussExtractor.extract_mesh_unbounded(resolution=args.mesh_res)
else:
name='fuse.ply'
depth_trunc= (gaussExtractor.radius*2.0)*5ifargs.depth_trunc<0elseargs.depth_trunc
# depth_trunc = (gaussExtractor.radius * 2.0) if args.depth_trunc < 0 else args.depth_trunc
voxel_size= (depth_trunc/args.mesh_res) ifargs.voxel_size<0elseargs.voxel_size
sdf_trunc=5.0*voxel_sizeifargs.sdf_trunc<0elseargs.sdf_trunc
mesh=gaussExtractor.extract_mesh_bounded(voxel_size=voxel_size, sdf_trunc=sdf_trunc, depth_trunc=depth_trunc)
o3d.io.write_triangle_mesh(os.path.join(train_dir, name), mesh)
print("mesh saved at {}".format(os.path.join(train_dir, name)))
# post-process the mesh and save, saving the largest N clusters
mesh_post=post_process_mesh(mesh, cluster_to_keep=args.num_cluster)
o3d.io.write_triangle_mesh(os.path.join(train_dir, name.replace('.ply', '_post.ply')), mesh_post)
print("mesh post processed saved at {}".format(os.path.join(train_dir, name.replace('.ply', '_post.ply'))))