DariusGiannoli commited on
Commit ·
e5b09ea
1
Parent(s): 4d6dc90
perf: add @st .cache_data to all image/data loaders in middlebury_loader
Browse files
utils/middlebury_loader.py
CHANGED
|
@@ -12,6 +12,7 @@ from pathlib import Path
|
|
| 12 |
|
| 13 |
import cv2
|
| 14 |
import numpy as np
|
|
|
|
| 15 |
|
| 16 |
DEFAULT_MIDDLEBURY_ROOT = os.path.join(
|
| 17 |
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
|
|
@@ -30,6 +31,7 @@ BUNDLED_SCENES = {
|
|
| 30 |
# Scanning
|
| 31 |
# ------------------------------------------------------------------
|
| 32 |
|
|
|
|
| 33 |
def scan_dataset_root(root_path: str = DEFAULT_MIDDLEBURY_ROOT) -> list:
|
| 34 |
"""Return sorted list of scene names that contain im0.png, im1.png, calib.txt."""
|
| 35 |
if not os.path.isdir(root_path):
|
|
@@ -45,6 +47,7 @@ def scan_dataset_root(root_path: str = DEFAULT_MIDDLEBURY_ROOT) -> list:
|
|
| 45 |
return scenes
|
| 46 |
|
| 47 |
|
|
|
|
| 48 |
def get_scene_groups(root_path: str = DEFAULT_MIDDLEBURY_ROOT) -> dict:
|
| 49 |
"""Group scenes by base name (strip trailing digits)."""
|
| 50 |
scenes = scan_dataset_root(root_path)
|
|
@@ -64,6 +67,7 @@ def get_available_views(scene_path: str) -> list:
|
|
| 64 |
# Loading
|
| 65 |
# ------------------------------------------------------------------
|
| 66 |
|
|
|
|
| 67 |
def load_stereo_pair(scene_path: str, view_suffix: str = "") -> dict:
|
| 68 |
"""Load left + right images, calibration and optional GT disparity."""
|
| 69 |
left = cv2.imread(os.path.join(scene_path, f"im0{view_suffix}.png"),
|
|
@@ -83,6 +87,7 @@ def load_stereo_pair(scene_path: str, view_suffix: str = "") -> dict:
|
|
| 83 |
}
|
| 84 |
|
| 85 |
|
|
|
|
| 86 |
def load_single_view(scene_path: str, view_suffix: str = "") -> np.ndarray:
|
| 87 |
"""Load and return im0{suffix}.png from a scene folder."""
|
| 88 |
return cv2.imread(os.path.join(scene_path, f"im0{view_suffix}.png"),
|
|
@@ -93,6 +98,7 @@ def load_single_view(scene_path: str, view_suffix: str = "") -> np.ndarray:
|
|
| 93 |
# Calibration parser
|
| 94 |
# ------------------------------------------------------------------
|
| 95 |
|
|
|
|
| 96 |
def parse_calib(calib_path: str) -> dict:
|
| 97 |
"""
|
| 98 |
Parse Middlebury ``calib.txt``.
|
|
@@ -128,6 +134,7 @@ def parse_calib(calib_path: str) -> dict:
|
|
| 128 |
# PFM loader
|
| 129 |
# ------------------------------------------------------------------
|
| 130 |
|
|
|
|
| 131 |
def load_pfm(filepath: str) -> np.ndarray:
|
| 132 |
"""Read a PFM (Portable FloatMap) and return a float32 ndarray."""
|
| 133 |
with open(filepath, "rb") as f:
|
|
@@ -147,6 +154,7 @@ def load_pfm(filepath: str) -> np.ndarray:
|
|
| 147 |
return np.flipud(data.copy())
|
| 148 |
|
| 149 |
|
|
|
|
| 150 |
def read_pfm_bytes(file_bytes: bytes) -> np.ndarray:
|
| 151 |
"""Parse PFM from raw bytes (uploaded file)."""
|
| 152 |
buf = io.BytesIO(file_bytes)
|
|
|
|
| 12 |
|
| 13 |
import cv2
|
| 14 |
import numpy as np
|
| 15 |
+
import streamlit as st
|
| 16 |
|
| 17 |
DEFAULT_MIDDLEBURY_ROOT = os.path.join(
|
| 18 |
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
|
|
|
|
| 31 |
# Scanning
|
| 32 |
# ------------------------------------------------------------------
|
| 33 |
|
| 34 |
+
@st.cache_data
|
| 35 |
def scan_dataset_root(root_path: str = DEFAULT_MIDDLEBURY_ROOT) -> list:
|
| 36 |
"""Return sorted list of scene names that contain im0.png, im1.png, calib.txt."""
|
| 37 |
if not os.path.isdir(root_path):
|
|
|
|
| 47 |
return scenes
|
| 48 |
|
| 49 |
|
| 50 |
+
@st.cache_data
|
| 51 |
def get_scene_groups(root_path: str = DEFAULT_MIDDLEBURY_ROOT) -> dict:
|
| 52 |
"""Group scenes by base name (strip trailing digits)."""
|
| 53 |
scenes = scan_dataset_root(root_path)
|
|
|
|
| 67 |
# Loading
|
| 68 |
# ------------------------------------------------------------------
|
| 69 |
|
| 70 |
+
@st.cache_data
|
| 71 |
def load_stereo_pair(scene_path: str, view_suffix: str = "") -> dict:
|
| 72 |
"""Load left + right images, calibration and optional GT disparity."""
|
| 73 |
left = cv2.imread(os.path.join(scene_path, f"im0{view_suffix}.png"),
|
|
|
|
| 87 |
}
|
| 88 |
|
| 89 |
|
| 90 |
+
@st.cache_data
|
| 91 |
def load_single_view(scene_path: str, view_suffix: str = "") -> np.ndarray:
|
| 92 |
"""Load and return im0{suffix}.png from a scene folder."""
|
| 93 |
return cv2.imread(os.path.join(scene_path, f"im0{view_suffix}.png"),
|
|
|
|
| 98 |
# Calibration parser
|
| 99 |
# ------------------------------------------------------------------
|
| 100 |
|
| 101 |
+
@st.cache_data
|
| 102 |
def parse_calib(calib_path: str) -> dict:
|
| 103 |
"""
|
| 104 |
Parse Middlebury ``calib.txt``.
|
|
|
|
| 134 |
# PFM loader
|
| 135 |
# ------------------------------------------------------------------
|
| 136 |
|
| 137 |
+
@st.cache_data
|
| 138 |
def load_pfm(filepath: str) -> np.ndarray:
|
| 139 |
"""Read a PFM (Portable FloatMap) and return a float32 ndarray."""
|
| 140 |
with open(filepath, "rb") as f:
|
|
|
|
| 154 |
return np.flipud(data.copy())
|
| 155 |
|
| 156 |
|
| 157 |
+
@st.cache_data
|
| 158 |
def read_pfm_bytes(file_bytes: bytes) -> np.ndarray:
|
| 159 |
"""Parse PFM from raw bytes (uploaded file)."""
|
| 160 |
buf = io.BytesIO(file_bytes)
|