Spaces:
mashroo
/
Running on Zero

YoussefAnso commited on
Commit
eb6226d
·
1 Parent(s): 1932e80

Refactor mesh generation in generate3d function to streamline processing

Browse files

- Updated the clean_mesh function call to enable remeshing while simplifying the input parameters.
- Removed the original color extraction and scaling logic, focusing on direct mesh generation.
- Introduced temporary file creation for exporting the generated mesh, enhancing output management.

Files changed (1) hide show
  1. inference.py +8 -20
inference.py CHANGED
@@ -62,26 +62,14 @@ def generate3d(model, rgb, ccm, device):
62
  data_config['faces'] = faces
63
 
64
  from kiui.mesh_utils import clean_mesh
65
- orig_verts = data_config['verts'].squeeze().cpu().numpy()
66
- # Extract per-vertex color for the original mesh
67
- orig_verts_tensor = data_config['verts'].unsqueeze(0) # shape [1, N, 3]
68
- with torch.no_grad():
69
- dec_verts = model.decoder(triplane_feature2, orig_verts_tensor)
70
- orig_colors = model.rgbMlp(dec_verts).squeeze().detach().cpu().numpy()
71
- print('orig_colors min/max BEFORE scaling:', orig_colors.min(), orig_colors.max())
72
- # Comment out the scaling below if orig_colors is already in [0, 1]
73
- # orig_colors = (orig_colors * 0.5 + 0.5).clip(0, 1) # scale to [0, 1]
74
- print('orig_colors min/max AFTER scaling:', orig_colors.min(), orig_colors.max())
75
- orig_colors = np.clip(orig_colors, 0, 1)
76
- orig_colors = np.power(orig_colors, 1/2.2)
77
- verts, faces = clean_mesh(
78
- orig_verts.astype(np.float32),
79
- data_config['faces'].squeeze().cpu().numpy().astype(np.int32),
80
- repair=True, remesh=False, remesh_size=0.005, remesh_iters=1
81
- )
82
- data_config['verts'] = torch.from_numpy(verts).to(device).contiguous()
83
- data_config['faces'] = torch.from_numpy(faces).to(device).contiguous()
84
 
 
 
 
 
85
  # # Build KDTree from original verts
86
  # tree = cKDTree(orig_verts)
87
 
@@ -92,7 +80,7 @@ def generate3d(model, rgb, ccm, device):
92
  # new_colors = orig_colors[idxs[:, 0]]
93
 
94
  # Create the new mesh with colors
95
- mesh = trimesh.Trimesh(vertices=verts, faces=faces, vertex_colors=orig_colors)
96
 
97
  # === Export OBJ/MTL/PNG ===
98
  obj_path = tempfile.NamedTemporaryFile(suffix=".obj", delete=False).name
 
62
  data_config['faces'] = faces
63
 
64
  from kiui.mesh_utils import clean_mesh
65
+ verts, faces = clean_mesh(data_config['verts'].squeeze().cpu().numpy().astype(np.float32), data_config['faces'].squeeze().cpu().numpy().astype(np.int32), repair = False, remesh=True, remesh_size=0.005, remesh_iters=1)
66
+ data_config['verts'] = torch.from_numpy(verts).cuda().contiguous()
67
+ data_config['faces'] = torch.from_numpy(faces).cuda().contiguous()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
+ start_time = time.time()
70
+ with torch.no_grad():
71
+ mesh_path_glb = tempfile.NamedTemporaryFile(suffix=f"", delete=False).name
72
+ model.export_mesh(data_config, mesh_path_glb, tri_fea_2 = triplane_feature2)
73
  # # Build KDTree from original verts
74
  # tree = cKDTree(orig_verts)
75
 
 
80
  # new_colors = orig_colors[idxs[:, 0]]
81
 
82
  # Create the new mesh with colors
83
+ # mesh = trimesh.Trimesh(vertices=verts, faces=faces, vertex_colors=orig_colors)
84
 
85
  # === Export OBJ/MTL/PNG ===
86
  obj_path = tempfile.NamedTemporaryFile(suffix=".obj", delete=False).name