File size: 2,158 Bytes
7c2d2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Load a single Bricker sample.

    python scripts/load_sample.py <sample_dir>

Example:
    python scripts/load_sample.py ../data/real/Test/scene_0000/000000_game

Works for both subsets (`real/` and `syn/`); only numpy is required.
"""

from __future__ import annotations

import argparse
import os

import numpy as np

FRAME_KEYS = ("I1", "I2", "I3", "I4", "I5")
EXPOSURE = {"I1": "1/16x", "I2": "1/4x", "I3": "1x (reference)", "I4": "4x", "I5": "16x"}
WHITE_LEVEL = 1023.0  # frames are stored as uint16 scaled to 2**10 - 1


def load_sample(sample_dir: str, normalize: bool = True) -> dict:
    """Read one sample directory.

    Returns a dict with:
        frames  -- {key: uint16 array of shape (Hp, Wp, 4)} for I1..I5
        gt      -- float32 array of shape (Hp, Wp, 4), 1.0 = white level
        meta    -- metadata dict (see README)
        raw     -- optional uint16 stack of the five frames, shape (5, Hp, Wp, 4)
    """
    frames = {k: np.load(os.path.join(sample_dir, f"raw_{k}.npy")) for k in FRAME_KEYS}
    gt = np.load(os.path.join(sample_dir, "raw_gt.npy"))
    meta = np.load(os.path.join(sample_dir, "metadata.npy"), allow_pickle=True).item()

    out = {"frames": frames, "gt": gt, "meta": meta}
    if normalize:
        out["raw"] = np.stack([frames[k].astype(np.float32) / WHITE_LEVEL for k in FRAME_KEYS])
    return out


def main() -> None:
    ap = argparse.ArgumentParser(description=__doc__)
    ap.add_argument("sample_dir")
    args = ap.parse_args()

    s = load_sample(args.sample_dir)
    print(f"sample: {args.sample_dir}")
    for k in FRAME_KEYS:
        a = s["frames"][k]
        print(f"  raw_{k}.npy  {str(a.dtype):7s} {a.shape}  exposure={EXPOSURE[k]:14s} "
              f"min={a.min():5d} max={a.max():5d} mean={a.mean():8.1f}")
    print(f"  raw_gt.npy  {str(s['gt'].dtype):7s} {s['gt'].shape}  "
          f"min={s['gt'].min():.3f} max={s['gt'].max():.3f}")
    print(f"  metadata keys: {sorted(s['meta'].keys())}")
    print(f"  CFA pattern: {s['meta']['CFAPattern']}  (packing: R, G1, G2, B)")
    print(f"  stacked frames: {s['raw'].shape} float32 in [0, 1]")


if __name__ == "__main__":
    main()