|
| 1 | +"""Download and cache Community Fish Detector weights (RF-DETR Nano).""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import hashlib |
| 6 | +from collections.abc import Callable |
| 7 | +from pathlib import Path |
| 8 | +from threading import Event |
| 9 | +from urllib.error import URLError |
| 10 | +from urllib.request import Request, urlopen |
| 11 | + |
| 12 | +from platformdirs import user_cache_dir |
| 13 | + |
| 14 | +# Pinned to upstream release cfd-2026.02.02-rf-detr-nano (digest from GitHub API). |
| 15 | +FISH_WEIGHTS_FILENAME = "community-fish-detector-2026.02.02-rf-detr-nano-640.pth" |
| 16 | +FISH_WEIGHTS_URL = ( |
| 17 | + "https://github.com/filippovarini/community-fish-detector/releases/download/" |
| 18 | + f"cfd-2026.02.02-rf-detr-nano/{FISH_WEIGHTS_FILENAME}" |
| 19 | +) |
| 20 | +FISH_WEIGHTS_SHA256_HEX = "076b0a80d5dbc3361fb2be707d521ce96fe990b453fb1c215d99194fc65b4762" |
| 21 | +FISH_WEIGHTS_SIZE_BYTES = 122_064_141 |
| 22 | + |
| 23 | +_READ_CHUNK = 1024 * 1024 # 1 MiB |
| 24 | + |
| 25 | + |
| 26 | +def fish_weights_cache_dir() -> Path: |
| 27 | + """Directory where the default fish `.pth` is stored after first download.""" |
| 28 | + return Path(user_cache_dir("deepmeerkat", appauthor=False)) / "fish" |
| 29 | + |
| 30 | + |
| 31 | +def fish_weights_cache_path() -> Path: |
| 32 | + """Full path to the cached default fish weights file.""" |
| 33 | + return fish_weights_cache_dir() / FISH_WEIGHTS_FILENAME |
| 34 | + |
| 35 | + |
| 36 | +def _sha256_file(path: Path) -> str: |
| 37 | + h = hashlib.sha256() |
| 38 | + with path.open("rb") as f: |
| 39 | + while True: |
| 40 | + chunk = f.read(_READ_CHUNK) |
| 41 | + if not chunk: |
| 42 | + break |
| 43 | + h.update(chunk) |
| 44 | + return h.hexdigest() |
| 45 | + |
| 46 | + |
| 47 | +def _download_to_path( |
| 48 | + url: str, |
| 49 | + dest: Path, |
| 50 | + *, |
| 51 | + expected_size: int, |
| 52 | + progress: Callable[[float, str], None] | None, |
| 53 | + cancel: Event | None, |
| 54 | +) -> None: |
| 55 | + dest.parent.mkdir(parents=True, exist_ok=True) |
| 56 | + tmp = dest.with_suffix(dest.suffix + ".download") |
| 57 | + req = Request( |
| 58 | + url, |
| 59 | + headers={"User-Agent": "DeepMeerkat/3.1 (+https://github.com/bw4sz/DeepMeerkat)"}, |
| 60 | + ) |
| 61 | + try: |
| 62 | + with urlopen(req, timeout=300) as resp: |
| 63 | + total = int(resp.headers.get("Content-Length") or 0) or expected_size |
| 64 | + got = 0 |
| 65 | + with tmp.open("wb") as out: |
| 66 | + while True: |
| 67 | + if cancel and cancel.is_set(): |
| 68 | + raise RuntimeError("Cancelled while downloading fish weights.") |
| 69 | + chunk = resp.read(_READ_CHUNK) |
| 70 | + if not chunk: |
| 71 | + break |
| 72 | + out.write(chunk) |
| 73 | + got += len(chunk) |
| 74 | + if progress and total > 0: |
| 75 | + frac = min(1.0, got / float(total)) |
| 76 | + mb_got = got // (1024 * 1024) |
| 77 | + mb_tot = max(1, total // (1024 * 1024)) |
| 78 | + progress( |
| 79 | + frac, |
| 80 | + f"Downloading fish model… ({mb_got} / ~{mb_tot} MiB)", |
| 81 | + ) |
| 82 | + tmp.replace(dest) |
| 83 | + except BaseException: |
| 84 | + tmp.unlink(missing_ok=True) |
| 85 | + raise |
| 86 | + |
| 87 | + |
| 88 | +def ensure_fish_weights( |
| 89 | + weights_path: str, |
| 90 | + *, |
| 91 | + progress: Callable[[float, str], None] | None = None, |
| 92 | + cancel: Event | None = None, |
| 93 | +) -> Path: |
| 94 | + """ |
| 95 | + Resolve `.pth` weights: use an explicit path if set and valid; otherwise use the |
| 96 | + cache path, downloading and verifying on first use. |
| 97 | + """ |
| 98 | + raw = (weights_path or "").strip() |
| 99 | + if raw: |
| 100 | + p = Path(raw).expanduser().resolve() |
| 101 | + if not p.is_file(): |
| 102 | + raise FileNotFoundError( |
| 103 | + f"Fish model weights not found: {p}\n" |
| 104 | + "Leave weights empty to download automatically, or set a valid .pth path." |
| 105 | + ) |
| 106 | + if progress: |
| 107 | + progress(1.0, "Using fish weights from disk…") |
| 108 | + return p |
| 109 | + |
| 110 | + cache = fish_weights_cache_path() |
| 111 | + if cache.is_file(): |
| 112 | + if progress: |
| 113 | + progress(0.0, "Verifying cached fish model…") |
| 114 | + digest = _sha256_file(cache) |
| 115 | + if digest == FISH_WEIGHTS_SHA256_HEX: |
| 116 | + if progress: |
| 117 | + progress(1.0, "Using cached fish model…") |
| 118 | + return cache |
| 119 | + cache.unlink(missing_ok=True) |
| 120 | + |
| 121 | + if progress: |
| 122 | + progress(0.0, "Downloading Community Fish Detector weights (~116 MB, one-time)…") |
| 123 | + |
| 124 | + try: |
| 125 | + _download_to_path( |
| 126 | + FISH_WEIGHTS_URL, |
| 127 | + cache, |
| 128 | + expected_size=FISH_WEIGHTS_SIZE_BYTES, |
| 129 | + progress=progress, |
| 130 | + cancel=cancel, |
| 131 | + ) |
| 132 | + except URLError as e: |
| 133 | + raise RuntimeError( |
| 134 | + "Could not download fish detector weights. Check your network, or download manually " |
| 135 | + "from:\n" |
| 136 | + f" {FISH_WEIGHTS_URL}\n" |
| 137 | + "Then set --fish-weights or the GUI Weights field to that file." |
| 138 | + ) from e |
| 139 | + |
| 140 | + if progress: |
| 141 | + progress(0.95, "Verifying download…") |
| 142 | + digest = _sha256_file(cache) |
| 143 | + if digest != FISH_WEIGHTS_SHA256_HEX: |
| 144 | + cache.unlink(missing_ok=True) |
| 145 | + raise RuntimeError( |
| 146 | + "Downloaded fish weights failed checksum verification. Delete the cache folder and " |
| 147 | + f"try again, or install manually. Cache: {cache.parent}" |
| 148 | + ) |
| 149 | + |
| 150 | + if progress: |
| 151 | + progress(1.0, "Fish model ready.") |
| 152 | + return cache |
0 commit comments