from __future__ import annotations from dataclasses import dataclass, field from pathlib import Path from typing import Iterable import torch from PIL import Image from torchvision import transforms from .base import FingerprintSample IMAGE_EXTS = {".png", ".jpg", ".jpeg", ".bmp", ".tif", ".tiff"} @dataclass class FVCPaths: """Root directories for FVC years to include. Each entry is the 'Dbs' folder (or parent of Dbs). Example: root_fvc2002 = "/path/to/FVC_Dataset/FVC2002" root_fvc2004 = "/path/to/FVC_Dataset/FVC2004" Leave empty string to skip a year. """ root_fvc2002: str = "" root_fvc2004: str = "" root_fvc2000: str = "" extra_roots: list[str] = field(default_factory=list) class FVCLoader: """Loader for FVC 2000/2002/2004 datasets. Directory layout (standard FVC): /Dbs//_.tif Metadata: identity_id — "__" e.g. "2002_db1_042" 8 impressions per subject → real quality variation for L_mat finger_id — "f01" for all FVC (one finger per subject in competition protocol) sensor_id — "fvc_db" e.g. "fvc2002_db1" dataset — "fvc" Subsets _a and _b belong to the same sensor_id — they are train/test splits of the same capture device, not different devices. """ def __init__(self, image_size: int = 224): self.transform = transforms.Compose( [ transforms.Resize((image_size, image_size)), transforms.ToTensor(), ] ) def discover(self, paths: FVCPaths) -> list[dict[str, str]]: records: list[dict[str, str]] = [] year_roots = [ (paths.root_fvc2000, "2000"), (paths.root_fvc2002, "2002"), (paths.root_fvc2004, "2004"), ] for extra in paths.extra_roots: year_roots.append((extra, "extra")) for root_str, year in year_roots: if not root_str: continue root = Path(root_str) dbs_dir = root / "Dbs" if (root / "Dbs").exists() else root records.extend(self._scan_dbs(dbs_dir, year)) return records def iter_samples( self, records: Iterable[dict[str, str]] ) -> Iterable[FingerprintSample]: for rec in records: image = Image.open(rec["image_path"]).convert("L") tensor = self.transform(image) yield { "image": tensor, "identity_id": rec["identity_id"], "finger_id": rec["finger_id"], "sensor_id": rec["sensor_id"], "dataset": rec["dataset"], "image_path": rec["image_path"], } def _scan_dbs(self, dbs_root: Path, year: str) -> list[dict[str, str]]: if not dbs_root.exists(): return [] records: list[dict[str, str]] = [] for db_dir in sorted(dbs_root.iterdir()): if not db_dir.is_dir(): continue db_name = db_dir.name.lower() # e.g. "db1_a", "DB1_A" # Normalise: "db1_a" or "DB1_A" → db_id = "db1" db_id = self._parse_db_id(db_name) if db_id is None: continue sensor_id = f"fvc{year}_{db_id}" dataset = f"fvc{year}" for img_path in sorted(db_dir.iterdir()): if not img_path.is_file() or img_path.suffix.lower() not in IMAGE_EXTS: continue meta = self._parse_filename(img_path, sensor_id, dataset, year, db_id) if meta is not None: records.append(meta) return records @staticmethod def _parse_db_id(db_dir_name: str) -> str | None: """'Db1_a', 'DB2_B', 'db3_a' → 'db1', 'db2', 'db3'.""" name = db_dir_name.lower().strip() # Strip trailing subset suffix (_a, _b, _A, _B) for suffix in ("_a", "_b"): if name.endswith(suffix): name = name[: -len(suffix)] break # Accept names like 'db1', 'db2', 'db3', 'db4' if name.startswith("db") and name[2:].isdigit(): return name return None @staticmethod def _parse_filename( img_path: Path, sensor_id: str, dataset: str, year: str, db_id: str, ) -> dict[str, str] | None: """Parse '_.tif' → metadata record.""" stem = img_path.stem # e.g. "042_3" or "100_8" parts = stem.split("_") if len(parts) != 2: return None subject_str, impression_str = parts if not subject_str.isdigit() or not impression_str.isdigit(): return None subject_id = int(subject_str) impression_id = int(impression_str) # Unique identity key: year + db + subject_id identity_id = f"{year}_{db_id}_{subject_id:04d}" return { "identity_id": identity_id, "finger_id": "f01", # FVC: one finger per subject "impression_id": str(impression_id), "sensor_id": sensor_id, "dataset": dataset, "image_path": str(img_path), }