-
Notifications
You must be signed in to change notification settings - Fork 591
Expand file tree
/
Copy pathrender.py
More file actions
120 lines (99 loc) · 3.59 KB
/
render.py
File metadata and controls
120 lines (99 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""Contains `sharp render` CLI implementation.
For licensing see accompanying LICENSE file.
Copyright (C) 2025 Apple Inc. All Rights Reserved.
"""
from __future__ import annotations
import logging
from pathlib import Path
import click
import torch
import torch.utils.data
from sharp.utils import camera, gsplat, io
from sharp.utils import logging as logging_utils
from sharp.utils.gaussians import Gaussians3D, SceneMetaData, load_ply
LOGGER = logging.getLogger(__name__)
@click.command()
@click.option(
"-i",
"--input-path",
type=click.Path(exists=True, path_type=Path),
help="Path to the ply or a list of plys.",
required=True,
)
@click.option(
"-o",
"--output-path",
type=click.Path(path_type=Path, file_okay=False),
help="Path to save the rendered videos.",
required=True,
)
@click.option("-v", "--verbose", is_flag=True, help="Activate debug logs.")
def render_cli(input_path: Path, output_path: Path, verbose: bool):
"""Render videos from predicted Gaussians with a camera trajectory (CUDA only)."""
logging_utils.configure(logging.DEBUG if verbose else logging.INFO)
if not torch.cuda.is_available():
LOGGER.error("Rendering a checkpoint requires CUDA.")
exit(1)
output_path.mkdir(exist_ok=True, parents=True)
params = camera.TrajectoryParams()
if input_path.suffix == ".ply":
scene_paths = [input_path]
elif input_path.is_dir():
scene_paths = list(input_path.glob("*.ply"))
else:
LOGGER.error("Input path must be either directory or single PLY file.")
exit(1)
for scene_path in scene_paths:
LOGGER.info("Rendering %s", scene_path)
gaussians, metadata = load_ply(scene_path)
render_gaussians(
gaussians=gaussians,
metadata=metadata,
params=params,
output_path=(output_path / scene_path.stem).with_suffix(".mp4"),
)
def render_gaussians(
gaussians: Gaussians3D,
metadata: SceneMetaData,
output_path: Path,
params: camera.TrajectoryParams | None = None,
) -> None:
"""Render a single gaussian checkpoint file."""
(width, height) = metadata.resolution_px
f_px = metadata.focal_length_px
if params is None:
params = camera.TrajectoryParams()
if not torch.cuda.is_available():
raise RuntimeError("Rendering a checkpoint requires CUDA.")
device = torch.device("cuda")
intrinsics = torch.tensor(
[
[f_px, 0, (width - 1) / 2.0, 0],
[0, f_px, (height - 1) / 2.0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
],
device=device,
dtype=torch.float32,
)
camera_model = camera.create_camera_model(
gaussians, intrinsics, resolution_px=metadata.resolution_px
)
trajectory = camera.create_eye_trajectory(
gaussians, params, resolution_px=metadata.resolution_px, f_px=f_px
)
renderer = gsplat.GSplatRenderer(color_space=metadata.color_space)
video_writer = io.VideoWriter(output_path)
for _, eye_position in enumerate(trajectory):
camera_info = camera_model.compute(eye_position)
rendering_output = renderer(
gaussians.to(device),
extrinsics=camera_info.extrinsics[None].to(device),
intrinsics=camera_info.intrinsics[None].to(device),
image_width=camera_info.width,
image_height=camera_info.height,
)
color = (rendering_output.color[0].permute(1, 2, 0) * 255.0).to(dtype=torch.uint8)
depth = rendering_output.depth[0]
video_writer.add_frame(color, depth)
video_writer.close()