Delete render.py
Browse files
render.py
DELETED
|
@@ -1,124 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import torch
|
| 3 |
-
import json
|
| 4 |
-
import numpy as np
|
| 5 |
-
from tqdm import tqdm
|
| 6 |
-
from argparse import ArgumentParser
|
| 7 |
-
import torchvision
|
| 8 |
-
from pathlib import Path
|
| 9 |
-
|
| 10 |
-
# 假设你已有这些模块
|
| 11 |
-
from gaussian_renderer import render, GaussianModel
|
| 12 |
-
from scene.cameras import Camera
|
| 13 |
-
from utils.graphics_utils import getWorld2View2, getProjectionMatrix
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def load_cameras_from_json(json_path):
|
| 17 |
-
"""从cameras.json加载相机参数"""
|
| 18 |
-
with open(json_path, 'r') as f:
|
| 19 |
-
camera_data = json.load(f)
|
| 20 |
-
|
| 21 |
-
cameras = []
|
| 22 |
-
for cam in camera_data:
|
| 23 |
-
# 根据你的cameras.json格式构建Camera对象
|
| 24 |
-
# 这里需要根据实际的json结构调整
|
| 25 |
-
camera = Camera(
|
| 26 |
-
colmap_id=cam.get('id', 0),
|
| 27 |
-
R=np.array(cam['rotation']),
|
| 28 |
-
T=np.array(cam['position']),
|
| 29 |
-
FoVx=cam['fov_x'],
|
| 30 |
-
FoVy=cam['fov_y'],
|
| 31 |
-
image=None, # 渲染时不需要GT图像
|
| 32 |
-
gt_alpha_mask=None,
|
| 33 |
-
image_name=cam.get('img_name', f"frame_{cam['id']:04d}"),
|
| 34 |
-
uid=cam['id'],
|
| 35 |
-
data_device='cuda'
|
| 36 |
-
)
|
| 37 |
-
cameras.append(camera)
|
| 38 |
-
|
| 39 |
-
return cameras
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
def load_gaussian_model(ply_path, sh_degree=3):
|
| 43 |
-
"""加载训练好的高斯模型"""
|
| 44 |
-
gaussians = GaussianModel(sh_degree)
|
| 45 |
-
gaussians.load_ply(ply_path)
|
| 46 |
-
return gaussians
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
def render_cameras(cameras, gaussians, output_dir, gpu_id=0, bg_color=[0, 0, 0]):
|
| 50 |
-
"""渲染指定相机视角"""
|
| 51 |
-
# 设置GPU
|
| 52 |
-
device = f'cuda:{gpu_id}'
|
| 53 |
-
torch.cuda.set_device(device)
|
| 54 |
-
|
| 55 |
-
# 创建输出目录
|
| 56 |
-
output_path = Path(output_dir)
|
| 57 |
-
output_path.mkdir(parents=True, exist_ok=True)
|
| 58 |
-
|
| 59 |
-
# 设置背景颜色
|
| 60 |
-
background = torch.tensor(bg_color, dtype=torch.float32, device=device)
|
| 61 |
-
|
| 62 |
-
# 渲染每个相机视角
|
| 63 |
-
rendered_images = []
|
| 64 |
-
for idx, camera in enumerate(tqdm(cameras, desc="Rendering")):
|
| 65 |
-
# 渲染
|
| 66 |
-
with torch.no_grad():
|
| 67 |
-
rendering = render(camera, gaussians, pipeline=None, background=background)["render"]
|
| 68 |
-
|
| 69 |
-
# 保存图像
|
| 70 |
-
img_name = f"{camera.image_name}.png" if hasattr(camera, 'image_name') else f"render_{idx:04d}.png"
|
| 71 |
-
save_path = output_path / img_name
|
| 72 |
-
torchvision.utils.save_image(rendering, save_path)
|
| 73 |
-
|
| 74 |
-
rendered_images.append(rendering)
|
| 75 |
-
|
| 76 |
-
print(f"渲染完成!图像保存至: {output_path}")
|
| 77 |
-
return rendered_images
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
def main():
|
| 81 |
-
parser = ArgumentParser(description="简化版3DGS渲染脚本")
|
| 82 |
-
|
| 83 |
-
# 核心参数
|
| 84 |
-
parser.add_argument("--cameras_json", type=str, required=True, help="相机参数JSON文件路径")
|
| 85 |
-
parser.add_argument("--ply_file", type=str, required=True, help="训练好的PLY模型文件路径")
|
| 86 |
-
parser.add_argument("--output_dir", type=str, required=True, help="渲染结果保存路径")
|
| 87 |
-
parser.add_argument("--gpu_id", type=int, default=0, help="使用的GPU ID")
|
| 88 |
-
|
| 89 |
-
# 可选参数
|
| 90 |
-
parser.add_argument("--sh_degree", type=int, default=3, help="球谐函数阶数")
|
| 91 |
-
parser.add_argument("--white_background", action="store_true", help="使用白色背景")
|
| 92 |
-
|
| 93 |
-
args = parser.parse_args()
|
| 94 |
-
|
| 95 |
-
# 设置背景颜色
|
| 96 |
-
bg_color = [1, 1, 1] if args.white_background else [0, 0, 0]
|
| 97 |
-
|
| 98 |
-
print(f"加载相机参数: {args.cameras_json}")
|
| 99 |
-
cameras = load_cameras_from_json(args.cameras_json)
|
| 100 |
-
|
| 101 |
-
print(f"加载高斯模型: {args.ply_file}")
|
| 102 |
-
gaussians = load_gaussian_model(args.ply_file, args.sh_degree)
|
| 103 |
-
|
| 104 |
-
print(f"开始渲染,使用GPU {args.gpu_id}")
|
| 105 |
-
render_cameras(
|
| 106 |
-
cameras=cameras,
|
| 107 |
-
gaussians=gaussians,
|
| 108 |
-
output_dir=args.output_dir,
|
| 109 |
-
gpu_id=args.gpu_id,
|
| 110 |
-
bg_color=bg_color
|
| 111 |
-
)
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
if __name__ == "__main__":
|
| 115 |
-
main()
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
# 使用示例:
|
| 119 |
-
# python render.py \
|
| 120 |
-
# --cameras_json /path/to/cameras.json \
|
| 121 |
-
# --ply_file /path/to/model.ply \
|
| 122 |
-
# --output_dir /path/to/output \
|
| 123 |
-
# --gpu_id 0 \
|
| 124 |
-
# --white_background
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|