from __future__ import annotations from pathlib import Path from typing import Any import numpy as np import pandas as pd from fall.data.features import add_keypoint_noise, apply_frame_drop, pose_to_feature_vector, resize_or_pad_sequence from fall.data.video import read_sampled_frames class PoseDataset: def __init__( self, manifest: str | Path, split: str = "train", frames: int = 32, use_confidence: bool = True, use_velocity: bool = True, min_confidence: float = 0.05, frame_drop: float = 0.0, keypoint_noise: float = 0.0, seed: int = 42, ) -> None: import torch from torch.utils.data import Dataset self._base = Dataset self.torch = torch df = pd.read_csv(manifest) if split != "all" and "split" in df.columns: df = df[df["split"] == split] if df.empty: raise RuntimeError(f"No rows for split '{split}' in {manifest}") self.df = df.reset_index(drop=True) self.frames = frames self.use_confidence = use_confidence self.use_velocity = use_velocity self.min_confidence = min_confidence self.frame_drop = frame_drop self.keypoint_noise = keypoint_noise self.rng = np.random.default_rng(seed) def __len__(self) -> int: return len(self.df) def __getitem__(self, idx: int) -> dict[str, Any]: row = self.df.iloc[idx] pose_path = row.get("pose_path") if not isinstance(pose_path, str): raise KeyError("PoseDataset requires a 'pose_path' column.") pose = np.load(pose_path).astype(np.float32) pose = resize_or_pad_sequence(pose, self.frames) pose = apply_frame_drop(pose, self.frame_drop, self.rng) pose = add_keypoint_noise(pose, self.keypoint_noise, self.rng) x = pose_to_feature_vector( pose, use_confidence=self.use_confidence, use_velocity=self.use_velocity, min_conf=self.min_confidence, ) return { "x": self.torch.from_numpy(x), "y": self.torch.tensor(float(row["label"]), dtype=self.torch.float32), "video_id": str(row.get("video_id", idx)), } @property def input_dim(self) -> int: item = self[0]["x"] return int(item.shape[-1]) class RGBVideoDataset: def __init__( self, manifest: str | Path, split: str = "train", frames: int = 32, image_size: int = 112, ) -> None: import torch self.torch = torch df = pd.read_csv(manifest) if split != "all" and "split" in df.columns: df = df[df["split"] == split] if df.empty: raise RuntimeError(f"No rows for split '{split}' in {manifest}") self.df = df.reset_index(drop=True) self.frames = frames self.image_size = image_size def __len__(self) -> int: return len(self.df) def __getitem__(self, idx: int) -> dict[str, Any]: row = self.df.iloc[idx] rgb_path = row.get("rgb_path", None) if isinstance(rgb_path, str) and rgb_path: frames = np.load(rgb_path).astype(np.float32) / 255.0 frames = frames[: self.frames] while len(frames) < self.frames: frames = np.concatenate([frames, frames[-1:]], axis=0) else: start = row.get("start", None) end = row.get("end", None) fps = row.get("fps", None) start = None if pd.isna(start) else float(start) end = None if pd.isna(end) else float(end) fps = None if pd.isna(fps) else float(fps) frames = read_sampled_frames(row["video_path"], self.frames, resize=self.image_size, start=start, end=end, fps=fps).astype(np.float32) / 255.0 frames = np.transpose(frames, (0, 3, 1, 2)) return { "x": self.torch.from_numpy(frames), "y": self.torch.tensor(float(row["label"]), dtype=self.torch.float32), "video_id": str(row.get("video_id", idx)), }