Download projects/spatial-0/scripts/geometry_conditioning.py from KineJIng/kinejing-open-source: direct link, hf CLI and curl.
- Browser
- Download file 3.65 kB
-
https://huggingface.co/datasets/KineJIng/kinejing-open-source/resolve/main/projects/spatial-0/scripts/geometry_conditioning.py
- Command line
-
hf download hf://datasets/KineJIng/kinejing-open-source/projects/spatial-0/scripts/geometry_conditioning.py
-
curl -L -o geometry_conditioning.py https://huggingface.co/datasets/KineJIng/kinejing-open-source/resolve/main/projects/spatial-0/scripts/geometry_conditioning.py
3.65 kB
| """Reuse the installed WorldStereo renderer with a multi-view reconstruction. | |
| Experimental conditioning only: depth is estimated, not verified ground truth. | |
| This module does not download models, publish media or change adapter source. | |
| """ | |
| import json | |
| import numpy as np | |
| import torch | |
| from PIL import Image | |
| def anchor_trajectory(c2ws): | |
| """Express an orbit relative to its first camera, which carries the image.""" | |
| if c2ws.ndim != 3 or c2ws.shape[1:] != (4, 4) or not len(c2ws): | |
| raise ValueError("Expected nonempty [N,4,4] trajectory") | |
| if not torch.isfinite(c2ws).all(): | |
| raise ValueError("Nonfinite trajectory") | |
| return torch.linalg.inv(c2ws[0]) @ c2ws | |
| def prepare_geometry_inputs(module, points, colors, output, *, image_pil, c2ws, | |
| intrs, moge_model, device, width, height, | |
| conditioning_frame_indices=None): | |
| from models.camera import get_camera_embedding | |
| if points.ndim != 2 or points.shape[1] != 3 or colors.shape != points.shape: | |
| raise ValueError("Expected matching point/color arrays of shape [P,3]") | |
| if not len(points) or not torch.isfinite(points).all() or not torch.isfinite(colors).all(): | |
| raise ValueError("Empty/nonfinite geometry") | |
| indices = conditioning_frame_indices | |
| if indices is None: | |
| indices = torch.arange(len(c2ws), device=c2ws.device) | |
| indices = indices.to(device=c2ws.device, dtype=torch.long) | |
| if not len(indices) or (indices < 0).any() or (indices >= len(c2ws)).any(): | |
| raise ValueError("Invalid conditioning frame indices") | |
| poses, cameras = c2ws[indices], intrs[indices] | |
| # Reuse the adapter's visibility-aware z-buffer, not a new rendering engine. | |
| rgb, mask = module._fallback_point_rendering( | |
| K=cameras, w2cs=torch.linalg.inv(poses), points=points, colors=colors, | |
| device=device, h=height, w=width) | |
| raw_coverage = (1 - mask).mean(dim=(1, 2, 3)).cpu().tolist() | |
| output.mkdir(parents=True, exist_ok=False) | |
| for i in range(len(rgb)): | |
| Image.fromarray((rgb[i].permute(1, 2, 0).cpu().clamp(0, 1).numpy()*255).astype(np.uint8)).save(output / f"render-{i:02d}.png") | |
| Image.fromarray((mask[i, 0].cpu().clamp(0, 1).numpy()*255).astype(np.uint8)).save(output / f"unknown-{i:02d}.png") | |
| # Only the actual anchor is fully observed. Do not mark other holes observed. | |
| anchor = torch.from_numpy(np.asarray(image_pil).copy()).permute(2, 0, 1).to(rgb.device).float()/255 | |
| if indices[0].item() == 0: | |
| rgb[0] = anchor | |
| mask[0] = 0 | |
| dtype = module._worldstereo_half_dtype() | |
| camera_embedding = get_camera_embedding( | |
| intrinsic=cameras.to(device), extrinsic=poses.to(device), f=len(indices), | |
| h=height, w=width, normalize=True, is_w2c=False).to(dtype=dtype) | |
| (output / "coverage.json").write_text(json.dumps({ | |
| "source": "estimated multi-view geometry; no generated points", | |
| "point_count": len(points), "frame_indices": indices.cpu().tolist(), | |
| "raw_projected_pixel_fraction": raw_coverage, | |
| "caveat": "Coverage does not establish correct geometry or successful completion" | |
| }, indent=2), encoding="utf-8") | |
| return { | |
| "image": image_pil, | |
| "render_video": (rgb*2-1).unsqueeze(0).permute(0, 2, 1, 3, 4).to(device=device, dtype=dtype), | |
| "render_mask": mask.unsqueeze(0).permute(0, 2, 1, 3, 4).to(device=device, dtype=dtype), | |
| "camera_embedding": camera_embedding, | |
| "extrinsics": torch.linalg.inv(c2ws).float().to(device), | |
| "intrinsics": intrs.to(device), "height": height, "width": width, | |
| "num_frames": len(c2ws), | |
| } | |