Spaces:
Running on Zero
Running on Zero
Download demo/examples.py from ritianyu/SplatAnything: direct link, hf CLI and curl.
- Browser
- Download file 4.81 kB
-
https://huggingface.co/spaces/ritianyu/SplatAnything/resolve/main/demo/examples.py
- Command line
-
hf download hf://spaces/ritianyu/SplatAnything/demo/examples.py
-
curl -L -o examples.py https://huggingface.co/spaces/ritianyu/SplatAnything/resolve/main/demo/examples.py
4.81 kB
| """Discover portable scene examples and create small previews without changing inputs.""" | |
| import hashlib | |
| import os | |
| from pathlib import Path | |
| import tempfile | |
| from PIL import Image, ImageOps | |
| from src.utils.camera_io import list_image_files | |
| from demo.runtime import RGB_MODE, POSE_MODE | |
| CATEGORIES = ( | |
| ('object', 'Object', 'Objects up close'), | |
| ('indoor', 'Indoor', 'Spaces we live in'), | |
| ('street', 'Street', 'Life at street level'), | |
| ('aerial', 'Aerial', 'A different perspective'), | |
| ('rig', 'Rig-Recon', 'Every direction, together'), | |
| ('pano', 'Pan-Recon', 'A full circle of possibilities'), | |
| ) | |
| def prepare_hosted_examples(): | |
| """Cache original examples from a dataset without filling the Space code repo.""" | |
| repo = os.environ.get('SPLATANYTHING_EXAMPLES_REPO') | |
| if not repo: | |
| return None | |
| from huggingface_hub import snapshot_download | |
| cache = Path(os.environ.get('SPLATANYTHING_EXAMPLES_CACHE', | |
| str(Path(__file__).resolve().parents[1] / '.cache/hosted-examples'))) | |
| snapshot_download(repo_id=repo, repo_type='dataset', | |
| revision=os.environ.get('SPLATANYTHING_EXAMPLES_REVISION', 'main'), | |
| allow_patterns=['examples/**'], local_dir=str(cache)) | |
| root = cache / 'examples' | |
| if not load_examples(root): | |
| raise ValueError(f'No scene examples found under examples/ in dataset {repo}.') | |
| return root.resolve() | |
| def load_examples(root): | |
| root = Path(root).resolve() | |
| result = [] | |
| for folder, category, description in CATEGORIES: | |
| parent = root / folder | |
| if not parent.is_dir(): | |
| continue | |
| for scene in sorted(parent.iterdir()): | |
| if not scene.is_dir() or not (scene / 'images').is_dir(): | |
| continue | |
| images = list_image_files(scene / 'images') | |
| camera = scene / 'prior_camera.json' | |
| camera = camera if camera.is_file() else None | |
| for path in [scene, *images, *([camera] if camera else [])]: | |
| if root not in path.resolve().parents: | |
| raise ValueError(f'Invalid example asset outside the examples directory: {path.name}') | |
| result.append(dict(id=f'{folder}-{scene.name}', category=category, folder=folder, | |
| label=f'Scene {scene.name}', description=description, | |
| mode=POSE_MODE if camera else RGB_MODE, | |
| images=[str(path.resolve()) for path in images], | |
| camera=str(camera.resolve()) if camera else None, | |
| sky=folder in ('street', 'rig'), | |
| resolution=504 if folder in ('street', 'rig', 'aerial') else 756, | |
| max_images={'object': 24, 'indoor': 24, 'pano': 24, 'street': 100, 'aerial': 100, 'rig': 150}[folder])) | |
| return result | |
| def thumbnail(source, cache, *, size=(640, 400)): | |
| source, cache = Path(source), Path(cache) | |
| stat = source.stat() | |
| key = f'{source.resolve()}:{stat.st_size}:{stat.st_mtime_ns}:{size[0]}x{size[1]}' | |
| target = cache / (hashlib.sha256(key.encode()).hexdigest() + '.jpg') | |
| if target.is_file(): | |
| return str(target) | |
| cache.mkdir(parents=True, exist_ok=True) | |
| with Image.open(source) as image: | |
| preview = ImageOps.exif_transpose(image).convert('RGB') | |
| preview.thumbnail(size, Image.Resampling.LANCZOS) | |
| with tempfile.NamedTemporaryFile(suffix='.jpg', dir=cache, delete=False) as temporary: | |
| temporary_path = Path(temporary.name) | |
| try: | |
| preview.save(temporary_path, quality=84, optimize=True) | |
| temporary_path.replace(target) | |
| finally: | |
| temporary_path.unlink(missing_ok=True) | |
| return str(target) | |
| def gallery_style(element_id, covers, *, square=False): | |
| """Square scene covers; panorama covers retain their native proportions.""" | |
| ratios = [] | |
| for path, _ in covers: | |
| with Image.open(path) as image: | |
| ratios.append(1 if square else image.width / image.height) | |
| columns = ' '.join(f'{ratio:.8f}fr' for ratio in ratios) | |
| width = sum(ratios) * 360 + max(len(ratios) - 1, 0) * 14 + len(ratios) * 2 | |
| return (f'#{element_id} {{ --preview-columns: {columns}; ' | |
| f'--preview-width: {width:.2f}px; ' | |
| f'--preview-ratio: {"1 / 1" if square else "auto"}; ' | |
| f'--preview-fit: {"cover" if square else "initial"}; }}') | |
| def preview_images(paths, cache): | |
| """Show an evenly spaced selection; inference retains every original image.""" | |
| count = min(8, len(paths)) | |
| indices = [round(i * (len(paths) - 1) / max(count - 1, 1)) for i in range(count)] | |
| return [(thumbnail(paths[i], cache), Path(paths[i]).name) for i in indices] | |