Update app.py
Browse files
app.py
CHANGED
|
@@ -30,7 +30,57 @@ from src.utils.image import process_image
|
|
| 30 |
|
| 31 |
os.environ["ANYSPLAT_PROCESSED"] = f"{os.getcwd()}/proprocess_results"
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def get_reconstructed_scene(outdir, model, device):
|
| 36 |
|
|
@@ -63,6 +113,7 @@ def get_reconstructed_scene(outdir, model, device):
|
|
| 63 |
)
|
| 64 |
|
| 65 |
plyfile = os.path.join(outdir, "gaussians.ply")
|
|
|
|
| 66 |
|
| 67 |
export_ply(
|
| 68 |
gaussians.means[0],
|
|
@@ -73,10 +124,12 @@ def get_reconstructed_scene(outdir, model, device):
|
|
| 73 |
Path(plyfile),
|
| 74 |
save_sh_dc_only=True,
|
| 75 |
)
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# Clean up
|
| 78 |
torch.cuda.empty_cache()
|
| 79 |
-
return
|
| 80 |
|
| 81 |
|
| 82 |
# 2) Handle uploaded video/images --> produce target_dir + images
|
|
|
|
| 30 |
|
| 31 |
os.environ["ANYSPLAT_PROCESSED"] = f"{os.getcwd()}/proprocess_results"
|
| 32 |
|
| 33 |
+
from plyfile import PlyData
|
| 34 |
+
import numpy as np
|
| 35 |
+
import argparse
|
| 36 |
+
from io import BytesIO
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def process_ply_to_splat(ply_file_path):
|
| 40 |
+
plydata = PlyData.read(ply_file_path)
|
| 41 |
+
vert = plydata["vertex"]
|
| 42 |
+
sorted_indices = np.argsort(
|
| 43 |
+
-np.exp(vert["scale_0"] + vert["scale_1"] + vert["scale_2"])
|
| 44 |
+
/ (1 + np.exp(-vert["opacity"]))
|
| 45 |
+
)
|
| 46 |
+
buffer = BytesIO()
|
| 47 |
+
for idx in sorted_indices:
|
| 48 |
+
v = plydata["vertex"][idx]
|
| 49 |
+
position = np.array([v["x"], v["y"], v["z"]], dtype=np.float32)
|
| 50 |
+
scales = np.exp(
|
| 51 |
+
np.array(
|
| 52 |
+
[v["scale_0"], v["scale_1"], v["scale_2"]],
|
| 53 |
+
dtype=np.float32,
|
| 54 |
+
)
|
| 55 |
+
)
|
| 56 |
+
rot = np.array(
|
| 57 |
+
[v["rot_0"], v["rot_1"], v["rot_2"], v["rot_3"]],
|
| 58 |
+
dtype=np.float32,
|
| 59 |
+
)
|
| 60 |
+
SH_C0 = 0.28209479177387814
|
| 61 |
+
color = np.array(
|
| 62 |
+
[
|
| 63 |
+
0.5 + SH_C0 * v["f_dc_0"],
|
| 64 |
+
0.5 + SH_C0 * v["f_dc_1"],
|
| 65 |
+
0.5 + SH_C0 * v["f_dc_2"],
|
| 66 |
+
1 / (1 + np.exp(-v["opacity"])),
|
| 67 |
+
]
|
| 68 |
+
)
|
| 69 |
+
buffer.write(position.tobytes())
|
| 70 |
+
buffer.write(scales.tobytes())
|
| 71 |
+
buffer.write((color * 255).clip(0, 255).astype(np.uint8).tobytes())
|
| 72 |
+
buffer.write(
|
| 73 |
+
((rot / np.linalg.norm(rot)) * 128 + 128)
|
| 74 |
+
.clip(0, 255)
|
| 75 |
+
.astype(np.uint8)
|
| 76 |
+
.tobytes()
|
| 77 |
+
)
|
| 78 |
|
| 79 |
+
return buffer.getvalue()
|
| 80 |
+
|
| 81 |
+
def save_splat_file(splat_data, output_path):
|
| 82 |
+
with open(output_path, "wb") as f:
|
| 83 |
+
f.write(splat_data)
|
| 84 |
|
| 85 |
def get_reconstructed_scene(outdir, model, device):
|
| 86 |
|
|
|
|
| 113 |
)
|
| 114 |
|
| 115 |
plyfile = os.path.join(outdir, "gaussians.ply")
|
| 116 |
+
splatfile = os.path.join(outdir, "gaussians.splat")
|
| 117 |
|
| 118 |
export_ply(
|
| 119 |
gaussians.means[0],
|
|
|
|
| 124 |
Path(plyfile),
|
| 125 |
save_sh_dc_only=True,
|
| 126 |
)
|
| 127 |
+
splat_data = process_ply_to_splat(plyfile)
|
| 128 |
+
save_splat_file(splat_data, splatfile)
|
| 129 |
|
| 130 |
# Clean up
|
| 131 |
torch.cuda.empty_cache()
|
| 132 |
+
return splatfile, video, depth_colored
|
| 133 |
|
| 134 |
|
| 135 |
# 2) Handle uploaded video/images --> produce target_dir + images
|