LTT commited on
Commit
d90686a
·
verified ·
1 Parent(s): bca10b4

Update models/ISOMER/scripts/project_mesh.py

Browse files
models/ISOMER/scripts/project_mesh.py CHANGED
@@ -91,7 +91,7 @@ class Pix2FacesRenderer:
91
  vertices = vertices * torch.tensor([-1, -1, 1]).to(vertices)
92
  vertices = torch.cat([vertices, torch.ones_like(vertices[..., :1])], dim=-1).to(torch.float32)
93
  return vertices
94
-
95
  def render_pix2faces_nvdiff(self, meshes: Meshes, cameras: CamerasBase, H=512, W=512):
96
  meshes = meshes.to(self.device)
97
  cameras = cameras.to(self.device)
@@ -110,7 +110,7 @@ def get_visible_faces(meshes: Meshes, cameras: CamerasBase, resolution=1024):
110
  unique_faces = torch.unique(pix_to_face.flatten())
111
  unique_faces = unique_faces[unique_faces != -1]
112
  return unique_faces
113
-
114
  def project_color(meshes: Meshes, cameras: CamerasBase, pil_image: Image.Image, use_alpha=True, eps=0.05, resolution=1024, device="cuda") -> dict:
115
  """
116
  Projects color from a given image onto a 3D mesh.
@@ -180,6 +180,7 @@ def project_color(meshes: Meshes, cameras: CamerasBase, pil_image: Image.Image,
180
  "cos_angles": cos_angles,
181
  }
182
 
 
183
  def complete_unseen_vertex_color(meshes: Meshes, valid_index: torch.Tensor) -> dict:
184
  """
185
  meshes: the mesh with vertex color to be completed.
@@ -228,6 +229,7 @@ def complete_unseen_vertex_color(meshes: Meshes, valid_index: torch.Tensor) -> d
228
  meshes.textures = TexturesVertex(verts_features=[colors])
229
  return meshes
230
 
 
231
  def load_glb_mesh(glb_path, device="cuda"):
232
  meshes = load_objs_as_meshes([glb_path], device=device)
233
  return meshes
@@ -240,6 +242,7 @@ def get_separated_images_from_img_grid(img_grid_path, image_num):
240
  img_list.append(grid.crop((i*h, 0, i*h + h, h)))
241
  return img_list
242
 
 
243
  def get_fov_camera_(azimuth, elevation, fovy, radius, mesh, auto_center, scale_factor, device='cuda'):
244
  if auto_center:
245
  verts = mesh.verts_packed()
@@ -255,6 +258,7 @@ def get_fov_camera_(azimuth, elevation, fovy, radius, mesh, auto_center, scale_f
255
  cameras = FoVPerspectiveCameras(device=device, R=R, T=T, fov=fovy)
256
  return cameras
257
 
 
258
  def multiview_color_projection(meshes: Meshes, image_list: List[Image.Image], cameras_list: List[CamerasBase], weights=None, eps=0.05, resolution=1024, device="cuda", reweight_with_cosangle="square", use_alpha=True, confidence_threshold=0.1, complete_unseen=False, below_confidence_strategy="smooth") -> Meshes:
259
  """
260
  Projects color from a given image onto a 3D mesh.
@@ -327,6 +331,7 @@ def multiview_color_projection(meshes: Meshes, image_list: List[Image.Image], ca
327
  del meshes
328
  return ret_mesh
329
 
 
330
  def get_cameras_list(azim_list, device, elevation, fov_in_degrees=None, focal=2/1.35, dist=1.1, cam_type='orthographic'):
331
  ret = []
332
  for azim in azim_list:
@@ -336,6 +341,7 @@ def get_cameras_list(azim_list, device, elevation, fov_in_degrees=None, focal=2/
336
  ret.append(cameras)
337
  return ret
338
 
 
339
  def get_cameras_list_azi_ele(azim_list, elev_list, device, fov_in_degrees=None, focal=2/1.35, dist=1.1, cam_type='orthographic'):
340
  ret = []
341
  for i in range(len(azim_list)):
@@ -345,18 +351,23 @@ def get_cameras_list_azi_ele(azim_list, elev_list, device, fov_in_degrees=None,
345
  ret.append(cameras)
346
  return ret
347
 
 
348
  def get_8view_cameras(device, focal=2/1.35):
349
  return get_cameras_list(azim_list = [180, 225, 270, 315, 0, 45, 90, 135], elevation=0, device=device, focal=focal)
350
 
 
351
  def get_6view_cameras(device, focal=2/1.35):
352
  return get_cameras_list(azim_list = [180, 225, 270, 0, 90, 135], elevation=0, device=device, focal=focal)
353
 
 
354
  def get_4view_cameras(device, focal=2/1.35):
355
  return get_cameras_list(azim_list = [180, 270, 0, 90], elevation=0, device=device, focal=focal)
356
 
 
357
  def get_2view_cameras(device, focal=2/1.35):
358
  return get_cameras_list(azim_list = [180, 0], elevation=0, device=device, focal=focal)
359
 
 
360
  def get_multiple_view_cameras(device, focal=2/1.35, offset=180, num_views=8, dist=1.1):
361
  return get_cameras_list(azim_list = (np.linspace(0, 360, num_views+1)[:-1] + offset) % 360, elevation=0, device=device, focal=focal, dist=dist)
362
 
 
91
  vertices = vertices * torch.tensor([-1, -1, 1]).to(vertices)
92
  vertices = torch.cat([vertices, torch.ones_like(vertices[..., :1])], dim=-1).to(torch.float32)
93
  return vertices
94
+ @spaces.GPU
95
  def render_pix2faces_nvdiff(self, meshes: Meshes, cameras: CamerasBase, H=512, W=512):
96
  meshes = meshes.to(self.device)
97
  cameras = cameras.to(self.device)
 
110
  unique_faces = torch.unique(pix_to_face.flatten())
111
  unique_faces = unique_faces[unique_faces != -1]
112
  return unique_faces
113
+ @spaces.GPU
114
  def project_color(meshes: Meshes, cameras: CamerasBase, pil_image: Image.Image, use_alpha=True, eps=0.05, resolution=1024, device="cuda") -> dict:
115
  """
116
  Projects color from a given image onto a 3D mesh.
 
180
  "cos_angles": cos_angles,
181
  }
182
 
183
+ @spaces.GPU
184
  def complete_unseen_vertex_color(meshes: Meshes, valid_index: torch.Tensor) -> dict:
185
  """
186
  meshes: the mesh with vertex color to be completed.
 
229
  meshes.textures = TexturesVertex(verts_features=[colors])
230
  return meshes
231
 
232
+ @spaces.GPU
233
  def load_glb_mesh(glb_path, device="cuda"):
234
  meshes = load_objs_as_meshes([glb_path], device=device)
235
  return meshes
 
242
  img_list.append(grid.crop((i*h, 0, i*h + h, h)))
243
  return img_list
244
 
245
+ @spaces.GPU
246
  def get_fov_camera_(azimuth, elevation, fovy, radius, mesh, auto_center, scale_factor, device='cuda'):
247
  if auto_center:
248
  verts = mesh.verts_packed()
 
258
  cameras = FoVPerspectiveCameras(device=device, R=R, T=T, fov=fovy)
259
  return cameras
260
 
261
+ @spaces.GPU
262
  def multiview_color_projection(meshes: Meshes, image_list: List[Image.Image], cameras_list: List[CamerasBase], weights=None, eps=0.05, resolution=1024, device="cuda", reweight_with_cosangle="square", use_alpha=True, confidence_threshold=0.1, complete_unseen=False, below_confidence_strategy="smooth") -> Meshes:
263
  """
264
  Projects color from a given image onto a 3D mesh.
 
331
  del meshes
332
  return ret_mesh
333
 
334
+ @spaces.GPU
335
  def get_cameras_list(azim_list, device, elevation, fov_in_degrees=None, focal=2/1.35, dist=1.1, cam_type='orthographic'):
336
  ret = []
337
  for azim in azim_list:
 
341
  ret.append(cameras)
342
  return ret
343
 
344
+ @spaces.GPU
345
  def get_cameras_list_azi_ele(azim_list, elev_list, device, fov_in_degrees=None, focal=2/1.35, dist=1.1, cam_type='orthographic'):
346
  ret = []
347
  for i in range(len(azim_list)):
 
351
  ret.append(cameras)
352
  return ret
353
 
354
+ @spaces.GPU
355
  def get_8view_cameras(device, focal=2/1.35):
356
  return get_cameras_list(azim_list = [180, 225, 270, 315, 0, 45, 90, 135], elevation=0, device=device, focal=focal)
357
 
358
+ @spaces.GPU
359
  def get_6view_cameras(device, focal=2/1.35):
360
  return get_cameras_list(azim_list = [180, 225, 270, 0, 90, 135], elevation=0, device=device, focal=focal)
361
 
362
+ @spaces.GPU
363
  def get_4view_cameras(device, focal=2/1.35):
364
  return get_cameras_list(azim_list = [180, 270, 0, 90], elevation=0, device=device, focal=focal)
365
 
366
+ @spaces.GPU
367
  def get_2view_cameras(device, focal=2/1.35):
368
  return get_cameras_list(azim_list = [180, 0], elevation=0, device=device, focal=focal)
369
 
370
+ @spaces.GPU
371
  def get_multiple_view_cameras(device, focal=2/1.35, offset=180, num_views=8, dist=1.1):
372
  return get_cameras_list(azim_list = (np.linspace(0, 360, num_views+1)[:-1] + offset) % 360, elevation=0, device=device, focal=focal, dist=dist)
373