Spaces:
Runtime error
Runtime error
jadechoghari
commited on
update render_camera
Browse files
app.py
CHANGED
@@ -42,29 +42,73 @@ def preprocess_image(image, source_size):
|
|
42 |
image = torch.clamp(image, 0, 1)
|
43 |
return image
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
#Ref: https://github.com/jadechoghari/vfusion3d/blob/main/lrm/inferrer.py
|
46 |
def generate_mesh(image, source_size=512, render_size=384, mesh_size=512, export_mesh=True):
|
47 |
image = preprocess_image(image, source_size).to(model_wrapper.device)
|
48 |
-
|
49 |
-
# TODO:
|
50 |
-
|
51 |
-
|
52 |
-
render_camera = torch.tensor([[0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1]], dtype=torch.float32).to(model_wrapper.device)
|
53 |
|
54 |
with torch.no_grad():
|
55 |
planes = model_wrapper.forward(image, source_camera)
|
56 |
-
|
57 |
if export_mesh:
|
58 |
grid_out = model_wrapper.model.synthesizer.forward_grid(planes=planes, grid_size=mesh_size)
|
59 |
-
|
60 |
vtx, faces = mcubes.marching_cubes(grid_out['sigma'].float().squeeze(0).squeeze(-1).cpu().numpy(), 1.0)
|
61 |
vtx = vtx / (mesh_size - 1) * 2 - 1
|
62 |
vtx_tensor = torch.tensor(vtx, dtype=torch.float32, device=model_wrapper.device).unsqueeze(0)
|
63 |
vtx_colors = model_wrapper.model.synthesizer.forward_points(planes, vtx_tensor)['rgb'].float().squeeze(0).cpu().numpy()
|
64 |
-
|
65 |
vtx_colors = (vtx_colors * 255).astype(np.uint8)
|
66 |
mesh = trimesh.Trimesh(vertices=vtx, faces=faces, vertex_colors=vtx_colors)
|
67 |
-
|
68 |
mesh_path = "awesome_mesh.obj"
|
69 |
mesh.export(mesh_path, 'obj')
|
70 |
return mesh_path
|
|
|
42 |
image = torch.clamp(image, 0, 1)
|
43 |
return image
|
44 |
|
45 |
+
def get_normalized_camera_intrinsics(intrinsics: torch.Tensor):
|
46 |
+
"""
|
47 |
+
intrinsics: (N, 3, 2), [[fx, fy], [cx, cy], [width, height]]
|
48 |
+
Return batched fx, fy, cx, cy
|
49 |
+
"""
|
50 |
+
fx, fy = intrinsics[:, 0, 0], intrinsics[:, 0, 1]
|
51 |
+
cx, cy = intrinsics[:, 1, 0], intrinsics[:, 1, 1]
|
52 |
+
width, height = intrinsics[:, 2, 0], intrinsics[:, 2, 1]
|
53 |
+
fx, fy = fx / width, fy / height
|
54 |
+
cx, cy = cx / width, cy / height
|
55 |
+
return fx, fy, cx, cy
|
56 |
+
|
57 |
+
|
58 |
+
def build_camera_principle(RT: torch.Tensor, intrinsics: torch.Tensor):
|
59 |
+
"""
|
60 |
+
RT: (N, 3, 4)
|
61 |
+
intrinsics: (N, 3, 2), [[fx, fy], [cx, cy], [width, height]]
|
62 |
+
"""
|
63 |
+
fx, fy, cx, cy = get_normalized_camera_intrinsics(intrinsics)
|
64 |
+
return torch.cat([
|
65 |
+
RT.reshape(-1, 12),
|
66 |
+
fx.unsqueeze(-1), fy.unsqueeze(-1), cx.unsqueeze(-1), cy.unsqueeze(-1),
|
67 |
+
], dim=-1)
|
68 |
+
|
69 |
+
|
70 |
+
def _default_intrinsics():
|
71 |
+
fx = fy = 384
|
72 |
+
cx = cy = 256
|
73 |
+
w = h = 512
|
74 |
+
intrinsics = torch.tensor([
|
75 |
+
[fx, fy],
|
76 |
+
[cx, cy],
|
77 |
+
[w, h],
|
78 |
+
], dtype=torch.float32)
|
79 |
+
return intrinsics
|
80 |
+
|
81 |
+
def _default_source_camera(batch_size: int = 1):
|
82 |
+
dist_to_center = 1.5
|
83 |
+
canonical_camera_extrinsics = torch.tensor([[
|
84 |
+
[0, 0, 1, 1],
|
85 |
+
[1, 0, 0, 0],
|
86 |
+
[0, 1, 0, 0],
|
87 |
+
]], dtype=torch.float32)
|
88 |
+
canonical_camera_intrinsics = _default_intrinsics().unsqueeze(0)
|
89 |
+
source_camera = build_camera_principle(canonical_camera_extrinsics, canonical_camera_intrinsics)
|
90 |
+
return source_camera.repeat(batch_size, 1)
|
91 |
+
|
92 |
+
|
93 |
#Ref: https://github.com/jadechoghari/vfusion3d/blob/main/lrm/inferrer.py
|
94 |
def generate_mesh(image, source_size=512, render_size=384, mesh_size=512, export_mesh=True):
|
95 |
image = preprocess_image(image, source_size).to(model_wrapper.device)
|
96 |
+
source_camera = _default_source_camera(batch_size=1).to(model_wrapper.device)
|
97 |
+
# TODO: export video we need render_camera
|
98 |
+
# render_camera = _default_render_cameras(batch_size=1).to(model_wrapper.device)
|
|
|
|
|
99 |
|
100 |
with torch.no_grad():
|
101 |
planes = model_wrapper.forward(image, source_camera)
|
102 |
+
|
103 |
if export_mesh:
|
104 |
grid_out = model_wrapper.model.synthesizer.forward_grid(planes=planes, grid_size=mesh_size)
|
|
|
105 |
vtx, faces = mcubes.marching_cubes(grid_out['sigma'].float().squeeze(0).squeeze(-1).cpu().numpy(), 1.0)
|
106 |
vtx = vtx / (mesh_size - 1) * 2 - 1
|
107 |
vtx_tensor = torch.tensor(vtx, dtype=torch.float32, device=model_wrapper.device).unsqueeze(0)
|
108 |
vtx_colors = model_wrapper.model.synthesizer.forward_points(planes, vtx_tensor)['rgb'].float().squeeze(0).cpu().numpy()
|
|
|
109 |
vtx_colors = (vtx_colors * 255).astype(np.uint8)
|
110 |
mesh = trimesh.Trimesh(vertices=vtx, faces=faces, vertex_colors=vtx_colors)
|
111 |
+
|
112 |
mesh_path = "awesome_mesh.obj"
|
113 |
mesh.export(mesh_path, 'obj')
|
114 |
return mesh_path
|