File size: 2,566 Bytes
08fff05
 
3de34d4
 
 
 
 
 
25abb1d
3de34d4
 
 
 
 
 
 
25abb1d
3de34d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Preview helpers consumed by ``app.py`` to turn raw pager outputs into
uint8 images for the Gradio UI.
"""

from __future__ import annotations

from typing import Tuple

import numpy as np
from scipy.ndimage import median_filter


def prepare_image_for_logging(image: np.ndarray, apply_median: bool = False) -> np.ndarray:
    """Per-sample min/max stretch to ``uint8``, optionally median-filtered."""
    if apply_median:
        image = median_filter(image.astype(np.float32), size=3)
    image = (image - image.min()) / (image.max() - image.min() + 1e-8)
    return (image * 255).astype("uint8")


def prepare_depth_for_logging(
    pager,
    depth,
    sky_mask,
    orig_size: Tuple[int, int],
    cmap,
    log_scale=None,
    sky_mask_threshold: float = 0.3,
    sky_mask_softness: float = 0.15,
    sky_mask_open_kernel: int = 0,
):
    """Return ``(raw_depth, viz_depth)``.

    ``raw_depth`` is a float32 metric-depth ERP array suitable for saving to
    ``.npz`` and feeding the evaluation pipeline. ``viz_depth`` is a uint8
    Spectral-coloured ERP image for previews.
    """
    raw_image, log_image = pager.process_depth_output(
        depth,
        orig_size=orig_size,
        sky_mask=sky_mask,
        log_scale=log_scale,
        sky_mask_threshold=sky_mask_threshold,
        sky_mask_softness=sky_mask_softness,
        sky_mask_open_kernel=sky_mask_open_kernel,
    )
    raw_image = raw_image.detach().cpu().numpy()
    log_image = log_image.detach().cpu().numpy()
    viz = prepare_image_for_logging(log_image, apply_median=True)
    viz = cmap(viz / 255.0)
    viz = (viz[..., :3] * 255)[0].astype(np.uint8)
    return raw_image, np.transpose(viz, (2, 0, 1))


def prepare_normals_for_logging(
    pager,
    normals,
    sky_mask,
    orig_size: Tuple[int, int],
    cmap=None,
    sky_mask_threshold: float = 0.3,
    sky_mask_softness: float = 0.15,
    sky_mask_open_kernel: int = 0,
):
    """Return ``(raw_normals, viz_normals)``.

    ``raw_normals`` is a float32 unit-normal ERP tensor in ``[-1, 1]``,
    shape ``(3, H, W)`` — what the evaluation script consumes directly.
    ``viz_normals`` is a uint8 RGB image (per-sample min/max rescaled).
    """
    raw_normals = pager.process_normals_output(
        normals,
        orig_size=orig_size,
        sky_mask=sky_mask,
        sky_mask_threshold=sky_mask_threshold,
        sky_mask_softness=sky_mask_softness,
        sky_mask_open_kernel=sky_mask_open_kernel,
    ).detach().cpu().numpy()
    viz = prepare_image_for_logging(raw_normals)
    return raw_normals, viz