Stanislaw Szymanowicz commited on
Commit
ccc0216
1 Parent(s): d4dacc1

Remove halo and add rotation

Browse files
Files changed (1) hide show
  1. utils/app_utils.py +38 -7
utils/app_utils.py CHANGED
@@ -8,7 +8,8 @@ import os
8
  import torch
9
  from .camera_utils import get_loop_cameras
10
  from .graphics_utils import getProjectionMatrix
11
- from .general_utils import matrix_to_quaternion
 
12
 
13
  def remove_background(image, rembg_session):
14
  do_remove = True
@@ -158,14 +159,44 @@ def export_to_obj(reconstruction, ply_out_path):
158
  assert v.shape[0] == 1, "Expected batch size to be 0"
159
  reconstruction[k] = v[0]
160
 
161
- xyz = reconstruction["xyz"].detach().cpu().numpy()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
  normals = np.zeros_like(xyz)
163
- f_dc = reconstruction["features_dc"].detach().transpose(1, 2).flatten(start_dim=1).contiguous().cpu().numpy()
164
- f_rest = reconstruction["features_rest"].detach().transpose(1, 2).flatten(start_dim=1).contiguous().cpu().numpy()
165
- opacities = reconstruction["opacity"].detach().cpu().numpy()
 
 
 
 
 
 
 
 
 
 
166
  # enlarge Gaussians - otherwise transforming them to .ply results in artefacts
167
- scale = reconstruction["scaling"].detach().cpu().numpy() + np.abs(reconstruction["scaling"].detach().cpu().numpy() * 0.1)
168
- rotation = reconstruction["rotation"].detach().cpu().numpy()
169
 
170
  dtype_full = [(attribute, 'f4') for attribute in construct_list_of_attributes()]
171
 
 
8
  import torch
9
  from .camera_utils import get_loop_cameras
10
  from .graphics_utils import getProjectionMatrix
11
+ from .general_utils import matrix_to_quaternion, quaternion_raw_multiply
12
+ import math
13
 
14
  def remove_background(image, rembg_session):
15
  do_remove = True
 
159
  assert v.shape[0] == 1, "Expected batch size to be 0"
160
  reconstruction[k] = v[0]
161
 
162
+
163
+ valid_gaussians = torch.where(reconstruction["opacity"] > -2.5)[0]
164
+
165
+ # transforms for visualisation in Gradio
166
+ # ============= Transform locations =============
167
+ xyz = reconstruction["xyz"][valid_gaussians].detach().cpu().clone()
168
+ t1 = torch.tensor([[1, 0, 0],
169
+ [0, 0, 1],
170
+ [0, -1, 0]], dtype=torch.float32)
171
+ angle1 = 30 * math.pi * 2 / 360
172
+ t2 = torch.tensor([[math.cos(angle1), -math.sin(angle1), 0],
173
+ [math.sin(angle1), math.cos(angle1), 0],
174
+ [0, 0, 1]], dtype=torch.float32)
175
+ angle2 = -60 * math.pi * 2 / 360
176
+ t3 = torch.tensor([[math.cos(angle2), 0, math.sin(angle2)],
177
+ [0, 1, 0],
178
+ [-math.sin(angle2), 0, math.cos(angle2)]], dtype=torch.float32)
179
+
180
+ overall_transform_matrix = (t1 @ t2)@ t3
181
+
182
+ xyz = torch.matmul(xyz, overall_transform_matrix).numpy()
183
  normals = np.zeros_like(xyz)
184
+
185
+ # ============= Transform rotations =============
186
+ camera_transformation_matrix = overall_transform_matrix.inverse()
187
+ camera_quaternions = matrix_to_quaternion(camera_transformation_matrix).to(reconstruction["rotation"].device)
188
+ rotation = reconstruction["rotation"].clone()
189
+ rotation = rotation.unsqueeze(0)
190
+ rotation = quaternion_raw_multiply(camera_quaternions.unsqueeze(0).unsqueeze(0).expand(*rotation.shape),
191
+ rotation).squeeze(0)
192
+
193
+ f_dc = reconstruction["features_dc"][valid_gaussians].detach().transpose(1, 2).flatten(start_dim=1).contiguous().cpu().numpy()
194
+ f_rest = reconstruction["features_rest"][valid_gaussians].detach().transpose(1, 2).flatten(start_dim=1).contiguous().cpu().numpy()
195
+ opacities = reconstruction["opacity"][valid_gaussians].detach().contiguous().cpu().numpy()
196
+
197
  # enlarge Gaussians - otherwise transforming them to .ply results in artefacts
198
+ scale = (reconstruction["scaling"][valid_gaussians] + torch.abs(reconstruction["scaling"][valid_gaussians] * 0.1)).detach().contiguous().cpu().numpy()
199
+ rotation = rotation[valid_gaussians].detach().contiguous().cpu().numpy()
200
 
201
  dtype_full = [(attribute, 'f4') for attribute in construct_list_of_attributes()]
202