diff --git a/winnow/cache.py b/winnow/cache.py index d554401..8d1bbdb 100644 --- a/winnow/cache.py +++ b/winnow/cache.py @@ -7,43 +7,62 @@ recomputing on reruns. Uses numpy binary format for fast I/O. import hashlib import logging import os +from pathlib import Path import numpy as np logger = logging.getLogger(__name__) -# Model versions — bump these when the upstream model changes. -# LIMITATION — no automatic invalidation: if the user replaces the buffalo_l -# model files on disk (e.g. custom weights, InsightFace update) without -# changing INSIGHTFACE_HOME, the version string here stays "buffalo_l_v1" and -# stale embeddings from the old model are served from cache indefinitely. -# TODO: derive the version from a checksum of the model files, or expose a -# --clear-cache / CLEAR_EMBEDDING_CACHE flag so users can force invalidation. MODEL_VERSIONS = { - "insightface": "buffalo_l_v1", "immich": "immich_buffalo_l_v1", } +def _insightface_model_fingerprint() -> str: + """Derive a version string from buffalo_l .onnx file sizes and mtimes. + + Changes automatically when model files are replaced or updated, preventing + stale embeddings from a previous model being served from cache. + Falls back to a static string before the model is downloaded (first run). + """ + insightface_home = os.environ.get("INSIGHTFACE_HOME", os.path.expanduser("~/.insightface")) + model_dir = Path(insightface_home) / "models" / "buffalo_l" + if not model_dir.exists(): + return "buffalo_l_v1" + onnx_files = sorted(model_dir.glob("*.onnx")) + if not onnx_files: + return "buffalo_l_v1" + fingerprint = "|".join( + f"{f.name}:{f.stat().st_size}:{int(f.stat().st_mtime)}" + for f in onnx_files + ) + return hashlib.sha256(fingerprint.encode()).hexdigest()[:12] + + class EmbeddingCache: """Simple disk-based embedding cache. Embeddings are stored as .npy files in a flat directory, - keyed by a hash of (asset_id, model_version). + keyed by a hash of (asset_id, model_version). The InsightFace version + is derived from buffalo_l model file metadata so the cache auto-invalidates + when model files are replaced or updated. """ def __init__(self, cache_dir: str = ".if_cache") -> None: self.cache_dir = cache_dir self._ensured = False + self._model_versions = { + **MODEL_VERSIONS, + "insightface": _insightface_model_fingerprint(), + } def _ensure_dir(self) -> None: if not self._ensured: os.makedirs(self.cache_dir, exist_ok=True) self._ensured = True - @staticmethod - def _key(asset_id: str, model: str) -> str: - version = MODEL_VERSIONS.get(model, model) + def _key(self, asset_id: str, model: str) -> str: + version = self._model_versions.get(model, model) raw = f"{asset_id}:{version}" return hashlib.sha256(raw.encode()).hexdigest()[:16] @@ -96,4 +115,3 @@ def get_cache(cache_dir: str = ".if_cache") -> EmbeddingCache: if _cache is None: _cache = EmbeddingCache(cache_dir) return _cache - diff --git a/winnow/cli.py b/winnow/cli.py index a88ee4e..52f2f73 100644 --- a/winnow/cli.py +++ b/winnow/cli.py @@ -9,7 +9,7 @@ from rich.prompt import Confirm from .config import Config, ConfigManager from .executor import execute_jobs, upload_to_frigate -from .immich_api import get_people, merge_people +from .immich_api import get_immich_version, get_people, merge_people from .jobs import _show_preview, auto_configure, interactive_configure from .log_config import console, setup_logging from .upload_tracker import find_by_crop_dimension, get_person_summary, reset_person @@ -216,6 +216,13 @@ def main() -> None: f" {counts['rejected']} rejected{frigate_part}[/dim]" ) + _immich_version = get_immich_version() + if _immich_version is not None and _immich_version < (1, 106, 0): + rprint( + f" [yellow]⚠ Immich {'.'.join(str(x) for x in _immich_version)} detected — " + "winnow requires v1.106+. Some features may not work.[/yellow]" + ) + people = get_people() if not people: rprint("[bold red]Could not fetch people from Immich. Check URL/Key.[/bold red]") diff --git a/winnow/executor.py b/winnow/executor.py index bd56342..35248a8 100644 --- a/winnow/executor.py +++ b/winnow/executor.py @@ -17,6 +17,7 @@ from .frigate_api import ( delete_frigate_person_files, get_all_frigate_person_files, get_frigate_person_files, + get_frigate_version, recognize_face, ) from .image_processing import process_face_mode @@ -311,10 +312,17 @@ def upload_to_frigate(jobs: list[dict]) -> None: rprint("[yellow]⚠️ FRIGATE_URL not set, skipping upload.[/yellow]") return - # TODO: verify Frigate version at startup (GET /api/version) and warn if - # below v0.16. The face training API (/api/faces/{name}/register, - # /api/faces/{name}/delete, /api/faces/recognize) was introduced in v0.16; - # older versions return 404s that surface as opaque upload failures. + _frigate_version = get_frigate_version() + if _frigate_version is not None: + try: + parts = [int(x) for x in _frigate_version.split("-")[0].split(".") if x.isdigit()] + if len(parts) >= 2 and (parts[0], parts[1]) < (0, 16): + rprint( + f" [yellow]⚠ Frigate {_frigate_version} detected — " + "face training API requires v0.16+. Uploads may fail.[/yellow]" + ) + except Exception: + pass rprint("\n[bold cyan]📤 Uploading to Frigate[/bold cyan]") rprint(f" Target: [dim]{frigate_url}[/dim]") diff --git a/winnow/frigate_api.py b/winnow/frigate_api.py index 38ce3f5..0cfdd7d 100644 --- a/winnow/frigate_api.py +++ b/winnow/frigate_api.py @@ -8,6 +8,24 @@ import requests logger = logging.getLogger(__name__) +def get_frigate_version() -> str | None: + """Fetch Frigate's version string from GET /api/version. + + Returns the version string (e.g. "0.16.0-beta4") or None if FRIGATE_URL + is unset, the endpoint is unreachable, or the response is not parseable. + """ + frigate_url = os.environ.get("FRIGATE_URL", "").rstrip("/") + if not frigate_url: + return None + try: + resp = requests.get(f"{frigate_url}/api/version", timeout=5) + if resp.ok: + return resp.text.strip().strip('"') + return None + except Exception: + return None + + def _get_faces_data() -> dict | None: """Fetch raw GET /api/faces response. Returns None if unavailable.""" frigate_url = os.environ.get("FRIGATE_URL", "").rstrip("/") diff --git a/winnow/immich_api.py b/winnow/immich_api.py index d3332be..32dd05f 100644 --- a/winnow/immich_api.py +++ b/winnow/immich_api.py @@ -26,6 +26,25 @@ class FaceData: image_height: int +def get_immich_version() -> tuple[int, int, int] | None: + """Fetch Immich server version from GET /api/server/version. + + Returns (major, minor, patch) or None if unreachable or unparseable. + """ + try: + resp = requests.get( + f"{Config.IMMICH_URL}/api/server/version", + headers=get_headers(), + timeout=5, + ) + if resp.ok: + data = resp.json() + return (int(data["major"]), int(data["minor"]), int(data["patch"])) + return None + except Exception: + return None + + def get_people() -> list[dict]: """Fetch all people from Immich.""" try: