Spaces:
Running on Zero
Running on Zero
Download pointmap_viewer.py from karimknaebel/surge: direct link, hf CLI and curl.
- Browser
- Download file 3.67 kB
-
https://huggingface.co/spaces/karimknaebel/surge/resolve/main/pointmap_viewer.py
- Command line
-
hf download hf://spaces/karimknaebel/surge/pointmap_viewer.py
-
curl -L -o pointmap_viewer.py https://huggingface.co/spaces/karimknaebel/surge/resolve/main/pointmap_viewer.py
3.67 kB
| from pathlib import Path | |
| import gradio as gr | |
| import numpy as np | |
| from gradio.data_classes import FileData, GradioModel | |
| class PointmapData(GradioModel): | |
| depth: FileData | |
| image: FileData | |
| mask: FileData | |
| intrinsics: list[list[float]] | |
| settings: tuple[float, bool, bool, bool] | |
| class PointmapViewer(gr.HTML): | |
| data_model = PointmapData | |
| def __init__(self, render=True): | |
| assets = Path(__file__).parent / "viewer" | |
| super().__init__( | |
| label="Point map", | |
| html_template=(assets / "pointmap.html").read_text(), | |
| css_template=(assets / "pointmap.css").read_text(), | |
| js_on_load=(assets / "pointmap.js").read_text(), | |
| apply_default_css=False, | |
| container=True, | |
| padding=False, | |
| elem_classes=["pointmap-component"], | |
| render=render, | |
| # Gradio 6.14's HTML wrappers retain padding despite padding=False. | |
| head=( | |
| "<style>.pointmap-component.block, " | |
| ".pointmap-component > .html-container { padding: 0; }" | |
| ".pointmap-component.block > [data-testid='status-tracker'] { bottom: 0; }" | |
| ".pointmap-component.block > [data-testid='status-tracker'] .meta-text-center " | |
| "{ position: static; transform: none; }" | |
| + (assets / "settings.css").read_text() | |
| + "</style>" | |
| ), | |
| ) | |
| def postprocess(self, value: dict | PointmapData | None) -> PointmapData | None: | |
| return None if value is None else PointmapData.model_validate(value) | |
| def api_info(self) -> dict: | |
| return PointmapData.model_json_schema() | |
| def cleanup_threshold(strength: float) -> float: | |
| if strength == 0: | |
| return np.inf | |
| return ( | |
| 0.05 * 0.2 ** ((strength - 1) / 49) | |
| if strength <= 50 | |
| else 0.01 * 0.1 ** ((strength - 50) / 50) | |
| ) | |
| def cleanup_info(strength: float) -> str: | |
| if strength == 0: | |
| return "Off → Gentle → Strong · Depth-edge cleanup: Off" | |
| return f"Off → Gentle → Strong · Depth-edge threshold: {cleanup_threshold(strength):.4f}" | |
| def pointmap_settings(): | |
| with gr.Column(variant="panel"): | |
| gr.HTML( | |
| html_template='<h3>Cleanup ' | |
| '<span id="result-settings-status" role="status" hidden>Updating…</span></h3>', | |
| elem_id="result-settings-heading", | |
| js_on_load=None, | |
| ) | |
| cleanup = gr.Slider( | |
| 0, | |
| 100, | |
| value=50, | |
| step=1, | |
| precision=0, | |
| label="Cleanup strength", | |
| info=cleanup_info(50), | |
| buttons=["reset"], | |
| ) | |
| cleanup.change( | |
| lambda strength: gr.update(info=cleanup_info(strength)), | |
| inputs=cleanup, | |
| outputs=cleanup, | |
| queue=False, | |
| show_progress="hidden", | |
| ) | |
| with gr.Row(elem_id="result-filters"): | |
| filter_sky = gr.Checkbox(value=False, label="Filter sky") | |
| filter_black = gr.Checkbox(value=False, label="Filter black background") | |
| filter_white = gr.Checkbox(value=False, label="Filter white background") | |
| return cleanup, filter_sky, filter_black, filter_white | |
| def pointmap_data( | |
| depth: Path, image: Path, intrinsics: np.ndarray, mask: Path, | |
| settings: tuple[float, bool, bool, bool], | |
| ) -> dict: | |
| return { | |
| "depth": FileData(path=str(depth)).model_dump(), | |
| "image": FileData(path=str(image)).model_dump(), | |
| "mask": FileData(path=str(mask)).model_dump(), | |
| "intrinsics": intrinsics.tolist(), | |
| "settings": settings, | |
| } | |