pis-spatial-intelligence-lab / tools /build_static_assets.py
pedroivospatial's picture
Add private static synthetic depth review build
573b479 verified
Raw History Blame Contribute Delete
4.58 kB
"""Build reviewed, deterministic assets for the zero-cost Static Space.
The generated images are synthetic source scenes plus pre-computed outputs from
the exact reviewed Depth Anything revision. No client or real-location data is
accepted or read by this script.
"""
from __future__ import annotations
import hashlib
import json
from pathlib import Path
import sys
import numpy as np
from transformers import pipeline
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from lab_core import SAMPLE_LABELS, _normalize_depth, colorize_depth, generate_scene
ASSET_DIR = ROOT / "assets" / "scenes"
RESULTS_PATH = ROOT / "static" / "results.json"
MODEL_ID = "depth-anything/Depth-Anything-V2-Small-hf"
MODEL_REVISION = "5426e4f0f36572d16453bbda7a8389317b1bef99"
SCENE_COPY = {
"training_room": {
"label": "Synthetic training venue",
"eyebrow": "Spatial separation",
"summary": "A procedural training floor with three abstract stations and converging room geometry.",
},
"retail_room": {
"label": "Synthetic retail room",
"eyebrow": "Layered shelving",
"summary": "A procedural retail layout with repeated shelf depth and a central display volume.",
},
"reception_room": {
"label": "Synthetic reception studio",
"eyebrow": "Foreground and backdrop",
"summary": "A procedural reception counter, backdrop and abstract foliage used to expose depth transitions.",
},
}
def file_sha256(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as stream:
for chunk in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def scene_metrics(raw_depth) -> dict[str, float | int]:
normalized = _normalize_depth(raw_depth)
grad_y, grad_x = np.gradient(normalized)
spread = float(np.std(normalized))
edge_energy = float(np.mean(np.hypot(grad_x, grad_y)))
near_far_balance = float(np.mean(normalized > 0.5))
score = int(np.clip((spread * 240.0) + (edge_energy * 900.0), 0, 100))
return {
"normalized_depth_spread": round(spread, 3),
"depth_edge_energy": round(edge_energy, 4),
"near_far_balance": round(near_far_balance, 4),
"demonstration_signal_score": score,
}
def main() -> None:
ASSET_DIR.mkdir(parents=True, exist_ok=True)
RESULTS_PATH.parent.mkdir(parents=True, exist_ok=True)
depth_pipeline = pipeline(
task="depth-estimation",
model=MODEL_ID,
revision=MODEL_REVISION,
device=-1,
)
scenes = []
for label, key in SAMPLE_LABELS.items():
source = generate_scene(key)
result = depth_pipeline(source)
raw_depth = result["depth"]
preview = colorize_depth(raw_depth)
source_path = ASSET_DIR / f"{key}-source.png"
depth_path = ASSET_DIR / f"{key}-depth.png"
source.save(source_path, format="PNG", optimize=True)
preview.save(depth_path, format="PNG", optimize=True)
copy = SCENE_COPY[key]
scenes.append(
{
"key": key,
"label": label,
"eyebrow": copy["eyebrow"],
"summary": copy["summary"],
"source": f"assets/scenes/{source_path.name}",
"depth": f"assets/scenes/{depth_path.name}",
"source_sha256": file_sha256(source_path),
"depth_sha256": file_sha256(depth_path),
"metrics": scene_metrics(raw_depth),
}
)
payload = {
"schema_version": "1.0",
"mode": "static_precomputed_demonstration",
"live_inference": False,
"synthetic_only": True,
"stored_client_data": "none",
"model": {
"id": MODEL_ID,
"revision": MODEL_REVISION,
"license_metadata": "Apache-2.0",
"boundary": "Repository metadata only; dependency review is required before client or paid use.",
},
"environment": {
"python": "3.11.0",
"transformers": "5.16.1",
"torch": "2.13.0+cpu",
"device": "CPU",
},
"scenes": scenes,
}
RESULTS_PATH.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8")
print(f"WROTE {RESULTS_PATH}")
for scene in scenes:
print(
scene["key"],
scene["metrics"],
scene["source_sha256"],
scene["depth_sha256"],
)
if __name__ == "__main__":
main()