feat: export ASCII 3DGS scenes to SPZ
This commit is contained in:
parent
6548e8bc33
commit
db727b8942
@ -1,3 +1,5 @@
|
||||
FROM node:20.19.2-bookworm-slim@sha256:7cd3fbc830c75c92256fe1122002add9a1c025831af8770cd0bf8e45688ef661 AS node-runtime
|
||||
|
||||
FROM pytorch/pytorch:2.2.0-cuda12.1-cudnn8-runtime
|
||||
|
||||
ARG ANYSPLAT_COMMIT=5f5e208a7dd57d52e43ea0d553a95eab526e8775
|
||||
@ -86,12 +88,16 @@ RUN git clone --filter=blob:none --no-checkout https://github.com/Motphys/motrix
|
||||
ln -s /opt/conda/bin/python /opt/real2sim_paper_runtime/envs/anysplat/bin/python && \
|
||||
ln -s /opt/conda/bin/pip /opt/real2sim_paper_runtime/envs/anysplat/bin/pip
|
||||
|
||||
COPY --from=node-runtime /usr/local/bin/node /usr/local/bin/node
|
||||
RUN test "$(node --version)" = "v20.19.2"
|
||||
|
||||
COPY src ./src
|
||||
COPY scripts ./scripts
|
||||
COPY vendor/gs_playground /opt/gs_playground
|
||||
RUN python -m pip install --no-cache-dir --no-deps .
|
||||
|
||||
RUN python -c 'import mujoco, motrixsim, torch; from huggingface_hub import snapshot_download; assert torch.__version__.startswith("2.2")' && \
|
||||
test "$(node --version)" = "v20.19.2" && \
|
||||
PYTHONPATH=/opt/DISCOVERSE/policies/act:/opt/DISCOVERSE python -c 'from policies.act.act import ACTPolicy; assert ACTPolicy is not None' && \
|
||||
PYTHONPATH=/opt/real2sim_paper_runtime/AnySplat python -c 'from src.model.model.anysplat import AnySplat; assert AnySplat is not None' && \
|
||||
test -f /opt/gs_playground/scripts/real2sim_video_motrix_scene.py
|
||||
|
||||
@ -1,7 +1,11 @@
|
||||
import fs from "node:fs/promises";
|
||||
import { createReadStream } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { createInterface } from "node:readline";
|
||||
import { pathToFileURL } from "node:url";
|
||||
|
||||
const SH_C0 = 0.28209479177387814;
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = {};
|
||||
for (let index = 2; index < argv.length; index += 1) {
|
||||
@ -33,6 +37,106 @@ async function ensureSparkImport(vendorRoot) {
|
||||
return import(pathToFileURL(path.join(tmpRoot, "spark.module.min.js")).href);
|
||||
}
|
||||
|
||||
async function readPlyHeader(input) {
|
||||
const handle = await fs.open(input, "r");
|
||||
try {
|
||||
const buffer = Buffer.alloc(4 * 1024 * 1024);
|
||||
const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
|
||||
const text = buffer.subarray(0, bytesRead).toString("utf8");
|
||||
const end = text.indexOf("end_header");
|
||||
if (end < 0) throw new Error("PLY header exceeds 4 MiB or is missing end_header");
|
||||
const lines = text.slice(0, end + "end_header".length).split(/\r?\n/);
|
||||
let format = "";
|
||||
let activeElement = "";
|
||||
let numSplats = 0;
|
||||
const properties = [];
|
||||
for (const rawLine of lines) {
|
||||
const parts = rawLine.trim().split(/\s+/);
|
||||
if (parts[0] === "format") format = parts[1] || "";
|
||||
if (parts[0] === "element") {
|
||||
activeElement = parts[1] || "";
|
||||
if (activeElement === "vertex") numSplats = Number(parts[2] || 0);
|
||||
}
|
||||
if (parts[0] === "property" && activeElement === "vertex") {
|
||||
if (parts[1] === "list") throw new Error("list properties are not supported for Gaussian vertices");
|
||||
properties.push(parts.at(-1));
|
||||
}
|
||||
}
|
||||
if (!format || !numSplats || !properties.length) throw new Error("invalid Gaussian PLY header");
|
||||
return { format, numSplats, properties };
|
||||
} finally {
|
||||
await handle.close();
|
||||
}
|
||||
}
|
||||
|
||||
function clamp01(value) {
|
||||
return Math.max(0, Math.min(1, Number(value)));
|
||||
}
|
||||
|
||||
function sigmoid(value) {
|
||||
const number = Number(value);
|
||||
if (number >= 0) return 1 / (1 + Math.exp(-number));
|
||||
const exponential = Math.exp(number);
|
||||
return exponential / (1 + exponential);
|
||||
}
|
||||
|
||||
function normalizeQuaternion(w, x, y, z) {
|
||||
const length = Math.hypot(w, x, y, z) || 1;
|
||||
return [x / length, y / length, z / length, w / length];
|
||||
}
|
||||
|
||||
async function convertAsciiPly(input, header, spark) {
|
||||
const propertyIndex = new Map(header.properties.map((name, index) => [name, index]));
|
||||
const required = ["x", "y", "z", "scale_0", "scale_1", "scale_2", "rot_0", "rot_1", "rot_2", "rot_3"];
|
||||
const missing = required.filter((name) => !propertyIndex.has(name));
|
||||
if (missing.length) throw new Error(`ASCII Gaussian PLY is missing properties: ${missing.join(", ")}`);
|
||||
const hasShColor = ["f_dc_0", "f_dc_1", "f_dc_2"].every((name) => propertyIndex.has(name));
|
||||
const hasRgbColor = ["red", "green", "blue"].every((name) => propertyIndex.has(name));
|
||||
if (!hasShColor && !hasRgbColor) throw new Error("ASCII Gaussian PLY has neither f_dc_* nor RGB color properties");
|
||||
|
||||
const writer = new spark.SpzWriter({ numSplats: header.numSplats, shDegree: 0 });
|
||||
const reader = createInterface({ input: createReadStream(input, { encoding: "utf8" }), crlfDelay: Infinity });
|
||||
let headerComplete = false;
|
||||
let written = 0;
|
||||
for await (const rawLine of reader) {
|
||||
const line = rawLine.trim();
|
||||
if (!headerComplete) {
|
||||
headerComplete = line === "end_header";
|
||||
continue;
|
||||
}
|
||||
if (!line || written >= header.numSplats) continue;
|
||||
const fields = line.split(/\s+/);
|
||||
if (fields.length < header.properties.length) throw new Error(`vertex ${written} has ${fields.length} values; expected ${header.properties.length}`);
|
||||
const value = (name) => Number(fields[propertyIndex.get(name)]);
|
||||
const numbers = required.map(value);
|
||||
if (numbers.some((number) => !Number.isFinite(number))) throw new Error(`vertex ${written} contains a non-finite required value`);
|
||||
const [quatX, quatY, quatZ, quatW] = normalizeQuaternion(value("rot_0"), value("rot_1"), value("rot_2"), value("rot_3"));
|
||||
writer.setCenter(written, value("x"), value("y"), value("z"));
|
||||
writer.setScale(
|
||||
written,
|
||||
Math.max(Math.exp(Math.max(-20, Math.min(20, value("scale_0")))), 1e-6),
|
||||
Math.max(Math.exp(Math.max(-20, Math.min(20, value("scale_1")))), 1e-6),
|
||||
Math.max(Math.exp(Math.max(-20, Math.min(20, value("scale_2")))), 1e-6),
|
||||
);
|
||||
writer.setQuat(written, quatX, quatY, quatZ, quatW);
|
||||
writer.setAlpha(written, propertyIndex.has("opacity") ? sigmoid(value("opacity")) : 1);
|
||||
if (hasShColor) {
|
||||
writer.setRgb(
|
||||
written,
|
||||
clamp01(0.5 + SH_C0 * value("f_dc_0")),
|
||||
clamp01(0.5 + SH_C0 * value("f_dc_1")),
|
||||
clamp01(0.5 + SH_C0 * value("f_dc_2")),
|
||||
);
|
||||
} else {
|
||||
const divisor = Math.max(value("red"), value("green"), value("blue")) > 1 ? 255 : 1;
|
||||
writer.setRgb(written, clamp01(value("red") / divisor), clamp01(value("green") / divisor), clamp01(value("blue") / divisor));
|
||||
}
|
||||
written += 1;
|
||||
}
|
||||
if (written !== header.numSplats) throw new Error(`ASCII Gaussian PLY declared ${header.numSplats} vertices but yielded ${written}`);
|
||||
return { writer, numSplats: header.numSplats, written };
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const args = parseArgs(process.argv);
|
||||
const input = args.input;
|
||||
@ -45,23 +149,31 @@ async function main() {
|
||||
|
||||
const started = Date.now();
|
||||
const spark = await ensureSparkImport(vendorRoot);
|
||||
const fileBytes = new Uint8Array(await fs.readFile(input));
|
||||
const reader = new spark.PlyReader({ fileBytes });
|
||||
await reader.parseHeader();
|
||||
await reader.parseData(() => {});
|
||||
const numSplats = Number(reader.numSplats || 0);
|
||||
if (!numSplats) throw new Error("Spark PlyReader returned zero splats");
|
||||
|
||||
const writer = new spark.SpzWriter({ numSplats, shDegree: 0 });
|
||||
let written = 0;
|
||||
reader.parseSplats((index, x, y, z, scaleX, scaleY, scaleZ, quatX, quatY, quatZ, quatW, opacity, red, green, blue) => {
|
||||
writer.setCenter(index, x, y, z);
|
||||
writer.setScale(index, Math.max(scaleX, 1e-6), Math.max(scaleY, 1e-6), Math.max(scaleZ, 1e-6));
|
||||
writer.setQuat(index, quatX, quatY, quatZ, quatW);
|
||||
writer.setAlpha(index, Math.max(0, Math.min(1, Number(opacity ?? 1))));
|
||||
writer.setRgb(index, Math.max(0, Math.min(1, red)), Math.max(0, Math.min(1, green)), Math.max(0, Math.min(1, blue)));
|
||||
written += 1;
|
||||
});
|
||||
const header = await readPlyHeader(input);
|
||||
let writer;
|
||||
let numSplats;
|
||||
let written;
|
||||
if (header.format === "ascii") {
|
||||
({ writer, numSplats, written } = await convertAsciiPly(input, header, spark));
|
||||
} else {
|
||||
if (header.format !== "binary_little_endian") throw new Error(`unsupported PLY format: ${header.format}`);
|
||||
const fileBytes = new Uint8Array(await fs.readFile(input));
|
||||
const reader = new spark.PlyReader({ fileBytes });
|
||||
await reader.parseHeader();
|
||||
await reader.parseData(() => {});
|
||||
numSplats = Number(reader.numSplats || 0);
|
||||
if (!numSplats) throw new Error("Spark PlyReader returned zero splats");
|
||||
writer = new spark.SpzWriter({ numSplats, shDegree: 0 });
|
||||
written = 0;
|
||||
reader.parseSplats((index, x, y, z, scaleX, scaleY, scaleZ, quatX, quatY, quatZ, quatW, opacity, red, green, blue) => {
|
||||
writer.setCenter(index, x, y, z);
|
||||
writer.setScale(index, Math.max(scaleX, 1e-6), Math.max(scaleY, 1e-6), Math.max(scaleZ, 1e-6));
|
||||
writer.setQuat(index, quatX, quatY, quatZ, quatW);
|
||||
writer.setAlpha(index, clamp01(opacity ?? 1));
|
||||
writer.setRgb(index, clamp01(red), clamp01(green), clamp01(blue));
|
||||
written += 1;
|
||||
});
|
||||
}
|
||||
const encoded = await writer.finalize();
|
||||
await fs.mkdir(path.dirname(output), { recursive: true });
|
||||
await fs.writeFile(output, encoded);
|
||||
@ -73,6 +185,7 @@ async function main() {
|
||||
output,
|
||||
splats: numSplats,
|
||||
written_splats: written,
|
||||
input_format: header.format,
|
||||
input_size_bytes: inputStat.size,
|
||||
output_size_bytes: outputStat.size,
|
||||
compression_ratio: Number((outputStat.size / Math.max(inputStat.size, 1)).toFixed(4)),
|
||||
|
||||
@ -1153,7 +1153,7 @@ def run_scene_splat_optimize(config: PlatformConfig, store: PlatformStore, task:
|
||||
"viewer_probe": {},
|
||||
"packed_output": None,
|
||||
"pack_status": "not_available",
|
||||
"recommendation": "Spark can load full 3DGS PLY manually. A native server-side SPZ/KSPLAT exporter is still required before making it the default asset.",
|
||||
"recommendation": "The full 3DGS PLY is being converted to a browser-native SPZ asset.",
|
||||
"created_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
||||
}
|
||||
_task_progress(store, task, 46, "导出SPZ", "正在把完整 3DGS PLY 转换成轻量 SPZ")
|
||||
@ -1213,7 +1213,7 @@ def _export_scene_spz(config: PlatformConfig, store: PlatformStore, task_id: str
|
||||
str(export_report_path),
|
||||
]
|
||||
try:
|
||||
completed = subprocess.run(command, cwd=str(config.app_root), text=True, capture_output=True, timeout=240)
|
||||
completed = subprocess.run(command, cwd=str(config.app_root), text=True, capture_output=True, timeout=1800)
|
||||
except Exception as exc:
|
||||
return {"ok": False, "error": f"SPZ exporter failed to run: {type(exc).__name__}: {exc}"}
|
||||
if completed.returncode != 0 or not output_path.is_file():
|
||||
|
||||
@ -91,6 +91,7 @@ def bootstrap(config: PlatformConfig) -> dict[str, Any]:
|
||||
checks["nvidia"] = _nvidia_probe()
|
||||
checks["simulators"] = _simulator_probe()
|
||||
checks["motrixsim_assets"] = _motrixsim_assets_probe()
|
||||
checks["spz_exporter"] = _spz_exporter_probe(config)
|
||||
checks["act"] = _act_probe(config)
|
||||
checks["anysplat"] = _anysplat_probe(config)
|
||||
except Exception as exc:
|
||||
@ -114,6 +115,62 @@ def bootstrap(config: PlatformConfig) -> dict[str, Any]:
|
||||
}
|
||||
|
||||
|
||||
def _spz_exporter_probe(config: PlatformConfig) -> dict[str, Any]:
|
||||
node = shutil.which("node")
|
||||
script = config.app_root / "scripts" / "convert_3dgs_ply_to_spz.mjs"
|
||||
vendor = config.app_root / "src" / "real2sim_platform" / "static" / "vendor"
|
||||
if not node or not script.is_file() or not (vendor / "spark.module.min.js").is_file():
|
||||
return {"ok": False, "node": node or "", "script": str(script), "vendor": str(vendor)}
|
||||
with tempfile.TemporaryDirectory(prefix="real2sim-spz-probe-") as temporary:
|
||||
root = Path(temporary)
|
||||
source = root / "probe.ply"
|
||||
output = root / "probe.spz"
|
||||
report = root / "report.json"
|
||||
source.write_text(
|
||||
"\n".join(
|
||||
[
|
||||
"ply",
|
||||
"format ascii 1.0",
|
||||
"element vertex 2",
|
||||
"property float x",
|
||||
"property float y",
|
||||
"property float z",
|
||||
"property float f_dc_0",
|
||||
"property float f_dc_1",
|
||||
"property float f_dc_2",
|
||||
"property float opacity",
|
||||
"property float scale_0",
|
||||
"property float scale_1",
|
||||
"property float scale_2",
|
||||
"property float rot_0",
|
||||
"property float rot_1",
|
||||
"property float rot_2",
|
||||
"property float rot_3",
|
||||
"end_header",
|
||||
"0 0 0 0.5 0 0 2 -3 -3 -3 1 0 0 0",
|
||||
"0.1 0.2 0.3 0 0.5 0 2 -3 -3 -3 1 0 0 0",
|
||||
"",
|
||||
]
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
completed = subprocess.run(
|
||||
[node, str(script), "--input", str(source), "--output", str(output), "--vendor-root", str(vendor), "--report", str(report)],
|
||||
text=True,
|
||||
capture_output=True,
|
||||
timeout=45,
|
||||
)
|
||||
payload = json.loads(report.read_text(encoding="utf-8")) if report.is_file() else {}
|
||||
return {
|
||||
"ok": completed.returncode == 0 and output.is_file() and output.stat().st_size > 0 and payload.get("splats") == 2,
|
||||
"node": subprocess.run([node, "--version"], text=True, capture_output=True, timeout=10).stdout.strip(),
|
||||
"input_format": payload.get("input_format"),
|
||||
"splats": payload.get("splats"),
|
||||
"output_size_bytes": output.stat().st_size if output.is_file() else 0,
|
||||
"stderr": completed.stderr[-500:],
|
||||
}
|
||||
|
||||
|
||||
def _nvidia_probe() -> dict[str, Any]:
|
||||
completed = subprocess.run(
|
||||
["nvidia-smi", "--query-gpu=name,memory.total,driver_version", "--format=csv,noheader"],
|
||||
|
||||
@ -14,7 +14,7 @@ from fastapi.testclient import TestClient
|
||||
from real2sim_platform.config import PlatformConfig
|
||||
from real2sim_platform.adapters import _act_task_config_source, _inspect_act_dataset, index_artifacts_for_task
|
||||
from real2sim_platform.rm65_controller import ACTION_ALIGNMENT, CONTROL_HOLD_STEPS, _write_preview_video, collect
|
||||
from real2sim_platform.runtime_gate import _motrixsim_assets_probe, runtime_key
|
||||
from real2sim_platform.runtime_gate import _motrixsim_assets_probe, _spz_exporter_probe, runtime_key
|
||||
from real2sim_platform.server import create_app
|
||||
from real2sim_platform.source_revision import read_source_revision
|
||||
from real2sim_platform.storage import BosStorage, StorageBoundaryError, normalize_relative_path
|
||||
@ -24,6 +24,15 @@ from real2sim_platform.store import PlatformStore
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def test_ascii_3dgs_spz_exporter_uses_real_spark_writer(tmp_path: Path) -> None:
|
||||
config = PlatformConfig(app_root=ROOT, workspace_dir=tmp_path / "workspace")
|
||||
probe = _spz_exporter_probe(config)
|
||||
assert probe["ok"] is True, probe
|
||||
assert probe["input_format"] == "ascii"
|
||||
assert probe["splats"] == 2
|
||||
assert probe["output_size_bytes"] > 0
|
||||
|
||||
|
||||
def test_auth_first_login_rbac_and_user_crud(tmp_path: Path) -> None:
|
||||
config = PlatformConfig(
|
||||
app_root=ROOT,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user