# # Copyright (C) 2023, Inria # GRAPHDECO research group, https://team.inria.fr/graphdeco # All rights reserved. # # This software is free for non-commercial, research and evaluation use # under the terms of the LICENSE.md file. # # For inquiries contact george.drettakis@inria.fr # import torch from scene import Scene import os import math import imageio from tqdm import tqdm from os import makedirs import numpy as np from gaussian_renderer import render import torchvision from utils.general_utils import safe_state from argparse import ArgumentParser from arguments import ModelParams, PipelineParams, get_combined_args from gaussian_renderer import GaussianModel # from load_llff_v2 import load_llff_data # from utils.sh_utils import RGB2SH from utils.graphics_utils import getWorld2View2, getProjectionMatrix import scipy.io as sio # import kornia os.environ["CUDA_VISIBLE_DEVICES"] = "6" # datadir='/home/G/zcy/gaussian-splatting-main/data/2' # render_poses= load_llff_data(datadir,4,recenter=True, bd_factor=.75,spherify=True) # render_poses = sio.loadmat('/home/G/zcy/gaussian-splatting-main_gai/w2c_mats_3.699_300_fine.mat')['w2c_mats'] render_poses = torch.tensor(np.load('/data_160TB/2021/yangkun/gaussian-splatting-main-copy/eg3d_human/test_real/kinect_data/render_path.npy')) # print(render_poses.shape) # render_poses = np.concatenate([render_poses[:, :, 1:2], render_poses[:, :, 0:1], -render_poses[:, :, 2:3], render_poses[:, :, 3:4], render_poses[:, :, 4:5]], 2) # model_path = '/home/G/zcy/gaussian-splatting-main/output/2' class Cam(): def __init__(self) -> None: self.FoVx = 0 self.FoVy = 0 self.image_height = 0 self.image_width = 0 self.world_view_transform = None self.full_proj_transform = None self.projection_matrix = None self.camera_center = None # self.original_image = None def focal2fov(focal, pixels): return 2*math.atan(pixels/(2*focal)) # def render_set(model_path, name, iteration, views, gaussians, pipeline, background): # render_path = os.path.join(model_path, name, "ours_{}".format(iteration), "renders") # gts_path = os.path.join(model_path, name, "ours_{}".format(iteration), "gt") # makedirs(render_path, exist_ok=True) # makedirs(gts_path, exist_ok=True) # for idx, view in enumerate(tqdm(views, desc="Rendering progress")): # rendering = render(view, gaussians, pipeline, background)["render"] # gt = view.original_image[0:3, :, :] # torchvision.utils.save_image(rendering, os.path.join(render_path, '{0:05d}'.format(idx) + ".png")) # torchvision.utils.save_image(gt, os.path.join(gts_path, '{0:05d}'.format(idx) + ".png")) def render_sets(dataset : ModelParams, iteration : int, pipeline : PipelineParams, skip_train : bool, skip_test : bool): with torch.no_grad(): gaussians = GaussianModel(dataset.sh_degree, dataset.data_device, dataset.xyz_grad, dataset.align, dataset.free_flame) scene = Scene(dataset, gaussians, load_iteration=iteration, shuffle=False) white_background = False # bg_color = [1,1,1] if dataset.white_background else [0, 0, 0] bg_color = [1,1,1] if white_background else [0, 0, 0] background = torch.tensor(bg_color, dtype=torch.float32, device="cuda") testcamera_list = [] if not skip_test: # render_set(dataset.model_path, "test", scene.loaded_iter, scene.getTestCameras(), gaussians, pipeline, background) for i in range(render_poses.shape[0]): sub_tensor = render_poses[i:i+1, :, :] # print(sub_tensor) # Rot = torch.tensor([ # [1, 0, 0, 0], # [0, 0, 1, 0], # [0, -1, 0, 0], # [0, 0, 0, 1] # ], dtype=torch.float32) # print(Rot.shape) # sub_tensor_Rt = torch.tensor(sub_tensor[:, :3, :4].squeeze()) # new = torch.tensor([[0, 0, 0, 1]]) # sub_tensor_Rt = torch.cat((sub_tensor_Rt, new), dim=0) # sub_tensor_Rt = torch.matmul(Rot.float(), sub_tensor_Rt.float()) # print(sub_tensor_Rt.shape) # new = torch.tensor([[0, 0, 0, 1]]) # sub_tensor_Rt = torch.cat((sub_tensor_Rt, new), dim=0) # print(sub_tensor_Rt) # inverse_matrix = torch.inverse(sub_tensor_Rt) # print(inverse_matrix) # sub_tensor_R = sub_tensor[:, :3, :3].T.squeeze().detach().cpu().numpy() sub_tensor_R = sub_tensor[:, :3, :3].squeeze().cpu().numpy() print(sub_tensor_R.shape) # sub_tensor_R_x = torch.tensor(sub_tensor_R[:,0][...,None]) # print(sub_tensor_R.shape) # sub_tensor_R_y = torch.tensor(sub_tensor_R[:,1][...,None]) # sub_tensor_R_z = torch.tensor(sub_tensor_R[:,2][...,None]) # sub_tensor_R_x1 = sub_tensor_R_x # sub_tensor_R_y1 = -sub_tensor_R_z # sub_tensor_R_z1 = sub_tensor_R_y # sub_tensor_R1 = torch.cat((sub_tensor_R_x1, sub_tensor_R_y1, sub_tensor_R_z1), dim=1).T.numpy() # sub_tensor_T = sub_tensor[:, :3, 3].detach().cpu().numpy() # np.dot(-R.T, extr.tvec) sub_tensor_T = np.dot(-sub_tensor_R.T, sub_tensor[:, :3, 3].squeeze().cpu().numpy()) # sub_tensor_T = inverse_matrix[:3, -1] print(sub_tensor_T.shape) height = 270 width = 480 focal_length_x = 932.96923828 focal_length_y = 939.74603271 # trans=np.array([0.0, 0.0, 0.0]) FovX = focal2fov(focal_length_x, width) FovY = focal2fov(focal_length_y, height) world_view_transform = torch.tensor(getWorld2View2(sub_tensor_R, sub_tensor_T, 1.0)).transpose(0, 1).cuda() projection_matrix = getProjectionMatrix(znear=0.01, zfar=100.0, fovX=FovX, fovY=FovY).transpose(0,1).cuda() full_proj_transform = (world_view_transform.unsqueeze(0).bmm(projection_matrix.unsqueeze(0))).squeeze(0) camera_center = world_view_transform.inverse()[3, :3] viewpoint_cam = Cam() viewpoint_cam.FoVx = FovX viewpoint_cam.FoVy = FovY viewpoint_cam.image_height = height viewpoint_cam.image_width = width viewpoint_cam.world_view_transform = world_view_transform viewpoint_cam.projection_matrix = projection_matrix viewpoint_cam.full_proj_transform = full_proj_transform viewpoint_cam.camera_center = camera_center # viewpoint_cam.original_image= original_image render_pkg = render(viewpoint_cam, gaussians, pipeline, background)["render"] # render_pkg = render_set(model_path, "test", scene.loaded_iter, viewpoint_cam, gaussians, pipeline, background) image = render_pkg # sigma = 1.5 # image = kornia.filters.gaussian_blur2d(image, (5, 5), (sigma, sigma)) # image = image.squeeze() to8b = lambda x: (255 * np.clip(x, 0, 1)).astype(np.uint8) image_cpu = image.cpu() image_np = image_cpu.detach().numpy() image_save = to8b(image_np) print(image_save.shape) output_path = '/data_160TB/2021/yangkun/gaussian-splatting-main-copy/checkpoints/checkpoint_realdata/testyk/frame0/output' os.makedirs(output_path, exist_ok=True) filename = output_path + "/image_" + str(i)+ ".png" imageio.imwrite(filename, np.transpose(image_save, (1, 2, 0))) # imageio.imwrite(filename,image_save)) # render_set(model_path, "test", scene.loaded_iter, scene.getTestCameras(), gaussians, pipeline, background) if __name__ == "__main__": # Set up command line argument parser parser = ArgumentParser(description="Testing script parameters") model = ModelParams(parser, sentinel=True) pipeline = PipelineParams(parser) parser.add_argument("--iteration", default=-1, type=int) parser.add_argument("--skip_train", action="store_true") parser.add_argument("--skip_test", action="store_true") parser.add_argument("--quiet", action="store_true") parser.add_argument('--output_path', type=str, default='/data_160TB/2021/yangkun/gaussian-splatting-main-copy/checkpoints/checkpoint_realdata/testyk/frame0/output', help='the path to the project root directory. Replace this path with yours!') parser.add_argument('--input_path', type=str, default='/data_160TB/2021/yangkun/gaussian-splatting-main-copy/checkpoints/checkpoint_realdata/testyk/frame0', help='the path to the project root directory. Replace this path with yours!') args = get_combined_args(parser) print("Rendering " + args.output_path) # Initialize system state (RNG) safe_state(args.quiet) render_sets(model.extract(args), args.iteration, pipeline.extract(args), args.skip_train, args.skip_test)