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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
from tqdm import tqdm
|
| 4 |
+
from argparse import ArgumentParser
|
| 5 |
+
import torchvision
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
# 使用原代码中的模块
|
| 9 |
+
from scene import Scene
|
| 10 |
+
from gaussian_renderer import render, GaussianModel
|
| 11 |
+
from arguments import ModelParams, PipelineParams
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def render_from_cameras(source_path, ply_path, output_dir, gpu_id=0,
|
| 15 |
+
white_background=False, sh_degree=3, resolution=1,
|
| 16 |
+
use_train_cameras=False):
|
| 17 |
+
"""
|
| 18 |
+
从指定的数据集和PLY文件渲染图像
|
| 19 |
+
|
| 20 |
+
Args:
|
| 21 |
+
source_path: 数据集路径(包含相机参数,如sparse/0/或transforms_train.json)
|
| 22 |
+
ply_path: 训练好的PLY模型文件路径(可选,如果不提供则使用Scene中的默认加载)
|
| 23 |
+
output_dir: 渲染结果保存路径
|
| 24 |
+
gpu_id: 使用的GPU ID
|
| 25 |
+
white_background: 是否使用白色背景
|
| 26 |
+
sh_degree: 球谐函数阶数
|
| 27 |
+
resolution: 分辨率缩放因子
|
| 28 |
+
use_train_cameras: 是否使用训练集相机(默认使用测试集)
|
| 29 |
+
"""
|
| 30 |
+
# 设置GPU
|
| 31 |
+
device = f'cuda:{gpu_id}'
|
| 32 |
+
torch.cuda.set_device(device)
|
| 33 |
+
|
| 34 |
+
# 创建输出目录
|
| 35 |
+
output_path = Path(output_dir)
|
| 36 |
+
output_path.mkdir(parents=True, exist_ok=True)
|
| 37 |
+
|
| 38 |
+
# 设置背景颜色
|
| 39 |
+
bg_color = [1, 1, 1] if white_background else [0, 0, 0]
|
| 40 |
+
background = torch.tensor(bg_color, dtype=torch.float32, device=device)
|
| 41 |
+
|
| 42 |
+
# 构造参数对象(模拟ModelParams)
|
| 43 |
+
class SimpleArgs:
|
| 44 |
+
def __init__(self):
|
| 45 |
+
self.source_path = source_path
|
| 46 |
+
self.model_path = source_path # 通常model_path和source_path相同
|
| 47 |
+
self.sh_degree = sh_degree
|
| 48 |
+
self.resolution = resolution
|
| 49 |
+
self.white_background = white_background
|
| 50 |
+
self.data_device = device
|
| 51 |
+
self.eval = True
|
| 52 |
+
self.images = "images" # 默认图像文件夹名
|
| 53 |
+
self.load_allres = False
|
| 54 |
+
|
| 55 |
+
args = SimpleArgs()
|
| 56 |
+
|
| 57 |
+
# 初始化高斯模型
|
| 58 |
+
print(f"初始化高斯模型 (SH degree: {sh_degree})")
|
| 59 |
+
gaussians = GaussianModel(sh_degree)
|
| 60 |
+
|
| 61 |
+
# 如果提供了外部PLY文件,先加载它
|
| 62 |
+
if ply_path and os.path.exists(ply_path):
|
| 63 |
+
print(f"从外部文件加载高斯模型: {ply_path}")
|
| 64 |
+
gaussians.load_ply(ply_path)
|
| 65 |
+
# 创建Scene但不让它加载PLY(通过设置load_iteration=None且不让Scene初始化gaussians)
|
| 66 |
+
scene = Scene(args, gaussians, load_iteration=None, shuffle=False)
|
| 67 |
+
else:
|
| 68 |
+
# 让Scene自动处理(会从point_cloud目录加载或从点云创建)
|
| 69 |
+
print(f"从数据集加载场景: {source_path}")
|
| 70 |
+
scene = Scene(args, gaussians, load_iteration=-1, shuffle=False)
|
| 71 |
+
|
| 72 |
+
# 获取相机
|
| 73 |
+
if use_train_cameras:
|
| 74 |
+
cameras = scene.getTrainCameras(scale=resolution)
|
| 75 |
+
camera_type = "训练集"
|
| 76 |
+
else:
|
| 77 |
+
cameras = scene.getTestCameras(scale=resolution)
|
| 78 |
+
camera_type = "测试集"
|
| 79 |
+
|
| 80 |
+
print(f"加载了 {len(cameras)} 个{camera_type}相机视角")
|
| 81 |
+
|
| 82 |
+
# 创建pipeline参数(如果需要)
|
| 83 |
+
class SimplePipeline:
|
| 84 |
+
def __init__(self):
|
| 85 |
+
self.convert_SHs_python = False
|
| 86 |
+
self.compute_cov3D_python = False
|
| 87 |
+
self.debug = False
|
| 88 |
+
|
| 89 |
+
pipeline = SimplePipeline()
|
| 90 |
+
|
| 91 |
+
# 渲染每个相机视角
|
| 92 |
+
for idx, camera in enumerate(tqdm(cameras, desc="渲染进度")):
|
| 93 |
+
with torch.no_grad():
|
| 94 |
+
rendering = render(camera, gaussians, pipeline, background)["render"]
|
| 95 |
+
|
| 96 |
+
# 保存图像
|
| 97 |
+
img_name = f"{camera.image_name}.png"
|
| 98 |
+
save_path = output_path / img_name
|
| 99 |
+
torchvision.utils.save_image(rendering, save_path)
|
| 100 |
+
|
| 101 |
+
print(f"\n渲染完成!图像保存至: {output_path}")
|
| 102 |
+
print(f"共渲染 {len(cameras)} 张图像")
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def main():
|
| 106 |
+
parser = ArgumentParser(description="简化版3DGS渲染脚本")
|
| 107 |
+
|
| 108 |
+
# 核心参数
|
| 109 |
+
parser.add_argument("--source_path", type=str, required=True,
|
| 110 |
+
help="数据集路径(包含sparse/、transforms_train.json或metadata.json)")
|
| 111 |
+
parser.add_argument("--ply_file", type=str, default=None,
|
| 112 |
+
help="训练好的PLY模型文件路径(可选,不提供则从point_cloud/目录自动加载)")
|
| 113 |
+
parser.add_argument("--output_dir", type=str, required=True,
|
| 114 |
+
help="渲染结果保存路径")
|
| 115 |
+
parser.add_argument("--gpu_id", type=int, default=0,
|
| 116 |
+
help="使用的GPU ID,默认0")
|
| 117 |
+
|
| 118 |
+
# 可选参数
|
| 119 |
+
parser.add_argument("--sh_degree", type=int, default=3,
|
| 120 |
+
help="球谐函数阶数,默认3")
|
| 121 |
+
parser.add_argument("--resolution", type=int, default=1,
|
| 122 |
+
help="分辨率缩放因子,默认1(原分辨率)")
|
| 123 |
+
parser.add_argument("--white_background", action="store_true",
|
| 124 |
+
help="使用白色背景(默认黑色)")
|
| 125 |
+
parser.add_argument("--use_train_cameras", action="store_true",
|
| 126 |
+
help="使用训练集相机(默认使用测试集)")
|
| 127 |
+
|
| 128 |
+
args = parser.parse_args()
|
| 129 |
+
|
| 130 |
+
# 打印配置信息
|
| 131 |
+
print("=" * 60)
|
| 132 |
+
print("3D Gaussian Splatting 渲染工具")
|
| 133 |
+
print("=" * 60)
|
| 134 |
+
print(f"数据集路径: {args.source_path}")
|
| 135 |
+
print(f"PLY文件: {args.ply_file if args.ply_file else '自动加载'}")
|
| 136 |
+
print(f"输出目录: {args.output_dir}")
|
| 137 |
+
print(f"GPU ID: {args.gpu_id}")
|
| 138 |
+
print(f"SH阶数: {args.sh_degree}")
|
| 139 |
+
print(f"分辨率缩放: {args.resolution}")
|
| 140 |
+
print(f"背景颜色: {'白色' if args.white_background else '黑色'}")
|
| 141 |
+
print(f"相机类型: {'训练集' if args.use_train_cameras else '测试集'}")
|
| 142 |
+
print("=" * 60)
|
| 143 |
+
print()
|
| 144 |
+
|
| 145 |
+
render_from_cameras(
|
| 146 |
+
source_path=args.source_path,
|
| 147 |
+
ply_path=args.ply_file,
|
| 148 |
+
output_dir=args.output_dir,
|
| 149 |
+
gpu_id=args.gpu_id,
|
| 150 |
+
white_background=args.white_background,
|
| 151 |
+
sh_degree=args.sh_degree,
|
| 152 |
+
resolution=args.resolution,
|
| 153 |
+
use_train_cameras=args.use_train_cameras
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
if __name__ == "__main__":
|
| 158 |
+
main()
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
"""
|
| 162 |
+
使用说明:
|
| 163 |
+
=========
|
| 164 |
+
|
| 165 |
+
1. 使用COLMAP格式数据集:
|
| 166 |
+
python render_simple.py \
|
| 167 |
+
--source_path /path/to/dataset \
|
| 168 |
+
--ply_file /path/to/point_cloud.ply \
|
| 169 |
+
--output_dir ./output \
|
| 170 |
+
--gpu_id 0
|
| 171 |
+
|
| 172 |
+
2. 使用Blender格式数据集:
|
| 173 |
+
python render_simple.py \
|
| 174 |
+
--source_path /path/to/blender_dataset \
|
| 175 |
+
--ply_file /path/to/point_cloud.ply \
|
| 176 |
+
--output_dir ./output \
|
| 177 |
+
--white_background
|
| 178 |
+
|
| 179 |
+
3. 自动加载已训练模型(不指定ply_file):
|
| 180 |
+
python render_simple.py \
|
| 181 |
+
--source_path /path/to/dataset \
|
| 182 |
+
--output_dir ./output
|
| 183 |
+
|
| 184 |
+
需要的文件结构:
|
| 185 |
+
================
|
| 186 |
+
|
| 187 |
+
COLMAP格式:
|
| 188 |
+
dataset/
|
| 189 |
+
├── sparse/
|
| 190 |
+
│ └── 0/
|
| 191 |
+
│ ├── cameras.bin
|
| 192 |
+
│ ├── images.bin
|
| 193 |
+
│ └── points3D.bin
|
| 194 |
+
└── images/
|
| 195 |
+
├── img_001.jpg
|
| 196 |
+
└── ...
|
| 197 |
+
|
| 198 |
+
Blender格式:
|
| 199 |
+
dataset/
|
| 200 |
+
├── transforms_train.json
|
| 201 |
+
├── transforms_test.json
|
| 202 |
+
└── train/
|
| 203 |
+
├── r_0.png
|
| 204 |
+
└── ...
|
| 205 |
+
|
| 206 |
+
如果使用自动加载(不指定--ply_file):
|
| 207 |
+
dataset/
|
| 208 |
+
└── point_cloud/
|
| 209 |
+
└── iteration_30000/
|
| 210 |
+
└── point_cloud.ply
|
| 211 |
+
"""
|