Spaces:
Running on Zero
Running on Zero
Download depth/export.py from eira06/depthwizard-backend: direct link, hf CLI and curl.
- Browser
- Download file 1.03 kB
-
https://huggingface.co/spaces/eira06/depthwizard-backend/resolve/main/depth/export.py
- Command line
-
hf download hf://spaces/eira06/depthwizard-backend/depth/export.py
-
curl -L -o export.py https://huggingface.co/spaces/eira06/depthwizard-backend/resolve/main/depth/export.py
1.03 kB
| """ | |
| M1 Perception Module - M5 Viewer JSON Bridge | |
| """ | |
| import json | |
| import logging | |
| import numpy as np | |
| from PIL import Image | |
| def export_to_m5_json(raw_depth: np.ndarray, output_path: str, grid_size: int = 128) -> bool: | |
| """ | |
| Compresses and normalizes the depth array to a 1D JSON list for the M5 Three.js viewer. | |
| """ | |
| try: | |
| # Normalize to 0.0 - 1.0 | |
| d_min, d_max = np.min(raw_depth), np.max(raw_depth) | |
| normalized = (raw_depth - d_min) / (d_max - d_min + 1e-8) | |
| # Resize to match Three.js grid | |
| img = Image.fromarray(normalized) | |
| img_resized = img.resize((grid_size, grid_size), Image.Resampling.BILINEAR) | |
| resized_array = np.array(img_resized, dtype=np.float32) | |
| # Flatten and export | |
| flat_list = resized_array.flatten().tolist() | |
| with open(output_path, 'w') as f: | |
| json.dump(flat_list, f) | |
| return True | |
| except Exception as e: | |
| logging.error(f"[M1 EXPORT] Failed to export M5 JSON: {str(e)}") | |
| return False | |