import cv2, numpy as np, pathlib # -------- basic image helpers --------------------------------------------- def load_rgb(path: pathlib.Path) -> np.ndarray: img = cv2.imread(str(path), cv2.IMREAD_COLOR) # BGR if img is None: raise FileNotFoundError(path) return img[:, :, ::-1].copy() # → RGB def safe_resize(img: np.ndarray, long_side: int = 960) -> np.ndarray: h, w = img.shape[:2] if max(h, w) <= long_side: return img scale = long_side / max(h, w) return cv2.resize(img, (int(w * scale), int(h * scale)), cv2.INTER_AREA) def pad_to_multiple_of_14(img: np.ndarray) -> np.ndarray: h, w = img.shape[:2] pad_h, pad_w = (-h) % 14, (-w) % 14 return cv2.copyMakeBorder(img, 0, pad_h, 0, pad_w, cv2.BORDER_REFLECT101) def load_rgb_bytes(b: bytes) -> np.ndarray: import numpy as np, cv2 arr = np.frombuffer(b, np.uint8) img = cv2.imdecode(arr, cv2.IMREAD_COLOR) if img is None: raise ValueError("Cannot decode image bytes") return img[:, :, ::-1]