Spaces:
mashroo
/
Running on Zero

YoussefAnso commited on
Commit
e666402
·
1 Parent(s): 854cd53

Refactor generate3d function in inference.py to implement CCM-based UV assignment for improved texture mapping. Updated color map preparation and vertex projection to enhance mesh quality in exports, ensuring compatibility with various input formats.

Browse files
Files changed (1) hide show
  1. inference.py +26 -14
inference.py CHANGED
@@ -62,24 +62,36 @@ def generate3d(model, rgb, ccm, device):
62
  data_config['verts'] = torch.from_numpy(verts).contiguous()
63
  data_config['faces'] = torch.from_numpy(faces).contiguous()
64
 
65
- # Generate UVs using xatlas (CPU)
66
- mesh_v = data_config['verts'].cpu().numpy()
67
- mesh_f = data_config['faces'].cpu().numpy()
68
- vmapping, ft, vt = xatlas.parametrize(mesh_v, mesh_f)
69
-
70
- # Load your color map (photorealistic texture)
71
- # Assume you save the color map as a PNG somewhere, or pass it as a numpy array
72
- # If rgb is your color map, convert it to the right format:
73
- if rgb.max() > 1.0:
74
- color_map = rgb.astype(np.uint8)
75
- else:
76
- color_map = (rgb * 255).astype(np.uint8)
77
- # If rgb is already in HWC, skip transpose; otherwise, transpose as needed
78
  if color_map.shape[-1] != 3:
79
  color_map = np.transpose(color_map, (1, 2, 0))
80
  albedo = cv2.cvtColor(color_map, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
81
 
82
- tex_res = (albedo.shape[0], albedo.shape[1])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
  # Create Mesh and export .glb
85
  mesh = Mesh(
 
62
  data_config['verts'] = torch.from_numpy(verts).contiguous()
63
  data_config['faces'] = torch.from_numpy(faces).contiguous()
64
 
65
+ # --- CCM-based UV assignment ---
66
+ mesh_v = data_config['verts'].cpu().numpy() # [N, 3]
67
+ mesh_f = data_config['faces'].cpu().numpy() # [M, 3]
68
+
69
+ # Prepare CCM and color map
70
+ ccm_img = ccm.astype(np.uint8) if ccm.max() > 1 else (ccm * 255).astype(np.uint8)
71
+ if ccm_img.shape[-1] != 3:
72
+ ccm_img = np.transpose(ccm_img, (1, 2, 0))
73
+ H, W, _ = ccm_img.shape
74
+
75
+ color_map = rgb.astype(np.uint8) if rgb.max() > 1 else (rgb * 255).astype(np.uint8)
 
 
76
  if color_map.shape[-1] != 3:
77
  color_map = np.transpose(color_map, (1, 2, 0))
78
  albedo = cv2.cvtColor(color_map, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
79
 
80
+ # Project mesh vertices to CCM image space and get UVs
81
+ vt = []
82
+ for v in mesh_v:
83
+ # Assume mesh is in [-1,1] in x/y, project to CCM image
84
+ x, y, z = v
85
+ u_img = int((x + 1) / 2 * (W - 1))
86
+ v_img = int((y + 1) / 2 * (H - 1))
87
+ u_img = np.clip(u_img, 0, W-1)
88
+ v_img = np.clip(v_img, 0, H-1)
89
+ r, g, b = ccm_img[v_img, u_img]
90
+ u = r / 255.0
91
+ v_ = g / 255.0
92
+ vt.append([u, v_])
93
+ vt = np.array(vt, dtype=np.float32)
94
+ ft = mesh_f.copy()
95
 
96
  # Create Mesh and export .glb
97
  mesh = Mesh(