File size: 3,667 Bytes
44413b8
 
 
 
 
 
 
 
50ac566
44413b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
961245c
 
 
44413b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6a6a85
 
 
44413b8
c6a6a85
44413b8
 
 
 
 
 
 
 
 
 
 
d954b71
 
 
 
 
 
 
5a33797
44413b8
 
 
 
 
 
 
50ac566
44413b8
 
 
50ac566
44413b8
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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,
    }