Compare commits

..
115 Commits
Author SHA1 Message Date
flan ea99ad10e3 Merge branch 'dev' 2026-06-14 18:05:00 +00:00
flan a237983777 chore: update uv.lock for v0.4.11 2026-06-14 18:04:21 +00:00
flan a2d0541493 chore: bump to v0.4.11 and update changelog
Documents object mode removal cleanup, dead embedding path removal,
FutureWarning fix, variant pyproject sync, and docs/wiki updates.
2026-06-14 18:04:05 +00:00
flan 1574aed7e4 docs: expand MERGE_DUPLICATE_PEOPLE docs and fix stale output dir description 2026-06-14 17:52:22 +00:00
flan 0192b6cb5b chore: sync variant pyproject files with main after object pipeline removal
Remove torch/torchvision/transformers/ultralytics from all three variant
pyproject files (rocm/cpu/intel) — no longer needed since SigLIP and YOLO
were removed in v0.4.10. Update version to 0.4.10 and description.

NOTE: uv-rocm.lock, uv-cpu.lock, uv-intel.lock are now stale and must be
regenerated in their respective platform environments before the next Docker
build of those variants.

Also fix fetch_face_data() docstring to not mention embedding (removed field).
2026-06-14 17:51:26 +00:00
flan 0ef15c5c12 chore: remove dead embedding paths and suppress insightface FutureWarning in image_processing
- immich_api.py: remove FaceData.embedding field and numpy import — Immich face
  embeddings were never consumed after being fetched; bbox/confidence is all that's used
- embeddings.py: remove immich_embedding param from get_embedding() — never passed by
  any caller; simplify docstring accordingly
- image_processing.py: wrap insightface_app.get() in warnings filter to suppress the
  scikit-image FutureWarning about estimate being deprecated (already suppressed in
  embeddings.py for the diversity path, was leaking from the crop-alignment path)
- README.md: remove two stale "object mode" / "object classification" fragments
2026-06-14 17:47:30 +00:00
flan 72dbbfa18a chore: remove object classification from package docstring 2026-06-14 17:43:17 +00:00
flan 274d50ee99 chore: remove remaining dead-mode references from executor, jobs, compose, Dockerfile
After removing the object pipeline, several dead 'mode' artifacts remained:

- executor.py: unpack `config` from job even though it was no longer read
- jobs.py: set `"mode": "face"` in both configure paths (key never consumed)
- compose.yml: TRAINING_MODE=face env, OBJECT_CLASS comment, HF_HOME, stale MAX_AUTO_IMAGES default note
- Dockerfile: "and object classification" label, /models/huggingface mkdir, HF_HOME ENV
2026-06-14 17:42:27 +00:00
flan 2d7b52470e chore: remove dead object/SigLIP references after pipeline removal
- cache.py: drop siglip MODEL_VERSIONS entry
- log_config.py: drop ultralytics/transformers/torch from noise silencers
- scheduler.py: drop HF_HOME and HuggingFace model check
- scripts/benchmark.py: drop bench_siglip and make_random_image
2026-06-14 17:31:54 +00:00
flan 835016e0e3 feat: remove object mode pipeline (YOLO, SigLIP, TRAINING_MODE, OBJECT_CLASS)
winnow is a face recognition training tool. Object mode required manual
file placement with no Frigate API, pulled in torch/torchvision/transformers/
ultralytics (~2 GB), and was architecturally misaligned with the project goal.

Removed:
- process_object_mode (YOLO inference), get_yolo_model
- SigLIP model stack (get_siglip_model, get_object_embedding, batch variant)
- entity_type branching throughout diversity, embeddings, jobs, executor
- TRAINING_MODE and OBJECT_CLASS env vars
- torch, torchvision, transformers, ultralytics dependencies
- pytorch index entries from pyproject.toml
- Object mode from README (Modes section, env var table, How It Works)
2026-06-14 17:29:25 +00:00
github-actions[bot] 3105b15beb chore: update lockfiles 2026-06-14 17:12:05 +00:00
flan d12abbc543 docs: clarify winnow's role as supplement to manual Frigate training 2026-06-14 17:11:59 +00:00
flan efce4e3443 Merge branch 'dev' of github.com:sudolulo/winnow into dev 2026-06-14 17:11:37 +00:00
flan cb670dd555 docs: clarify winnow fills the gap where manual Frigate training images are absent 2026-06-14 17:11:34 +00:00
github-actions[bot] f027f0a7d1 chore: update lockfiles 2026-06-14 17:09:33 +00:00
flan 4e989b042e Merge branch 'main' of github.com:sudolulo/winnow 2026-06-14 17:08:43 +00:00
flan d63bcfc10b chore: bump version to 0.4.10 2026-06-14 17:08:33 +00:00
flan 650629d3ed release: v0.4.10 2026-06-14 17:08:33 +00:00
flan d7dfc1446a config: lower MAX_AUTO_IMAGES default from 80 to 20 2026-06-14 17:07:52 +00:00
github-actions[bot] 20904b6b22 chore: update lockfiles 2026-06-14 17:06:47 +00:00
github-actions[bot] e47fdaadf6 chore: update lockfiles 2026-06-14 17:06:19 +00:00
flan dfaa03de47 release: v0.4.9 2026-06-14 17:05:46 +00:00
flan 9d5741f626 feat: warn on launch when unsupported advanced tuning vars are set; move ENABLE_FRIGATE_SCORES to unsupported section 2026-06-14 16:59:46 +00:00
flan d70a246a1c docs: move FRIGATE_SCORE_CEILING to unsupported advanced tuning section 2026-06-14 16:56:24 +00:00
flan 51cc7032eb docs: README accuracy audit — novelty gate, rejection scope, calibrated defaults
- How It Works step 9: "upload freely" → note novelty gate may skip below-cap candidates
- Persistence note: "Frigate rejections" → "rejected assets" (covers confidence skips too)
- RETRY_REJECTED description: explicitly covers all rejection types, not just Frigate
- Image Quality section: split into user-adjustable controls and calibrated image
  processing defaults with a support disclaimer to deter blind tuning
2026-06-14 16:54:56 +00:00
flan 105099c819 fix: mark assets rejected when faces API confidence is below threshold
Previously, assets that passed embedding-phase selection but failed the
faces API confidence check in execute_jobs were silently skipped with no
tracker entry. They appeared as valid candidates on every future run,
were re-selected, and re-skipped in an endless cycle. Now they are
marked rejected so they are excluded from future runs. RETRY_REJECTED=true
clears them if Immich later re-processes the image.
2026-06-14 16:46:09 +00:00
flan 0afc9386c6 feat: dynamic Frigate score ceiling; consolidate quality replacement branches
FRIGATE_SCORE_CEILING now defaults to dynamic mode (unset): below-cap
candidates are skipped if their pre-upload Frigate score exceeds the
most-redundant tracked file's score. This catches conditions already
covered by manually-added Frigate images that winnow cannot track —
the embedding-based diversity selection has no visibility into those.
Set FRIGATE_SCORE_CEILING=0 to disable; a positive value (e.g. 0.85)
still acts as a fixed hard ceiling. First-run safety is unchanged
(pre_run_count==0 prevents recognize_face from being called).

The two quality replacement branches (Frigate-score and blur-score)
shared identical structure and are merged into a single code path
parameterised by score source and comparison direction.

Also raises MIN_FACE_COUNT default from 0 to 3 and updates the
config test to match.
2026-06-14 16:38:25 +00:00
flan a59d05e7fd rename STRATEGY=auto to STRATEGY=adaptive; keep auto as alias
AUTO_MODE (batch/unattended) and STRATEGY=auto (diversity algorithm) shared
the same word for unrelated concepts. Renaming the strategy value to
'adaptive' eliminates the ambiguity. The old value is kept as a silent alias
so existing configs continue to work.

Interactive menu updated from "Auto (Objective Diversity)" to "Adaptive
Diversity". README AUTO_MODE description clarified to emphasise unattended
batch processing, not selection strategy.
2026-06-14 16:15:34 +00:00
flan d0cb2e17b1 config: raise MIN_FACE_COUNT default from 0 to 3
People with fewer than 3 tagged photos produce degenerate training sets
and rarely benefit from processing. Skip them by default.
2026-06-14 16:12:23 +00:00
flan 2dc26b5a3d docs: fix CUDA version, add MERGE_DUPLICATE_PEOPLE and TRACE_CROP_SIZE to README
- `:latest` image tag listed CUDA 13.3 — actual base is 12.8.1
- MERGE_DUPLICATE_PEOPLE existed in config but was absent from env var table
- TRACE_CROP_SIZE existed in CLI but was absent from env var table
- RESET_PERSON description now mentions `*` wildcard for bulk reset of all people
2026-06-14 16:11:21 +00:00
flan fe4cfac7b5 docs: add missing dedup step, fix auto-stop description in How It Works
- Step 5 (near-duplicate removal) was added in v0.4.4 but never
  documented; added between embedding and diversity selection
- Auto-stop was described as 'stops when similarity exceeds threshold'
  which is backwards; it stops when the next candidate's distance to
  already-selected images falls below the threshold (too similar, not
  enough new information)
- Renumbered steps 5-8 to 6-9
- Tightened hard-example weighting description to match the code
2026-06-14 16:06:45 +00:00
flan ec074fd279 fix: remove unused record_frigate_file import (ruff F401) 2026-06-14 04:10:15 +00:00
github-actions[bot] 6018bf7222 chore: update lockfiles 2026-06-14 04:09:08 +00:00
flan 4eb6e3169b perf: write-through in-memory cache for tracker JSON reads
All _load() calls after the first return the cached dict instead of
re-reading disk. _save() updates both disk and cache atomically.
Drops per-person tracker reads from ~90 to ~1 in the upload loop.
Keyed by resolved file path so test isolation (unique tmp_path dirs)
is preserved with no fixture changes needed.
2026-06-14 04:08:29 +00:00
github-actions[bot] f5ec9a0001 chore: update lockfiles 2026-06-14 04:06:38 +00:00
flan 7dce4a5c71 perf: eliminate O(K²) dedup allocs, vectorize kmedoids cost, batch tracker writes
- _dedup_embeddings: pre-allocated (Q,D) buffer replaces vstack-on-keep,
  dropping O(K²×D) copy overhead down to O(K×D) fill work
- _kmedoids: swap cost sum replaced with numpy fancy-index reduction,
  ~20-50x faster per swap evaluation
- _reconcile_frigate_mappings: O(L) load/save pairs collapsed to one
  batch write via record_frigate_files_batch
2026-06-14 04:03:55 +00:00
github-actions[bot] 56c802a245 chore: update lockfiles 2026-06-14 03:50:30 +00:00
flan cc092ec972 fix: cap fetch_all_assets at 5000 items; filter non-dict page entries
Fetching up to MAX_PAGES*page_size (1M) assets before the 3000-item
diversity pool cap was applied could exhaust memory on large Immich
libraries. Early-exit once 5000 items are collected — the pool cap
of 3000 makes anything beyond that wasteful. Also filter null/non-dict
items from page responses at fetch time.
2026-06-14 03:49:45 +00:00
github-actions[bot] d6e4582401 chore: update lockfiles 2026-06-14 03:42:28 +00:00
flan a85bc31da9 release: merge dev → main for 0.4.5 2026-06-14 03:41:35 +00:00
flan 3c602e2ef6 fix: code review corrections — dedup O(N²), truncated rejection check, pool warning, path guard
- _dedup_embeddings: rebuild kept_stack only on keep (was every iteration → O(N²))
- _dedup_embeddings: fix quality_score sort key to use explicit None check (falsy-zero)
- _select_by_embedding: add post-dedup pool < limit guard with warning
- executor: use full resp.text for 'face' keyword check; only truncate display snippet
- _safe_person_dir: avoid false "//" prefix when output_dir resolves to filesystem root
2026-06-14 03:40:39 +00:00
flan 96b71798ad docs: add disclaimer that winnow is not an approved Frigate training method 2026-06-14 03:25:38 +00:00
flan a7d4504db9 docs: add disclaimer that winnow is not an approved Frigate training method 2026-06-14 03:24:31 +00:00
github-actions[bot] e05363f632 chore: update lockfiles 2026-06-14 03:23:07 +00:00
github-actions[bot] b276d686f8 chore: update lockfiles 2026-06-14 03:22:57 +00:00
flan a3e54dea7a release: merge dev → main for 0.4.4 2026-06-14 03:22:38 +00:00
flan f9482eec4d chore: bump version to 0.4.4, update changelog 2026-06-14 03:22:32 +00:00
flan 694f860b6d Raise near-duplicate dedup threshold from 0.10 to 0.20
0.10 only removed burst shots (distance 0.01-0.05). Same-event photos with
similar pose and lighting sit at 0.10-0.20 and were passing through,
producing visually similar training images especially for people with small
datasets. 0.20 removes these while still preserving genuinely different
poses, expressions, and lighting conditions.
2026-06-14 02:52:12 +00:00
flan 6e34d41036 Guard person-name path traversal in output directory construction
os.path.join silently discards the base when the second arg is absolute,
and '../..' sequences escape the output tree. _safe_person_dir() resolves
both paths with realpath and rejects any name that lands outside the output
directory, logging an error and skipping the job rather than touching an
unintended path.
2026-06-14 01:48:20 +00:00
flan a9c1114b86 Warn when a person named '*' exists during RESET_PERSON=* bulk reset 2026-06-14 01:46:46 +00:00
flan 19f1a5e03b Add RESET_PERSON=* to reset all tracked people; fix near-duplicate dedup
- RESET_PERSON=* resets every tracked person (deletes their Frigate files
  and clears the tracker). Any other value resets that specific person by
  name, including someone literally named 'all'.
- Near-duplicate removal pass added before diversity clustering: greedily
  drops candidates within 0.10 cosine distance of a higher-quality image,
  eliminating burst-shot duplicates that FPS would otherwise pass through.
2026-06-14 01:46:28 +00:00
flan 0a8a0c16dd Deduplicate near-identical embeddings before diversity selection
Burst shots produce embeddings that differ slightly (~0.01-0.05 cosine
distance) due to JPEG noise and minor lighting variation, so FPS does not
filter them. Add a greedy dedup pass after embedding collection: sort
candidates by quality score descending, then drop any candidate within
0.10 cosine distance of an already-kept image. The best frame from each
near-identical group survives; the rest are dropped before clustering.
2026-06-14 01:26:41 +00:00
flan eb3abe2cca Fix misleading HTTP 500 detail and RuntimeWarning on single-image selection
- HTTP 500 errors from Frigate no longer echo the response body to the user
  (Frigate's generic message says 'Try restarting Frigate' which is wrong —
  500s on upload are almost always image-specific, not a health issue). The
  detail is now logged at debug level. HTTP 400 detail is still shown since
  'No face was detected' is genuinely useful.
- np.median on empty upper triangle (n=1 after quality filtering) no longer
  emits RuntimeWarning; _compute_adaptive_threshold returns the floor (0.05)
  immediately when there are no pairwise distances to sample.
- k-medoids cluster count floor raised to 1 (was 0 when n < 3), preventing
  k=0 being passed to _kmedoids.
2026-06-14 01:23:46 +00:00
flan 44b717d615 release: merge dev → main for 0.4.3 2026-06-14 00:47:06 +00:00
github-actions[bot] 168a8e33b5 chore: update lockfiles 2026-06-14 00:27:42 +00:00
flan fd0bd213e8 chore: bump version to 0.4.3, update changelog 2026-06-14 00:26:56 +00:00
flan 7efe283e9a Merge feature/insightface-crop-alignment into dev 2026-06-14 00:26:23 +00:00
flan 2dd911e9ea Detect and handle duplicate Immich people with the same name
When Immich has multiple person records sharing a name (e.g. unmerged
face clusters), winnow would previously run separate jobs for each,
with the second job wiping the first job's output directory — resulting
in far fewer training images than expected.

New behaviour:
- At startup, duplicate names are detected and a warning is printed
  showing asset counts for each duplicate.
- By default (MERGE_DUPLICATE_PEOPLE=false), only the person with the
  most assets is processed; smaller duplicates are skipped cleanly.
- With MERGE_DUPLICATE_PEOPLE=true, the duplicates are permanently
  merged inside Immich via PUT /api/people/{id}/merge (keeps the
  largest), then the people list is re-fetched before jobs run.

Also adds an explicit comment in executor.py confirming that replacement
targets come exclusively from tracker-mapped files, so manually-added
Frigate training images are never selected for deletion.
2026-06-13 23:58:38 +00:00
flan 341c6b0e85 Use InsightFace for landmark-based face crop alignment
Immich's /api/faces endpoint only returns bounding boxes, not facial
landmarks. This meant align_face() never fired and all crops fell back
to a plain bbox rectangle — producing partial crops (forehead-only,
off-angle faces) when Immich's detection was slightly off.

Now, when ENABLE_FACE_ALIGNMENT is true and InsightFace is loaded,
execute_jobs() passes the app to process_face_mode(). For each face,
it expands the Immich bbox by 50%, crops that search region, runs
InsightFace detection within it, and aligns the nearest face to the
standard ArcFace 112×112 format using norm_crop(). Falls back to bbox
crop if InsightFace finds no face in the search region.

The InsightFace model is already in GPU memory from the diversity/
embedding phase, so the singleton lookup adds no load cost.
2026-06-13 23:13:50 +00:00
flan 38fe4d6f0c ci: merge dev → main — drop arm64 from GPU build 2026-06-13 22:44:43 +00:00
flan 6fb3d2f61d ci: drop arm64 from GPU build — use :cpu for arm64 instead 2026-06-13 22:43:27 +00:00
flan 9c42da4d37 docs: merge dev → main — AI attribution disclosure 2026-06-13 22:31:26 +00:00
flan 3888a5e6db docs: disclose AI-assisted development in CONTRIBUTING.md 2026-06-13 22:28:41 +00:00
github-actions[bot] 68505aeb0b chore: update lockfiles 2026-06-13 22:19:49 +00:00
github-actions[bot] b804b13644 chore: update lockfiles 2026-06-13 22:19:49 +00:00
flanandClaude Sonnet 4.6 0f86c1054a chore: bump version to 0.4.2, update changelog
CUDA base image downgraded to 12.8.1 (driver 570 compatibility fix),
benchmark script added.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 22:19:16 +00:00
flanandClaude Sonnet 4.6 634688fc93 Fix ruff lint errors in benchmark.py
Remove unused imports, fix unsorted imports, remove bare f-strings.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 22:19:16 +00:00
flanandClaude Sonnet 4.6 9f0a78522f Downgrade GPU base image to CUDA 12.8.1; add benchmark script
CUDA 13.3 requires driver >= 575 but the host only has 570 (error 804).
CUDA 12.8.1 is the highest version supported by driver 570 and works
correctly with the NVIDIA Container Toolkit.

Add scripts/benchmark.py to measure InsightFace + SigLIP latency and
throughput across GPU and CPU modes.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 22:19:16 +00:00
flanandClaude Sonnet 4.6 8d1f5da05a ci: consolidate Docker builds — eliminate duplicate builds on release
release.yml now calls docker-publish.yml via workflow_call instead of
re-running all four image builds independently. docker-publish.yml gains
workflow_call inputs (tag, version) for release context; branch trigger
is narrowed to dev only (main changes only land via tagged releases).

Each release previously built all four variants twice (~90 min) — once on
merge to main, once on tag push. Now it builds once.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 22:19:16 +00:00
flanandClaude Sonnet 4.6 62bc1b70c5 chore: bump version to 0.4.2, update changelog
CUDA base image downgraded to 12.8.1 (driver 570 compatibility fix),
benchmark script added.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 22:19:06 +00:00
flanandClaude Sonnet 4.6 67f1845687 Fix ruff lint errors in benchmark.py
Remove unused imports, fix unsorted imports, remove bare f-strings.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 22:16:54 +00:00
flanandClaude Sonnet 4.6 39111d3a6a Downgrade GPU base image to CUDA 12.8.1; add benchmark script
CUDA 13.3 requires driver >= 575 but the host only has 570 (error 804).
CUDA 12.8.1 is the highest version supported by driver 570 and works
correctly with the NVIDIA Container Toolkit.

Add scripts/benchmark.py to measure InsightFace + SigLIP latency and
throughput across GPU and CPU modes.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 22:14:07 +00:00
flanandClaude Sonnet 4.6 5553877c90 ci: consolidate Docker builds — eliminate duplicate builds on release
release.yml now calls docker-publish.yml via workflow_call instead of
re-running all four image builds independently. docker-publish.yml gains
workflow_call inputs (tag, version) for release context; branch trigger
is narrowed to dev only (main changes only land via tagged releases).

Each release previously built all four variants twice (~90 min) — once on
merge to main, once on tag push. Now it builds once.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 21:00:58 +00:00
github-actions[bot] e4edf202fa chore: update lockfiles 2026-06-13 19:59:26 +00:00
github-actions[bot] a7257cf031 chore: update lockfiles 2026-06-13 19:59:04 +00:00
flanandClaude Sonnet 4.6 326fdbdf38 release: merge dev → main for 0.4.1
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 19:58:19 +00:00
flanandClaude Sonnet 4.6 405413490b fix: RESET_PERSON now deletes managed Frigate files before clearing tracker
Previously reset_person wiped the local tracker but left existing Frigate
training files as orphans, causing the next run to upload a full new batch
on top of them. Now deletes all winnow-managed files from Frigate first so
the next run starts truly clean. Manually-added Frigate files are never
touched.

Also fixes a spurious warning when FRIGATE_URL is unset: the deletion step
is now skipped at info level rather than logging a misleading error. Moves
the deferred import to top-level and eliminates a double disk read.

Bumps to 0.4.1. Also fixes ruff lint violations in executor.py (import
sort, line length) and promotes the "winnow only touches files it uploaded"
callout to the README intro.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 19:58:08 +00:00
flanandClaude Sonnet 4.6 91e0858aa6 refactor: merge dev → main — post-0.4.0 cleanup
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 18:55:42 +00:00
flanandClaude Sonnet 4.6 e67f2d9638 refactor: cleanup audit findings — dedup helpers, prune orphan scores, cache has_frigate_scores
- upload_tracker: extract _pick_mapped_file() private helper; get_lowest_quality_mapped_file
  and get_most_redundant_mapped_file are now one-liners over the same body
- upload_tracker: remove_frigate_file now also prunes the corresponding frigate_scores entry,
  preventing unbounded accumulation of orphaned score entries across replacement cycles
- frigate_api: get_frigate_face_counts delegates to get_all_frigate_person_files, eliminating
  the duplicated "name != 'train' and isinstance(files, list)" filter body
- executor: cache has_frigate_scores(name) as person_has_fscores before the per-file loop;
  refresh it after each remove_frigate_file call and after each scored upload, eliminating
  two redundant disk reads per at-cap file iteration
- executor: casefold() both sides of the recognize_face person-name comparison so a Frigate
  casing normalization or manual-registration casing mismatch does not silently suppress scoring

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 18:55:18 +00:00
github-actions[bot] 71df0e81de chore: update lockfiles 2026-06-13 18:36:36 +00:00
flanandClaude Sonnet 4.6 9bb0727807 release: merge dev → main for 0.4.0
Frigate pre-upload scoring, quality replacement inversion, bootstrap fix,
FRIGATE_SCORE_CEILING / ENABLE_FRIGATE_SCORES, removal of post-upload gate.
See CHANGELOG.md [0.4.0] for the full list.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 18:36:02 +00:00
github-actions[bot] bbbac18207 chore: update lockfiles 2026-06-13 18:35:54 +00:00
flanandClaude Sonnet 4.6 6fcea587ff chore: bump version to 0.4.0, update changelog and all docs
Finalizes the 0.4.0 release:

- Version bumped to 0.4.0 in pyproject.toml
- CHANGELOG.md: add [0.4.0] section covering Frigate pre-upload scoring,
  quality replacement inversion, bootstrap fix, FRIGATE_SCORE_CEILING,
  ENABLE_FRIGATE_SCORES, removal of post-upload quality gate, and all
  doc/default corrections
- README.md: step 8 updated for dual-mode replacement, FRIGATE_SCORE_CEILING
  and ENABLE_FRIGATE_SCORES added to env var table, MIN_FACE_WIDTH and
  BLUR_THRESHOLD defaults corrected (50→90, 100→120)
- .env.example: FRIGATE_SCORE_THRESHOLD replaced with FRIGATE_SCORE_CEILING;
  QUALITY_REPLACEMENT line added; comments updated to match current semantics
- winnow/executor.py: bootstrap fix — recognize now called for all below-cap
  uploads when ENABLE_FRIGATE_SCORES=true (was gated on CEILING > 0)
- winnow/upload_tracker.py: frigate_scores schema comment corrected to
  pre-upload; get_most_redundant_mapped_file() added
- winnow/frigate_api.py: recognize_face returns (face_name, score)|None tuple
  so wrong-person scores never drive replacement or ceiling decisions
- winnow/config.py: FRIGATE_SCORE_THRESHOLD renamed to FRIGATE_SCORE_CEILING;
  ENABLE_FRIGATE_SCORES added
- tests/test_upload_tracker.py: 4 new tests for get_most_redundant_mapped_file

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 18:35:11 +00:00
flanandClaude Sonnet 4.6 ed045f07dd fix: label InsightFace skip as "detection confidence" to distinguish from Frigate score
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 17:22:58 +00:00
flanandClaude Sonnet 4.6 110a45f467 perf: batch GET /api/faces; skip download on low confidence; batch gate tracker writes
Fetch all Frigate training files once before the upload loop instead of once
per person — for N people this reduces GET /api/faces calls from N to 1.
Falls back to per-person calls if the pre-fetch fails.

Check InsightFace detection confidence immediately after face enrichment,
before fetching the full-resolution image. Assets that fail MIN_CONFIDENCE
are skipped without downloading, saving potentially large image downloads.

Collapse the gate removal tracker writes from 3×N file ops into 2 total
via remove_and_reclassify_batch: one write to the uploaded tracker (remove
file mappings + remove from flat set) and one write to the rejected tracker.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 17:22:08 +00:00
flanandClaude Sonnet 4.6 785c9d4a22 feat: quality gate on by default; fix replacement/gate conflict; gate-failed → rejected
Dynamic floor now always active once Frigate scores exist — new images must
score at least as well as the weakest image already in the set, with no
config required. FRIGATE_SCORE_THRESHOLD adds an explicit absolute floor on
top. Gate active state is surfaced in normal output for both cases.

Quality replacement now pre-checks the gate threshold before deleting the
worst image. If the candidate would fail the gate, replacement is skipped
entirely rather than creating a net slot loss.

Gate-failed assets are reclassified as rejected (moved from uploaded_asset_ids
to rejected_asset_ids) so they are excluded from future runs without wasting
API calls on re-upload. RESET_PERSON still clears rejected records for a true
full reset. RETRY_REJECTED can recover them if the threshold is later lowered.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 17:10:30 +00:00
flanandClaude Sonnet 4.6 e0a5d98df6 fix: surface dynamic gate floor in normal output
When the dynamic threshold (min stored Frigate score) raises the effective
gate floor above the configured FRIGATE_SCORE_THRESHOLD, print it as a dim
info line rather than only logging at DEBUG/VERBOSE level.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 17:06:19 +00:00
flanandClaude Sonnet 4.6 ef5934d1af fix: stale frigate mapping reconciliation, recognize opt-out, cold start notice
- At upload start, diff tracker vs live Frigate file list and remove any
  mappings for files no longer present; corrects effective_count so manually
  deleted files don't permanently consume quota slots
- Add ENABLE_FRIGATE_SCORES config (default true); when false, skips all
  recognize_face calls and falls back to blur scores for quality replacement
- Print a dim notice when FRIGATE_SCORE_THRESHOLD is set but pre_run_count
  is zero, so users know the gate is deferred to the next run

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 16:51:44 +00:00
flanandClaude Sonnet 4.6 903d7f1054 fix: quality gate disabled at threshold 0; batch deletions; summary shows net count
- FRIGATE_SCORE_THRESHOLD=0.0 now fully disables the quality gate including the
  dynamic floor; a positive value is required to activate either
- Post-reconcile gate deletions are batched into one API call per person instead
  of one call per file
- Per-person summary reports gate removals and net uploaded count when the gate
  fires; grand summary includes total removed across all people
- .env.example comment updated to match the corrected opt-in behaviour

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 16:41:31 +00:00
flanandClaude Sonnet 4.6 f322eba380 feat: dynamic Frigate score threshold from stored set minimum
At the start of each person's upload phase, compute the minimum stored
Frigate recognition score across all currently mapped files. Use
max(config_threshold, dynamic_min) as the effective gate threshold so
new uploads must score at least as well as the weakest image already
in the training set.

Prevents overtraining well-recognised people: if all 80 images score
≥0.85, the dynamic threshold becomes ~0.85 and new additions that
score below that are removed rather than diluting a good training set.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 16:32:47 +00:00
flanandClaude Sonnet 4.6 26b598db98 feat: post-upload quality gate via FRIGATE_SCORE_THRESHOLD
When FRIGATE_SCORE_THRESHOLD > 0, images that score below the threshold
after upload are deleted from Frigate and removed from the tracker.
Skipped when pre_run_count == 0 (cold start — no class mean to compare
against yet). Deletion happens after reconciliation so the Frigate
filename is known. Disabled by default (0.0).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 16:29:29 +00:00
flanandClaude Sonnet 4.6 03be6ce2cb feat: store Frigate recognition scores and use them for quality replacement
After each successful upload, call POST /api/faces/recognize to get
Frigate's own confidence score (0-1) for the uploaded crop. Store it
in the tracker as frigate_scores alongside the existing blur score.

When quality replacement activates and frigate_scores are present,
use them for the replacement comparison instead of blur scores — an
image Frigate recognizes poorly is a worse training image than one it
recognizes well, regardless of sharpness. Falls back to blur scores
on first run before any frigate_scores are populated.

Also surfaces frigate_score in TRACE_CROP_SIZE output.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 16:21:58 +00:00
flanandClaude Sonnet 4.6 ab641847b2 fix: raise BLUR_THRESHOLD default from 100 to 120 to match Frigate's floor
Frigate classifies images with Laplacian variance < 120 as "very blurry"
and its own docs recommend avoiding blurry training data. Winnow was
accepting images in the 100-120 range that Frigate considers too blurry.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 15:56:52 +00:00
flanandClaude Sonnet 4.6 2b1d9e8b8a chore: bump version to 0.3.3, update changelog and lockfile
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 15:45:12 +00:00
flanandClaude Sonnet 4.6 4856a6d36f fix: raise MIN_FACE_WIDTH default from 50 to 90px (8k pixel floor)
50px crops produce ~2,500–4,225 total pixels — well below Frigate's own
camera capture range of 16k–50k px. 90px guarantees ≥8,100 total pixels
even when face margins are fully clipped by image edges.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 15:45:12 +00:00
flanandClaude Sonnet 4.6 7c306a4423 chore: bump version to 0.3.3, update changelog and lockfile
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 15:45:06 +00:00
flanandClaude Sonnet 4.6 c22857b912 fix: raise MIN_FACE_WIDTH default from 50 to 90px (8k pixel floor)
50px crops produce ~2,500–4,225 total pixels — well below Frigate's own
camera capture range of 16k–50k px. 90px guarantees ≥8,100 total pixels
even when face margins are fully clipped by image edges.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 15:45:06 +00:00
github-actions[bot] c53172f5ff chore: update lockfiles 2026-06-13 15:33:27 +00:00
github-actions[bot] c0c2d88941 chore: update lockfiles 2026-06-13 15:33:25 +00:00
flanandClaude Sonnet 4.6 9598142997 release: v0.3.2
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 15:32:33 +00:00
flanandClaude Sonnet 4.6 82dbc8502a feat: merge crop-dimension-trace into dev
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 15:32:27 +00:00
flanandClaude Sonnet 4.6 6311227763 chore: regenerate uv.lock for 0.3.2 version bump
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 15:30:14 +00:00
flanandClaude Sonnet 4.6 19416cc7b8 feat: record crop pixel dimensions and add TRACE_CROP_SIZE lookup
Store (width, height) of each face crop in the tracker at upload time
alongside the existing blur score. Expose TRACE_CROP_SIZE=<px> to look
up which Immich asset produced a crop with that pixel dimension, making
it straightforward to trace unexpected or low-quality images visible in
Frigate back to their source.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 15:20:54 +00:00
flanandClaude Sonnet 4.6 80c5b563b2 Merge main: bump transformers lower bound to >=5.12.0 in variant files
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 14:54:02 +00:00
flanandClaude Sonnet 4.6 de1df19642 chore: bump transformers lower bound to >=5.12.0 in variant pyproject files
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 14:53:56 +00:00
github-actions[bot] 3ae6a86684 chore: update lockfiles 2026-06-13 14:53:14 +00:00
github-actions[bot] 621c310d5b chore: update lockfiles 2026-06-13 14:52:59 +00:00
flanandClaude Sonnet 4.6 467f258fa5 chore: bump transformers lower bound to >=5.12.0
Was >=4.57.6; installed version is 5.12.0. Prevents users from
accidentally resolving the old 4.x series.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 14:52:27 +00:00
flanandClaude Sonnet 4.6 36a5fb56b6 fix: revert ruff-action to v3 (v4 major tag does not exist)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 14:47:31 +00:00
flanandClaude Sonnet 4.6 f90af65cd2 fix: revert setup-uv to v7 (v8 major tag does not exist)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 14:45:02 +00:00
flanandClaude Sonnet 4.6 cd3fad8afe chore: bump astral-sh actions to latest major versions
ruff-action v3 → v4, setup-uv v7 → v8

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 14:42:43 +00:00
flanandClaude Sonnet 4.6 102827537e Merge dev: bump docker actions to Node.js 24-compatible versions
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 14:40:48 +00:00
flanandClaude Sonnet 4.6 aa12c79f9d chore: bump docker actions to Node.js 24-compatible versions
setup-qemu-action v3 → v4, build-push-action v6 → v7
Required before June 16 when Node.js 20 actions are forced to Node.js 24

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 14:40:37 +00:00
github-actions[bot] 0f6b17f76f chore: update lockfiles 2026-06-13 14:26:04 +00:00
github-actions[bot] c61e4dc698 chore: update lockfiles 2026-06-13 14:26:03 +00:00
33 changed files with 1391 additions and 2428 deletions
+5 -2
View File
@@ -23,13 +23,16 @@ STRATEGY=auto
# YEARS_FILTER=10 # Only include images from the last N years (default: 10) # YEARS_FILTER=10 # Only include images from the last N years (default: 10)
# ── Image Quality ───────────────────────────────────────────────────────────── # ── Image Quality ─────────────────────────────────────────────────────────────
# MIN_FACE_WIDTH=50 # Minimum face width in pixels (default: 50) # MIN_FACE_WIDTH=90 # Minimum face width in pixels (default: 90, guarantees ≥8,100px crop)
# FACE_MARGIN=0.15 # Padding around face crop as fraction (default: 0.15) # FACE_MARGIN=0.15 # Padding around face crop as fraction (default: 0.15)
# ENABLE_FACE_ALIGNMENT=true # Align face before cropping (default: true) # ENABLE_FACE_ALIGNMENT=true # Align face before cropping (default: true)
# USE_FULL_RESOLUTION=true # Use full-res images vs thumbnails (default: true) # USE_FULL_RESOLUTION=true # Use full-res images vs thumbnails (default: true)
# MIN_CONFIDENCE=0.7 # Minimum face detection confidence (default: 0.7) # MIN_CONFIDENCE=0.7 # Minimum face detection confidence (default: 0.7)
# BLUR_THRESHOLD=100.0 # Laplacian blur threshold; lower = accept more blur (default: 100.0) # BLUR_THRESHOLD=120.0 # Laplacian blur threshold; lower = accept more blur (default: 120.0)
# MAX_AUTO_IMAGES=80 # Hard cap on auto-diversity selection (default: 80) # MAX_AUTO_IMAGES=80 # Hard cap on auto-diversity selection (default: 80)
# QUALITY_REPLACEMENT=true # At cap, replace a weaker tracked image with a better candidate (default: true)
# FRIGATE_SCORE_CEILING=0.0 # Skip uploads already well-covered (pre-upload score > ceiling = redundant; 0 = disabled; requires at least one prior run)
# ENABLE_FRIGATE_SCORES=true # Call Frigate's recognize endpoint pre-upload to store diversity scores (default: true; adds ~200ms per upload)
# ── Caching & Models ────────────────────────────────────────────────────────── # ── Caching & Models ──────────────────────────────────────────────────────────
# FORCE_CPU=true # Disable GPU, fall back to CPU # FORCE_CPU=true # Disable GPU, fall back to CPU
+127 -44
View File
@@ -2,7 +2,7 @@ name: Publish Docker Image
on: on:
push: push:
branches: ["main", "dev"] branches: ["dev"]
paths-ignore: paths-ignore:
- "**.md" - "**.md"
- "docs/**" - "docs/**"
@@ -15,6 +15,16 @@ on:
- "uv-cpu.lock" - "uv-cpu.lock"
- "uv-rocm.lock" - "uv-rocm.lock"
- "uv-intel.lock" - "uv-intel.lock"
workflow_call:
inputs:
tag:
type: string
required: false
description: "Release tag, e.g. v0.4.1 — triggers :latest + versioned image tags"
version:
type: string
required: false
description: "Version string without v prefix, e.g. 0.4.1"
concurrency: concurrency:
group: docker-${{ github.ref }} group: docker-${{ github.ref }}
@@ -26,15 +36,13 @@ env:
jobs: jobs:
build: build:
name: Build (${{ matrix.platform }}) name: Build (linux/amd64)
runs-on: ${{ matrix.runner }} runs-on: ubuntu-latest
strategy: strategy:
matrix: matrix:
include: include:
- platform: linux/amd64 - platform: linux/amd64
runner: ubuntu-latest runner: ubuntu-latest
- platform: linux/arm64
runner: ubuntu-latest
permissions: permissions:
contents: read contents: read
packages: write packages: write
@@ -50,10 +58,8 @@ jobs:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v6 uses: actions/checkout@v6
with:
- name: Set up QEMU ref: ${{ inputs.tag || github.ref }}
if: matrix.platform == 'linux/arm64'
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4 uses: docker/setup-buildx-action@v4
@@ -65,13 +71,23 @@ jobs:
username: ${{ github.actor }} username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }} password: ${{ secrets.GITHUB_TOKEN }}
- name: Compute build version
id: version
run: |
if [ -n "${{ inputs.version }}" ]; then
echo "value=${{ inputs.version }}" >> "$GITHUB_OUTPUT"
else
echo "value=dev" >> "$GITHUB_OUTPUT"
fi
- name: Build and push by digest - name: Build and push by digest
id: build id: build
uses: docker/build-push-action@v6 uses: docker/build-push-action@v7
with: with:
context: . context: .
file: ./Dockerfile file: ./Dockerfile
platforms: ${{ matrix.platform }} platforms: ${{ matrix.platform }}
build-args: VERSION=${{ steps.version.outputs.value }}
cache-from: type=gha,scope=${{ matrix.platform }} cache-from: type=gha,scope=${{ matrix.platform }}
cache-to: type=gha,mode=max,scope=${{ matrix.platform }} cache-to: type=gha,mode=max,scope=${{ matrix.platform }}
github-token: ${{ secrets.GITHUB_TOKEN }} github-token: ${{ secrets.GITHUB_TOKEN }}
@@ -86,7 +102,7 @@ jobs:
- name: Upload digest - name: Upload digest
uses: actions/upload-artifact@v4 uses: actions/upload-artifact@v4
with: with:
name: digest-${{ matrix.platform == 'linux/amd64' && 'amd64' || 'arm64' }} name: digest-amd64
path: /tmp/digests/* path: /tmp/digests/*
if-no-files-found: error if-no-files-found: error
retention-days: 1 retention-days: 1
@@ -120,22 +136,26 @@ jobs:
- name: Determine image tags - name: Determine image tags
id: tags id: tags
run: | run: |
if [ "${{ github.ref_name }}" = "dev" ]; then IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}"
echo "tags=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:dev" >> "$GITHUB_OUTPUT" INPUT_TAG="${{ inputs.tag }}"
if [ -n "$INPUT_TAG" ]; then
echo "tag_args=-t ${IMAGE}:latest -t ${IMAGE}:${INPUT_TAG}" >> "$GITHUB_OUTPUT"
echo "inspect_tag=${IMAGE}:latest" >> "$GITHUB_OUTPUT"
else else
echo "tags=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest" >> "$GITHUB_OUTPUT" echo "tag_args=-t ${IMAGE}:dev" >> "$GITHUB_OUTPUT"
echo "inspect_tag=${IMAGE}:dev" >> "$GITHUB_OUTPUT"
fi fi
- name: Create and push multi-arch manifest - name: Create and push multi-arch manifest
working-directory: /tmp/digests working-directory: /tmp/digests
run: | run: |
docker buildx imagetools create \ docker buildx imagetools create \
-t ${{ steps.tags.outputs.tags }} \ ${{ steps.tags.outputs.tag_args }} \
$(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *) $(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *)
- name: Inspect image - name: Inspect image
run: | run: |
docker buildx imagetools inspect ${{ steps.tags.outputs.tags }} docker buildx imagetools inspect ${{ steps.tags.outputs.inspect_tag }}
- name: Ensure package is public - name: Ensure package is public
run: | run: |
@@ -162,9 +182,11 @@ jobs:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v6 uses: actions/checkout@v6
with:
ref: ${{ inputs.tag || github.ref }}
- name: Set up QEMU - name: Set up QEMU
uses: docker/setup-qemu-action@v3 uses: docker/setup-qemu-action@v4
- name: Set up Docker Buildx - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4 uses: docker/setup-buildx-action@v4
@@ -176,31 +198,50 @@ jobs:
username: ${{ github.actor }} username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }} password: ${{ secrets.GITHUB_TOKEN }}
- name: Determine CPU image tag - name: Determine CPU image tags
id: cpu-tag id: cpu-tags
run: | run: |
if [ "${{ github.ref_name }}" = "dev" ]; then IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}"
echo "tag=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:dev-cpu" >> "$GITHUB_OUTPUT" INPUT_TAG="${{ inputs.tag }}"
if [ -n "$INPUT_TAG" ]; then
{
echo "tags<<EOF"
printf '%s\n' "${IMAGE}:cpu" "${IMAGE}:${INPUT_TAG}-cpu"
echo "EOF"
} >> "$GITHUB_OUTPUT"
echo "inspect_tag=${IMAGE}:cpu" >> "$GITHUB_OUTPUT"
else else
echo "tag=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:cpu" >> "$GITHUB_OUTPUT" echo "tags=${IMAGE}:dev-cpu" >> "$GITHUB_OUTPUT"
echo "inspect_tag=${IMAGE}:dev-cpu" >> "$GITHUB_OUTPUT"
fi
- name: Compute build version
id: version
run: |
if [ -n "${{ inputs.version }}" ]; then
echo "value=${{ inputs.version }}" >> "$GITHUB_OUTPUT"
else
echo "value=dev" >> "$GITHUB_OUTPUT"
fi fi
- name: Build and push CPU image - name: Build and push CPU image
uses: docker/build-push-action@v6 uses: docker/build-push-action@v7
with: with:
context: . context: .
file: ./Dockerfile file: ./Dockerfile
platforms: linux/amd64,linux/arm64 platforms: linux/amd64,linux/arm64
build-args: VARIANT=cpu build-args: |
VARIANT=cpu
VERSION=${{ steps.version.outputs.value }}
cache-from: type=gha,scope=cpu cache-from: type=gha,scope=cpu
cache-to: type=gha,mode=max,scope=cpu cache-to: type=gha,mode=max,scope=cpu
github-token: ${{ secrets.GITHUB_TOKEN }} github-token: ${{ secrets.GITHUB_TOKEN }}
push: true push: true
tags: ${{ steps.cpu-tag.outputs.tag }} tags: ${{ steps.cpu-tags.outputs.tags }}
- name: Inspect CPU image - name: Inspect CPU image
run: | run: |
docker buildx imagetools inspect ${{ steps.cpu-tag.outputs.tag }} docker buildx imagetools inspect ${{ steps.cpu-tags.outputs.inspect_tag }}
- name: Ensure package is public - name: Ensure package is public
run: | run: |
@@ -227,6 +268,8 @@ jobs:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v6 uses: actions/checkout@v6
with:
ref: ${{ inputs.tag || github.ref }}
- name: Set up Docker Buildx - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4 uses: docker/setup-buildx-action@v4
@@ -238,31 +281,50 @@ jobs:
username: ${{ github.actor }} username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }} password: ${{ secrets.GITHUB_TOKEN }}
- name: Determine ROCm image tag - name: Determine ROCm image tags
id: rocm-tag id: rocm-tags
run: | run: |
if [ "${{ github.ref_name }}" = "dev" ]; then IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}"
echo "tag=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:dev-rocm" >> "$GITHUB_OUTPUT" INPUT_TAG="${{ inputs.tag }}"
if [ -n "$INPUT_TAG" ]; then
{
echo "tags<<EOF"
printf '%s\n' "${IMAGE}:rocm" "${IMAGE}:${INPUT_TAG}-rocm"
echo "EOF"
} >> "$GITHUB_OUTPUT"
echo "inspect_tag=${IMAGE}:rocm" >> "$GITHUB_OUTPUT"
else else
echo "tag=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:rocm" >> "$GITHUB_OUTPUT" echo "tags=${IMAGE}:dev-rocm" >> "$GITHUB_OUTPUT"
echo "inspect_tag=${IMAGE}:dev-rocm" >> "$GITHUB_OUTPUT"
fi
- name: Compute build version
id: version
run: |
if [ -n "${{ inputs.version }}" ]; then
echo "value=${{ inputs.version }}" >> "$GITHUB_OUTPUT"
else
echo "value=dev" >> "$GITHUB_OUTPUT"
fi fi
- name: Build and push ROCm image - name: Build and push ROCm image
uses: docker/build-push-action@v6 uses: docker/build-push-action@v7
with: with:
context: . context: .
file: ./Dockerfile file: ./Dockerfile
platforms: linux/amd64 platforms: linux/amd64
build-args: VARIANT=rocm build-args: |
VARIANT=rocm
VERSION=${{ steps.version.outputs.value }}
cache-from: type=gha,scope=linux/amd64-rocm cache-from: type=gha,scope=linux/amd64-rocm
cache-to: type=gha,mode=max,scope=linux/amd64-rocm cache-to: type=gha,mode=max,scope=linux/amd64-rocm
github-token: ${{ secrets.GITHUB_TOKEN }} github-token: ${{ secrets.GITHUB_TOKEN }}
push: true push: true
tags: ${{ steps.rocm-tag.outputs.tag }} tags: ${{ steps.rocm-tags.outputs.tags }}
- name: Inspect ROCm image - name: Inspect ROCm image
run: | run: |
docker buildx imagetools inspect ${{ steps.rocm-tag.outputs.tag }} docker buildx imagetools inspect ${{ steps.rocm-tags.outputs.inspect_tag }}
- name: Ensure package is public - name: Ensure package is public
run: | run: |
@@ -289,6 +351,8 @@ jobs:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v6 uses: actions/checkout@v6
with:
ref: ${{ inputs.tag || github.ref }}
- name: Set up Docker Buildx - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4 uses: docker/setup-buildx-action@v4
@@ -300,31 +364,50 @@ jobs:
username: ${{ github.actor }} username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }} password: ${{ secrets.GITHUB_TOKEN }}
- name: Determine Intel image tag - name: Determine Intel image tags
id: intel-tag id: intel-tags
run: | run: |
if [ "${{ github.ref_name }}" = "dev" ]; then IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}"
echo "tag=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:dev-intel" >> "$GITHUB_OUTPUT" INPUT_TAG="${{ inputs.tag }}"
if [ -n "$INPUT_TAG" ]; then
{
echo "tags<<EOF"
printf '%s\n' "${IMAGE}:intel" "${IMAGE}:${INPUT_TAG}-intel"
echo "EOF"
} >> "$GITHUB_OUTPUT"
echo "inspect_tag=${IMAGE}:intel" >> "$GITHUB_OUTPUT"
else else
echo "tag=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:intel" >> "$GITHUB_OUTPUT" echo "tags=${IMAGE}:dev-intel" >> "$GITHUB_OUTPUT"
echo "inspect_tag=${IMAGE}:dev-intel" >> "$GITHUB_OUTPUT"
fi
- name: Compute build version
id: version
run: |
if [ -n "${{ inputs.version }}" ]; then
echo "value=${{ inputs.version }}" >> "$GITHUB_OUTPUT"
else
echo "value=dev" >> "$GITHUB_OUTPUT"
fi fi
- name: Build and push Intel image - name: Build and push Intel image
uses: docker/build-push-action@v6 uses: docker/build-push-action@v7
with: with:
context: . context: .
file: ./Dockerfile file: ./Dockerfile
platforms: linux/amd64 platforms: linux/amd64
build-args: VARIANT=intel build-args: |
VARIANT=intel
VERSION=${{ steps.version.outputs.value }}
cache-from: type=gha,scope=linux/amd64-intel cache-from: type=gha,scope=linux/amd64-intel
cache-to: type=gha,mode=max,scope=linux/amd64-intel cache-to: type=gha,mode=max,scope=linux/amd64-intel
github-token: ${{ secrets.GITHUB_TOKEN }} github-token: ${{ secrets.GITHUB_TOKEN }}
push: true push: true
tags: ${{ steps.intel-tag.outputs.tag }} tags: ${{ steps.intel-tags.outputs.tags }}
- name: Inspect Intel image - name: Inspect Intel image
run: | run: |
docker buildx imagetools inspect ${{ steps.intel-tag.outputs.tag }} docker buildx imagetools inspect ${{ steps.intel-tags.outputs.inspect_tag }}
- name: Ensure package is public - name: Ensure package is public
run: | run: |
+8 -182
View File
@@ -127,188 +127,14 @@ jobs:
prerelease: false, prerelease: false,
}); });
build-gpu: build-images:
name: Build GPU image name: Build and push Docker images
needs: release needs: release
runs-on: ubuntu-latest uses: ./.github/workflows/docker-publish.yml
with:
tag: ${{ needs.release.outputs.tag }}
version: ${{ needs.release.outputs.version }}
secrets: inherit
permissions: permissions:
packages: write packages: write
steps: contents: read
- name: Free up disk space
run: |
sudo rm -rf /usr/share/dotnet
sudo rm -rf /opt/ghc
sudo rm -rf "/usr/local/share/boost"
sudo rm -rf "$AGENT_TOOLSDIRECTORY"
echo "Disk space freed."
- name: Checkout
uses: actions/checkout@v6
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Log in to GHCR
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push GPU image (latest)
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
platforms: linux/amd64,linux/arm64
push: true
build-args: VERSION=${{ needs.release.outputs.version }}
cache-from: type=gha,scope=release-gpu
cache-to: type=gha,mode=max,scope=release-gpu
tags: |
ghcr.io/sudolulo/winnow:latest
ghcr.io/sudolulo/winnow:${{ needs.release.outputs.tag }}
build-cpu:
name: Build CPU image
needs: release
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- name: Free up disk space
run: |
sudo rm -rf /usr/share/dotnet
sudo rm -rf /opt/ghc
sudo rm -rf "/usr/local/share/boost"
sudo rm -rf "$AGENT_TOOLSDIRECTORY"
echo "Disk space freed."
- name: Checkout
uses: actions/checkout@v6
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Log in to GHCR
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push CPU image
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
platforms: linux/amd64,linux/arm64
push: true
build-args: |
VARIANT=cpu
VERSION=${{ needs.release.outputs.version }}
cache-from: type=gha,scope=release-cpu
cache-to: type=gha,mode=max,scope=release-cpu
tags: |
ghcr.io/sudolulo/winnow:cpu
ghcr.io/sudolulo/winnow:${{ needs.release.outputs.tag }}-cpu
build-rocm:
name: Build ROCm image
needs: release
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- name: Free up disk space
run: |
sudo rm -rf /usr/share/dotnet
sudo rm -rf /opt/ghc
sudo rm -rf "/usr/local/share/boost"
sudo rm -rf "$AGENT_TOOLSDIRECTORY"
echo "Disk space freed."
- name: Checkout
uses: actions/checkout@v6
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Log in to GHCR
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push ROCm image
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
platforms: linux/amd64
push: true
build-args: |
VARIANT=rocm
VERSION=${{ needs.release.outputs.version }}
cache-from: type=gha,scope=release-rocm
cache-to: type=gha,mode=max,scope=release-rocm
tags: |
ghcr.io/sudolulo/winnow:rocm
ghcr.io/sudolulo/winnow:${{ needs.release.outputs.tag }}-rocm
build-intel:
name: Build Intel image
needs: release
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- name: Free up disk space
run: |
sudo rm -rf /usr/share/dotnet
sudo rm -rf /opt/ghc
sudo rm -rf "/usr/local/share/boost"
sudo rm -rf "$AGENT_TOOLSDIRECTORY"
echo "Disk space freed."
- name: Checkout
uses: actions/checkout@v6
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Log in to GHCR
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Intel image
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
platforms: linux/amd64
push: true
build-args: |
VARIANT=intel
VERSION=${{ needs.release.outputs.version }}
cache-from: type=gha,scope=release-intel
cache-to: type=gha,mode=max,scope=release-intel
tags: |
ghcr.io/sudolulo/winnow:intel
ghcr.io/sudolulo/winnow:${{ needs.release.outputs.tag }}-intel
+146
View File
@@ -7,6 +7,152 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
## [0.4.11] - 2026-06-14
### Removed
- **Object mode pipeline fully removed**: YOLO object detection, SigLIP image classification, `TRAINING_MODE`, and `OBJECT_CLASS` env vars are gone. Frigate has no training API for objects; the ~2 GB model stack (torch, torchvision, transformers, ultralytics) was dead weight.
- **Dead Immich embedding path removed**: `FaceData.embedding` field and the `immich_embedding` parameter to `get_embedding()` were never consumed by any caller. Both removed along with the NumPy import in `immich_api.py` that existed solely for that path.
- **Dead `mode` config key removed**: `"mode": "face"` was written into job config dicts in `jobs.py` but never read after object mode removal.
### Fixed
- **InsightFace `FutureWarning` suppressed in crop-alignment path**: the `insightface_app.get()` call in `image_processing.py` now wraps the same `warnings.catch_warnings()` suppressor already present in `embeddings.py`, preventing scikit-image deprecation noise in logs.
### Changed
- **Variant pyproject files synced to current state**: `pyproject-rocm.toml`, `pyproject-cpu.toml`, `pyproject-intel.toml` were at v0.2.13 and still listed torch/transformers/ultralytics. Updated to v0.4.11 and cleaned to face-only deps. Note: corresponding lockfiles (uv-rocm.lock, uv-cpu.lock, uv-intel.lock) need regeneration in their respective platform environments.
- **`MERGE_DUPLICATE_PEOPLE` documented**: README and wiki now explain the default warn-and-skip behaviour vs. setting `true` for a permanent Immich merge, with irreversibility callout.
- **Wiki fully updated**: all five wiki pages rewritten to remove object mode references, correct model size (~300 MB InsightFace vs former ~1–2 GB HuggingFace+InsightFace), fix default values (`MAX_AUTO_IMAGES` 80→20, `MIN_FACE_COUNT` 0→3), add `MERGE_DUPLICATE_PEOPLE` coverage, and update GPU verification commands for current ONNX provider API.
## [0.4.10] - 2026-06-14
### Changed
- **`MAX_AUTO_IMAGES` default lowered from `80` to `20`**: winnow is designed to fill the gap where manual Frigate training images don't exist — not to be the primary dataset. A conservative default ensures winnow-imported images remain secondary to hand-picked ones where both exist.
## [0.4.9] - 2026-06-14
### Changed
- **`FRIGATE_SCORE_CEILING` is now dynamic by default**: previously defaulted to `0` (disabled). Now unset (default) enables a self-calibrating novelty gate — below-cap candidates are skipped if their pre-upload Frigate score exceeds the most-redundant tracked file's score. This catches conditions already covered by manually-added Frigate images that winnow cannot track. Set `FRIGATE_SCORE_CEILING=0` to disable entirely; set a positive value (e.g. `0.85`) for a fixed hard ceiling.
- **Quality replacement branches consolidated**: the Frigate-score and blur-score replacement paths in the upload loop shared identical structure. Merged into a single code path parameterised by score source and comparison direction.
- **`MIN_FACE_COUNT` default raised from `0` to `3`**: people with fewer than 3 tagged photos produce degenerate training sets; skipping them by default avoids noisy runs.
- **`STRATEGY=adaptive`** is the new primary name for embedding-based diversity selection; `auto` remains a silent alias for backwards compatibility.
- **`MERGE_DUPLICATE_PEOPLE` and `TRACE_CROP_SIZE`** added to the README env var table (were in the codebase but undocumented).
- **CUDA version corrected** in the image tags table (was 13.3, actual base image is 12.8.1).
## [0.4.8] - 2026-06-14
### Changed
- **Tracker write-through cache**: `upload_tracker` now keeps an in-memory copy of each JSON file keyed by its resolved path. All reads after the first hit the cache instead of disk; writes go to both disk and cache atomically. Cuts per-person disk I/O in the upload loop from ~90 reads to ~1, with no API or behaviour changes.
## [0.4.7] - 2026-06-14
### Changed
- **`_dedup_embeddings` pre-allocated buffer**: replaced the grow-on-keep `np.vstack` pattern with a pre-allocated `(Q, D)` buffer filled row-by-row. Eliminates O(K²) copy work and the GC pressure from K intermediate heap allocations while keeping identical arithmetic for the similarity checks.
- **`_kmedoids` cost computation vectorized**: the Python-level `sum(dist_matrix[i, medoids[labels[i]]] for i in range(n))` generator (called once per swap evaluation) is replaced with `dist_matrix[np.arange(n), np.array(medoids)[labels]].sum()` — a single numpy fancy-index + reduction, ~20–50× faster in the swap loop.
- **`_reconcile_frigate_mappings` single-write batch**: previously called `record_frigate_file` once per uploaded file, each doing a full JSON load + save (O(L) disk round-trips per person). Now builds the full `{frigate_filename: asset_id}` mapping dict and writes it in one `record_frigate_files_batch` call (O(1) disk round-trip).
## [0.4.6] - 2026-06-14
### Fixed
- **OOM when Immich returns many pages per person**: `fetch_all_assets` now stops fetching once 5000 assets have been collected — the diversity selection pool is already capped at 3000 items, so fetching up to 1,000,000 was wasteful and could exhaust memory on large libraries. 5000 provides ample headroom for the pool cap while bounding per-person memory to ~2 MB.
- **Non-dict items in Immich asset pages silently skipped**: a malformed or partially-null Immich response page could include `null` or non-object items in the assets array. These are now filtered at fetch time rather than causing `AttributeError` downstream.
## [0.4.5] - 2026-06-14
### Fixed
- **Near-duplicate dedup O(N²) allocation**: `np.vstack(kept_normed)` was rebuilt on every loop iteration even for candidates that would be dropped; the stack is now rebuilt only when a new item is kept, reducing memory pressure significantly for large pools.
- **`quality_score` falsy-zero in dedup sort**: the sort key used `c.get("quality_score") or 0.0`, which treated a legitimate `quality_score=0.0` identically to a missing key. Changed to an explicit `None` check so zero is preserved as-is, and object-mode candidates (which have no `quality_score`) continue to sort stably to the back.
- **Post-dedup pool not re-checked against limit**: after near-duplicate removal the pool could silently shrink below the requested limit with no warning. A second `len < limit` guard now fires after dedup and emits the same "Only N embeddings" warning that the pre-dedup guard does.
- **`mark_rejected` could miss plain-text 400 bodies longer than 100 bytes**: `error_detail = resp.text[:100]` was being searched for the keyword `"face"` to gate `mark_rejected()`, so a response body with `"face"` after byte 100 would never mark the asset rejected and it would be retried on every future run. The `"face"` check now uses the full response body; truncation is kept only for the displayed snippet.
- **`_safe_person_dir` raised ValueError for all person names when `output_dir` resolved to `/`**: `base + os.sep` produced `"//"` when base was `"/"`, and valid paths like `/alice` don't start with `"//"`. Fixed by using `base` directly as the prefix when `base == os.sep`.
## [0.4.4] - 2026-06-14
### Added
- **`RESET_PERSON=*` bulk reset**: resets every tracked person at once (deletes their Frigate training files and clears tracker data). Any other value still resets that specific person by name. If a person is literally named `*` they are reset as part of the bulk operation, and a warning is printed to clarify this.
- **Near-duplicate removal before diversity selection**: a greedy dedup pass now runs after embedding collection and before clustering. Candidates within 0.20 cosine distance of a higher-quality image are dropped, eliminating burst shots and same-event lookalike photos that produce redundant training images. The best-quality frame from each near-identical group is kept. Dropped count is logged per person.
### Fixed
- **HTTP 500 upload errors no longer show Frigate's misleading "Try restarting Frigate" message**: the response body is now logged at debug level only. HTTP 400 detail (e.g. "No face was detected") is still shown since it is actionable.
- **`RuntimeWarning: Mean of empty slice`** when a person has only one image after quality filtering: `_compute_adaptive_threshold` now returns the floor value immediately when there are no pairwise distances to sample, and the k-medoids cluster count is floored at 1 to prevent `k=0`.
- **Path traversal guard on output directory**: person names with `../` sequences or absolute paths (e.g. `/etc`) are now rejected before any filesystem operation, logging an error and skipping the job rather than writing outside the output tree.
## [0.4.3] - 2026-06-14
### Added
- **InsightFace landmark-based face crop alignment**: face crops for Frigate training are now aligned using InsightFace's `norm_crop` (ArcFace 112×112 alignment with 5-point facial landmarks). Previously, Immich's API returned only bounding boxes with no landmarks, so `align_face()` was dead code and crops were plain bbox slices — resulting in misaligned or partial crops (e.g. foreheads). The fix runs InsightFace detection on an expanded region around the Immich bbox, finds the nearest face, and uses its keypoints for proper alignment. Controlled by `ENABLE_FACE_ALIGNMENT` (default `true`).
- **Duplicate Immich person detection and handling**: when multiple Immich person records share the same name, winnow now detects this at startup and warns with a per-group summary. Without handling, two jobs would run for the same Frigate folder and overwrite each other's output. By default (`MERGE_DUPLICATE_PEOPLE=false`) only the first person per name is processed. Set `MERGE_DUPLICATE_PEOPLE=true` to permanently merge duplicate records inside Immich (keeps the person with the most assets).
## [0.4.2] - 2026-06-13
### Changed
- **GPU image now uses CUDA 12.8.1** (was 13.3): CUDA 13.3 requires driver ≥ 575; driver 570 (the current stable release) was incorrectly rejected with "CUDA driver version is insufficient" at startup. The `:latest` image now works with any NVIDIA driver ≥ 570.
### Added
- **`scripts/benchmark.py`**: measures InsightFace and SigLIP inference latency and throughput across GPU and CPU modes. Run inside the container with `python /app/scripts/benchmark.py`. RTX 2070 SUPER results: InsightFace 12.8 ms / 78 img/s (8× CPU), SigLIP batch 32 at 5.4 ms/img / 187 img/s (33× CPU).
## [0.4.1] - 2026-06-13
### Fixed
- **`RESET_PERSON` no longer creates duplicate Frigate files**: previously, resetting a person only wiped the local tracker — existing Frigate training files were left as unmanaged orphans, causing the next run to upload a full new batch on top of them. `reset_person` now deletes all winnow-managed files for that person from Frigate before clearing the tracker. Manually-added Frigate files are unaffected.
- **No spurious warning when `FRIGATE_URL` is unset and `RESET_PERSON` is used**: the deletion step is now skipped silently at info level rather than logging a misleading "could not delete" warning.
## [0.4.0] - 2026-06-13
### Added
- **Pre-upload Frigate recognition scores**: `recognize_face` is now called before each upload to measure how novel the candidate is relative to the existing training set. The score is stored in the tracker (`frigate_scores` field) and drives quality replacement in subsequent runs. Adds ~200 ms per upload.
- **`ENABLE_FRIGATE_SCORES`** (default `true`): controls all pre-upload Frigate recognize calls. Set `false` to use blur-score replacement only and skip the Frigate round-trip entirely.
- **`FRIGATE_SCORE_CEILING`** (default `0.0`): skip uploads whose pre-upload recognize score already exceeds this value — those face conditions are already well-covered by the training set. `0` disables (no ceiling); requires at least one prior run to have stored scores.
- **`get_most_redundant_mapped_file()`**: new upload-tracker function that returns the mapped file with the highest Frigate pre-upload score. High score = the training set already covers that face condition well = the best deletion target for quality replacement.
- **Cold-start notice**: first run (no existing Frigate model) now logs a clear message explaining why Frigate scores are unavailable and that they will populate on subsequent runs.
- **4 new tests** for `get_most_redundant_mapped_file` covering score ordering, ties, excludes, and no-score cases.
### Changed
- **Quality replacement now uses Frigate scores**: when Frigate scores are available, at-cap replacement targets the _most redundant_ mapped file (highest pre-upload score) and replaces it only when the candidate is _more novel_ (lower score). Falls back to blur-score comparison when no Frigate scores have been stored yet.
- **`recognize_face` returns `(face_name, score) | None`** instead of `float | None`: the caller now validates that the recognized person matches the expected person before using the score. Wrong-person scores no longer drive ceiling skips or replacement decisions.
- **Bootstrap fix**: recognize was previously called below-cap only when `FRIGATE_SCORE_CEILING > 0`, so `frigate_scores` was never populated with default settings and the Frigate replacement path never activated. Recognize is now called for all below-cap uploads when `ENABLE_FRIGATE_SCORES=true`, seeding scores for future at-cap runs regardless of ceiling setting.
- **Batch GET `/api/faces`**: Frigate file-count lookups are now batched to reduce round-trip overhead on runs with many people.
- **Skip candidate download on low Frigate confidence**: candidates where the Immich detection confidence is below threshold are now filtered before the full-resolution download, saving bandwidth.
### Removed
- **Post-upload quality gate (`FRIGATE_SCORE_THRESHOLD`)**: enforcement of a Frigate score threshold after upload has been removed. Post-upload scores are taken after the image is already in the training set, so the model has already retrained on it — deleting it at that point is wasteful and disrupts the model for the next Frigate run. Pre-upload scoring (`FRIGATE_SCORE_CEILING`) provides a cleaner signal at the right moment.
### Fixed
- **Frigate replacement path never activated with default settings**: with `FRIGATE_SCORE_CEILING=0.0` (default), the bootstrap call to `recognize_face` was gated behind `CEILING > 0`, so `frigate_scores` stayed empty, `has_frigate_scores` was always False, and the Frigate replacement branch was permanently unreachable. Removing the ceiling guard from the below-cap recognize call breaks the circular dependency.
- **Schema comment contradiction**: `upload_tracker.py` line-16 comment described `frigate_scores` as "post-upload" while the block comment on lines 22–24 said "pre-upload". Corrected to "pre-upload" throughout.
- **README default values**: `MIN_FACE_WIDTH` was documented as `50` (actual default: `90`); `BLUR_THRESHOLD` was documented as `100.0` (actual default: `120.0`). Both corrected.
- **README missing env vars**: `FRIGATE_SCORE_CEILING` and `ENABLE_FRIGATE_SCORES` were present in `config.py` and `.env.example` but absent from the README env var table. Both added.
- **README quality-replacement description**: Step 8 and the `QUALITY_REPLACEMENT` row now document the dual-mode behaviour (Frigate-score path and blur-score fallback) instead of describing only the original blur-score path.
## [0.3.3] - 2026-06-13
### Fixed
- **`MIN_FACE_WIDTH` default raised from 50 → 90px**: 50px crops produce 2,500–4,225 total pixels, well below Frigate's own camera capture range of 16k–50k px. 90px guarantees ≥8,100 total pixels even when face margins are fully clipped by image edges, keeping winnow training crops above the floor Frigate considers useful.
## [0.3.2] - 2026-06-13
### Added
- **Crop dimension tracing**: winnow now records the pixel dimensions (width × height) of each face crop at upload time in the tracker (`crop_dims` field). Run `TRACE_CROP_SIZE=3848 winnow` to look up which Immich asset produced a crop with that pixel dimension — output includes person name, asset ID, Immich URL, blur score, and the Frigate filename. Useful for tracing low-quality or unexpected images visible in Frigate back to their source.
## [0.3.1] - 2026-06-13 ## [0.3.1] - 2026-06-13
### Fixed ### Fixed
+4
View File
@@ -36,6 +36,10 @@ CI runs both on every push and PR to `main` and `dev`. PRs must pass before merg
- Keep the `CHANGELOG.md` entry in the `[Unreleased]` section updated. - Keep the `CHANGELOG.md` entry in the `[Unreleased]` section updated.
- Commit messages should be plain English describing what changed and why. - Commit messages should be plain English describing what changed and why.
## Development Tooling
Development uses Claude Code (Anthropic) for implementation assistance. All code is reviewed and the final call on design, behavior, and what ships is made by the maintainer. Contributions from humans are equally welcome.
## License ## License
By submitting a contribution you agree that your work will be released under the project's [AGPLv3+ license](LICENSE). By submitting a contribution you agree that your work will be released under the project's [AGPLv3+ license](LICENSE).
+5 -5
View File
@@ -1,5 +1,5 @@
# ── Base images ─────────────────────────────────────────────────────────────── # ── Base images ───────────────────────────────────────────────────────────────
# amd64 + gpu: NVIDIA CUDA 13.3 + cuDNN (GPU acceleration via NVIDIA Container Toolkit) # amd64 + gpu: NVIDIA CUDA 12.8 + cuDNN (GPU acceleration via NVIDIA Container Toolkit)
# amd64 + rocm: Ubuntu 22.04 (AMD GPU via ROCm — pass /dev/kfd and /dev/dri) # amd64 + rocm: Ubuntu 22.04 (AMD GPU via ROCm — pass /dev/kfd and /dev/dri)
# amd64 + intel: Ubuntu 22.04 (Intel Arc / iGPU via OpenVINO — pass /dev/dri) # amd64 + intel: Ubuntu 22.04 (Intel Arc / iGPU via OpenVINO — pass /dev/dri)
# amd64 + cpu: Ubuntu 22.04 (CPU-only, ~2 GB smaller image) # amd64 + cpu: Ubuntu 22.04 (CPU-only, ~2 GB smaller image)
@@ -7,7 +7,7 @@
ARG VARIANT=gpu ARG VARIANT=gpu
FROM --platform=$BUILDPLATFORM nvidia/cuda:13.3.0-cudnn-runtime-ubuntu22.04 AS base-amd64-gpu FROM --platform=$BUILDPLATFORM nvidia/cuda:12.8.1-cudnn-runtime-ubuntu22.04 AS base-amd64-gpu
FROM ubuntu:22.04 AS base-amd64-rocm FROM ubuntu:22.04 AS base-amd64-rocm
FROM ubuntu:22.04 AS base-amd64-intel FROM ubuntu:22.04 AS base-amd64-intel
FROM ubuntu:22.04 AS base-amd64-cpu FROM ubuntu:22.04 AS base-amd64-cpu
@@ -67,7 +67,7 @@ FROM base-${TARGETARCH}-${VARIANT} AS runtime
ARG VARIANT=gpu ARG VARIANT=gpu
ARG VERSION=dev ARG VERSION=dev
LABEL org.opencontainers.image.title="winnow" \ LABEL org.opencontainers.image.title="winnow" \
org.opencontainers.image.description="Selects diverse, high-quality photos from Immich as training data for Frigate face recognition and object classification." \ org.opencontainers.image.description="Selects diverse, high-quality photos from Immich as training data for Frigate face recognition." \
org.opencontainers.image.source="https://github.com/sudolulo/winnow" \ org.opencontainers.image.source="https://github.com/sudolulo/winnow" \
org.opencontainers.image.licenses="AGPL-3.0-or-later" \ org.opencontainers.image.licenses="AGPL-3.0-or-later" \
org.opencontainers.image.version="${VERSION}" org.opencontainers.image.version="${VERSION}"
@@ -116,7 +116,7 @@ https://repositories.intel.com/graphics/ubuntu jammy flex" \
fi fi
RUN groupadd -g 568 apps && useradd -u 568 -g apps -m -s /bin/bash appuser \ RUN groupadd -g 568 apps && useradd -u 568 -g apps -m -s /bin/bash appuser \
&& mkdir -p /models/.insightface /models/huggingface \ && mkdir -p /models/.insightface \
&& chown -R appuser:apps /app /models && chown -R appuser:apps /app /models
WORKDIR /app WORKDIR /app
@@ -124,7 +124,7 @@ USER appuser
# PYTHONPATH=/app makes the winnow package importable from the entry point script. # PYTHONPATH=/app makes the winnow package importable from the entry point script.
# uv sync builds the wheel before winnow/ is COPY'd, so site-packages has only # uv sync builds the wheel before winnow/ is COPY'd, so site-packages has only
# the dist-info. Explicitly adding /app lets Python find winnow/__init__.py there. # the dist-info. Explicitly adding /app lets Python find winnow/__init__.py there.
ENV HF_HOME=/models/huggingface INSIGHTFACE_HOME=/models/.insightface PYTHONPATH=/app ENV INSIGHTFACE_HOME=/models/.insightface PYTHONPATH=/app
HEALTHCHECK CMD test -f /app/entrypoint.sh || exit 1 HEALTHCHECK CMD test -f /app/entrypoint.sh || exit 1
ENTRYPOINT ["tini", "--", "/app/entrypoint.sh"] ENTRYPOINT ["tini", "--", "/app/entrypoint.sh"]
+61 -45
View File
@@ -2,11 +2,18 @@
[![Docker](https://github.com/sudolulo/winnow/actions/workflows/docker-publish.yml/badge.svg)](https://github.com/sudolulo/winnow/actions/workflows/docker-publish.yml) [![Test](https://github.com/sudolulo/winnow/actions/workflows/test.yml/badge.svg)](https://github.com/sudolulo/winnow/actions/workflows/test.yml) [![GitHub release](https://img.shields.io/github/v/release/sudolulo/winnow)](https://github.com/sudolulo/winnow/releases/latest) [![License: AGPL v3](https://img.shields.io/badge/License-AGPL_v3-blue.svg)](LICENSE) [![Immich](https://img.shields.io/badge/Immich-v1.106%2B-blueviolet)](https://immich.app) [![Frigate](https://img.shields.io/badge/Frigate-Ready-brightgreen)](https://frigate.video) [![Docker](https://github.com/sudolulo/winnow/actions/workflows/docker-publish.yml/badge.svg)](https://github.com/sudolulo/winnow/actions/workflows/docker-publish.yml) [![Test](https://github.com/sudolulo/winnow/actions/workflows/test.yml/badge.svg)](https://github.com/sudolulo/winnow/actions/workflows/test.yml) [![GitHub release](https://img.shields.io/github/v/release/sudolulo/winnow)](https://github.com/sudolulo/winnow/releases/latest) [![License: AGPL v3](https://img.shields.io/badge/License-AGPL_v3-blue.svg)](LICENSE) [![Immich](https://img.shields.io/badge/Immich-v1.106%2B-blueviolet)](https://immich.app) [![Frigate](https://img.shields.io/badge/Frigate-Ready-brightgreen)](https://frigate.video)
> **Note:** winnow's approach to training Frigate face recognition is not an officially documented workflow — results may vary.
> **Early Development — Use With Caution**
> winnow is functional but still maturing. Features that modify your Frigate training data — quality replacement, stale mapping cleanup — can remove images from your dataset and are not yet battle-tested at scale. Review the logs after each run and keep backups of your Frigate face training directory until you are confident in the results.
**Docs:** [Setup](https://github.com/sudolulo/winnow/wiki/Setup) · [Troubleshooting](https://github.com/sudolulo/winnow/wiki/Troubleshooting) · [FAQ](https://github.com/sudolulo/winnow/wiki/FAQ) **Docs:** [Setup](https://github.com/sudolulo/winnow/wiki/Setup) · [Troubleshooting](https://github.com/sudolulo/winnow/wiki/Troubleshooting) · [FAQ](https://github.com/sudolulo/winnow/wiki/FAQ)
`winnow` pulls photos from your [Immich](https://immich.app) library, selects the most diverse and highest-quality subset using AI embeddings, and delivers them as training data for [Frigate](https://frigate.video)'s face recognition and object classification models. `winnow` pulls photos from your [Immich](https://immich.app) library, selects the most diverse and highest-quality subset using AI embeddings, and delivers them as training data for [Frigate](https://frigate.video)'s face recognition.
Frigate's face recognition is only as good as its training data — and the key quality metric is **diversity**, not volume. A hundred photos from the same week teach the model one lighting condition. What you need is a spread: different years, different angles, different lighting, different contexts. Your photo library already has that data. winnow finds and delivers the right subset automatically. The best Frigate training data is images you curate manually — photos taken specifically for recognition, in controlled conditions, uploaded directly through Frigate's UI. For people you can do that for, do it. winnow is for everyone else: people in your library you want Frigate to recognise but don't have dedicated training photos for. It mines your existing Immich library for the most diverse spread of real-world appearances and fills the gap.
> **winnow only touches files it uploaded.** Faces added to Frigate manually through its UI are never deleted, replaced, or modified — not by quality replacement, not by `RESET_PERSON`, not by stale cleanup. Your manually curated images are always the primary dataset; winnow only adds to it.
--- ---
@@ -32,47 +39,46 @@ Immich library
│ │
▼ ▼
4. Compute embeddings from the same preview thumbnails 4. Compute embeddings from the same preview thumbnails
• Faces → InsightFace (ArcFace / Buffalo_L) → 512-dim vector • InsightFace (ArcFace / Buffalo_L) → 512-dim vector
• Objects → SigLIP (Vision Transformer) → 768-dim vector
│ │
▼ ▼
5. Diversity selection 5. Near-duplicate removal — greedy cosine-distance pass drops burst shots
and near-identical photos before clustering runs; the highest-quality
image from each near-duplicate group is kept
│
▼
6. Diversity selection
• K-Medoids clustering → one representative per natural group • K-Medoids clustering → one representative per natural group
• Farthest Point Sampling → fill remaining slots with maximally spread picks • Farthest Point Sampling → fill remaining slots with maximally spread picks
• Hard example weighting — unusual angles and low-confidence detections • Hard example weighting — low-confidence detections get a distance boost
are biased toward selection, since those are where models tend to fail so unusual angles and harder looks are preferred over easy frontals
• Auto mode: stops when similarity to the existing set exceeds a threshold • Adaptive mode: stops when the next candidate is too similar to those already
(20 % of median pairwise distance for faces, 10 % for objects) selected (distance threshold = 20 % of median pairwise distance for
faces, 10 % for objects)
│ │
▼ ▼
6. Download full-resolution originals from Immich 7. Download full-resolution originals from Immich
│ │
▼ ▼
7. Crop and process 8. Crop and process — EXIF-corrected, landmark-aligned 112×112 crop (ArcFace format)
• Face mode: EXIF-corrected, landmark-aligned 112×112 crop (ArcFace format)
• Object mode: YOLOv9c detection → one crop per matched instance
│ │
▼ ▼
8. Deliver 9. Deliver — upload crops to Frigate's face registration API
• Face mode: upload crops to Frigate's face registration API ↳ below MAX_AUTO_IMAGES — upload, unless the novelty gate
↳ below MAX_AUTO_IMAGES — upload freely (FRIGATE_SCORE_CEILING) determines the candidate is already
↳ at cap + QUALITY_REPLACEMENT=true — swap the lowest-scoring tracked covered by the current training set
image if the new candidate scores higher; manually added files are ↳ at cap + QUALITY_REPLACEMENT=true — with Frigate scoring active,
never touched swap the most redundant tracked image (highest pre-upload recognize
score) if the candidate is more novel (lower score); falling back to
blur-score comparison when no Frigate scores are available; manually
added files are never touched
↳ at cap + QUALITY_REPLACEMENT=false — skip this person ↳ at cap + QUALITY_REPLACEMENT=false — skip this person
• Object mode: save crops to disk → place into your Frigate data directory
``` ```
Uploaded and rejected asset IDs are persisted across runs. The same image is never processed twice; Frigate rejections are permanently skipped unless `RETRY_REJECTED=true`. Uploaded and rejected asset IDs are persisted across runs. The same image is never processed twice; rejected assets are permanently skipped unless `RETRY_REJECTED=true`.
--- ---
## Modes
**Face mode** (default) — extracts face crops using Immich's bounding box metadata, applies EXIF orientation correction, and aligns them to ArcFace's standard 112×112 format using 5-point facial landmarks. Crops are uploaded directly to Frigate's face registration API.
**Object mode** — runs each full-resolution image through YOLOv9c to detect instances of a target class (dog, cat, car, etc.), crops each detection, and saves it to the output directory. Frigate has no API for uploading object training data; place the crops into your Frigate data directory manually.
--- ---
## Running in Docker ## Running in Docker
@@ -81,7 +87,7 @@ Uploaded and rejected asset IDs are persisted across runs. The same image is nev
| Tag | Arch | Acceleration | | Tag | Arch | Acceleration |
| :-- | :-- | :-- | | :-- | :-- | :-- |
| `:latest` | amd64 + arm64 | NVIDIA CUDA 13.3 (amd64) · requires [NVIDIA Container Toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html) | | `:latest` | amd64 + arm64 | NVIDIA CUDA 12.8 (amd64) · requires [NVIDIA Container Toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html) |
| `:rocm` | amd64 | AMD ROCm · pass `/dev/kfd` + `/dev/dri` | | `:rocm` | amd64 | AMD ROCm · pass `/dev/kfd` + `/dev/dri` |
| `:intel` | amd64 | Intel Arc / iGPU via OpenVINO · pass `/dev/dri`, set `OPENVINO_DEVICE=GPU` | | `:intel` | amd64 | Intel Arc / iGPU via OpenVINO · pass `/dev/dri`, set `OPENVINO_DEVICE=GPU` |
| `:cpu` | amd64 + arm64 | CPU only · ~2 GB smaller · no GPU required | | `:cpu` | amd64 + arm64 | CPU only · ~2 GB smaller · no GPU required |
@@ -99,7 +105,7 @@ services:
- FRIGATE_URL=http://192.168.1.10:5000 - FRIGATE_URL=http://192.168.1.10:5000
- CRON_SCHEDULE=0 3 * * 0 - CRON_SCHEDULE=0 3 * * 0
volumes: volumes:
- /path/to/models:/models - /path/to/models:/models # INSIGHTFACE_HOME — persists Buffalo_L model (~300 MB)
- /path/to/cache:/app/.if_cache - /path/to/cache:/app/.if_cache
- /path/to/output:/app/frigate_train - /path/to/output:/app/frigate_train
deploy: deploy:
@@ -145,7 +151,7 @@ See [compose.yml](compose.yml) for the full annotated example with all options.
| *(empty string)* | Stay alive, run nothing — trigger manually with `docker exec -it winnow winnow` | | *(empty string)* | Stay alive, run nothing — trigger manually with `docker exec -it winnow winnow` |
| Cron expression | Run on startup, then repeat on schedule | | Cron expression | Run on startup, then repeat on schedule |
In scheduled mode the process (and loaded models) stays resident between runs. The first run after a fresh install downloads the embedding models (~1–2 GB); subsequent runs use the cached models from the mounted volume. In scheduled mode the process (and loaded models) stays resident between runs. The first run after a fresh install downloads InsightFace Buffalo_L (~300 MB); subsequent runs use the cached model from the mounted volume.
--- ---
@@ -163,11 +169,9 @@ In scheduled mode the process (and loaded models) stays resident between runs. T
| Variable | Default | Description | | Variable | Default | Description |
| :--- | :--- | :--- | | :--- | :--- | :--- |
| `TRAINING_MODE` | `face` | `face` — upload crops to Frigate; `object` — save crops to disk | | `STRATEGY` | `adaptive` | `adaptive` — embedding-based diversity selection, stops when candidates become redundant; `standard` — fixed 30 images; `broad` — fixed 100 images |
| `STRATEGY` | `auto` | `auto` (embedding-based adaptive), `standard` (30 images), `broad` (100 images) |
| `LIMIT` | *(unset)* | Exact image count — overrides `STRATEGY` | | `LIMIT` | *(unset)* | Exact image count — overrides `STRATEGY` |
| `OBJECT_CLASS` | `dog` | Target class for object mode (any YOLO class: `dog`, `cat`, `car`, etc.) | | `AUTO_MODE` | *(auto)* | Skip interactive prompts and process all people unattended — auto-detected when no TTY is present (Docker, cron); set `true` to force in a terminal |
| `AUTO_MODE` | *(auto)* | Force non-interactive mode in a terminal; auto-detected otherwise |
| `VERBOSE` | `false` | Enable DEBUG-level console output (log file is always DEBUG) | | `VERBOSE` | `false` | Enable DEBUG-level console output (log file is always DEBUG) |
### People Filtering ### People Filtering
@@ -176,21 +180,33 @@ In scheduled mode the process (and loaded models) stays resident between runs. T
| :--- | :--- | :--- | | :--- | :--- | :--- |
| `ONLY_PEOPLE` | *(unset)* | Comma-separated whitelist — process only these people | | `ONLY_PEOPLE` | *(unset)* | Comma-separated whitelist — process only these people |
| `SKIP_PEOPLE` | *(unset)* | Comma-separated list — skip these people | | `SKIP_PEOPLE` | *(unset)* | Comma-separated list — skip these people |
| `MIN_FACE_COUNT` | `0` | Skip people with fewer than N tagged assets in Immich | | `MIN_FACE_COUNT` | `3` | Skip people with fewer than N tagged assets in Immich |
| `MERGE_DUPLICATE_PEOPLE` | `false` | When Immich has duplicate entries for the same person (same face split across multiple names), merge their asset pools before processing. Without this, each duplicate group emits a warning and is skipped |
| `YEARS_FILTER` | `10` | Ignore images older than N years | | `YEARS_FILTER` | `10` | Ignore images older than N years |
> **Duplicate people detection** — winnow warns at startup if the same name appears on multiple Immich person records (a common side-effect of Immich's face clustering creating separate pools for the same individual). By default (`false`) it logs the duplicates, keeps only the person with the most assets, and skips the rest — no data is changed. Set `MERGE_DUPLICATE_PEOPLE=true` to permanently merge each duplicate group inside Immich (the person with the most assets absorbs the others). **This modifies Immich and cannot be undone.** Only enable it once you've verified the duplicates are actually the same person.
### Image Quality ### Image Quality
| Variable | Default | Description | | Variable | Default | Description |
| :--- | :--- | :--- | | :--- | :--- | :--- |
| `MIN_FACE_WIDTH` | `50` | Minimum face crop width in pixels | | `MAX_AUTO_IMAGES` | `20` | Maximum training images per person in Frigate |
| `QUALITY_REPLACEMENT` | `true` | When at cap, swap a weaker tracked image for a better candidate. With Frigate scoring active, targets the most redundant image (highest pre-upload recognize score); otherwise uses blur score. Never touches manually added Frigate files. Set `false` to skip people at cap |
#### Advanced Tuning *(calibrated — do not adjust)*
These defaults are tuned for Frigate's ArcFace requirements. winnow will warn on launch if any are set. Image quality issues caused by non-default values will not be investigated.
| Variable | Default | Description |
| :--- | :--- | :--- |
| `ENABLE_FRIGATE_SCORES` | `true` | Call Frigate's recognize endpoint pre-upload to store diversity scores used for quality replacement. Adds ~200 ms per upload. Disabling also disables the below-cap novelty gate |
| `FRIGATE_SCORE_CEILING` | *(unset)* | Below-cap novelty gate. Unset: dynamic — skips candidates whose Frigate score exceeds the most-redundant tracked file's score, auto-calibrates each run. `0`: disable entirely. Positive value (e.g. `0.85`): fixed hard ceiling |
| `MIN_FACE_WIDTH` | `90` | Minimum face crop width in pixels |
| `FACE_MARGIN` | `0.15` | Padding around bounding box crop (fraction of face size) | | `FACE_MARGIN` | `0.15` | Padding around bounding box crop (fraction of face size) |
| `ENABLE_FACE_ALIGNMENT` | `true` | Align to ArcFace 112×112 format using facial landmarks | | `ENABLE_FACE_ALIGNMENT` | `true` | Align to ArcFace 112×112 format using facial landmarks |
| `USE_FULL_RESOLUTION` | `true` | Download full-resolution originals rather than preview thumbnails | | `USE_FULL_RESOLUTION` | `true` | Download full-resolution originals rather than preview thumbnails |
| `MIN_CONFIDENCE` | `0.7` | Minimum Immich face detection confidence | | `MIN_CONFIDENCE` | `0.7` | Minimum Immich face detection confidence |
| `BLUR_THRESHOLD` | `100.0` | Laplacian variance threshold — lower accepts more blur | | `BLUR_THRESHOLD` | `120.0` | Laplacian variance threshold — lower accepts more blur |
| `MAX_AUTO_IMAGES` | `80` | Maximum training images per person in Frigate |
| `QUALITY_REPLACEMENT` | `true` | When at cap, swap the lowest-scoring tracked image for a better candidate. Never touches manually added Frigate files. Set `false` to skip people already at cap |
### GPU & Models ### GPU & Models
@@ -200,22 +216,22 @@ In scheduled mode the process (and loaded models) stays resident between runs. T
| `OPENVINO_DEVICE` | `CPU` | Intel variant only: set `GPU` to use Arc or iGPU; default runs on CPU | | `OPENVINO_DEVICE` | `CPU` | Intel variant only: set `GPU` to use Arc or iGPU; default runs on CPU |
| `ENABLE_CACHE` | `true` | Cache computed embeddings to disk (speeds up re-runs on the same library) | | `ENABLE_CACHE` | `true` | Cache computed embeddings to disk (speeds up re-runs on the same library) |
| `CACHE_DIR` | `.if_cache` | Path for embedding cache and upload tracker files | | `CACHE_DIR` | `.if_cache` | Path for embedding cache and upload tracker files |
| `HF_HOME` | *(system)* | HuggingFace model cache path (SigLIP) |
| `INSIGHTFACE_HOME` | *(system)* | InsightFace model cache path (Buffalo_L) | | `INSIGHTFACE_HOME` | *(system)* | InsightFace model cache path (Buffalo_L) |
### Output ### Output
| Variable | Default | Description | | Variable | Default | Description |
| :--- | :--- | :--- | | :--- | :--- | :--- |
| `OUTPUT_DIR` | `./frigate_train` | Directory for object-mode crops and the `winnow.log` file. In Docker, set this via the volume mount instead. | | `OUTPUT_DIR` | `./frigate_train` | Directory where face crops are staged before upload and where `winnow.log` is written. In Docker, set this via the volume mount instead. |
### Tracker Overrides *(one-shot — remove after use)* ### Tracker Overrides *(one-shot — remove after use)*
| Variable | Default | Description | | Variable | Default | Description |
| :--- | :--- | :--- | | :--- | :--- | :--- |
| `DRY_RUN` | `false` | Preview selection without downloading or uploading | | `DRY_RUN` | `false` | Preview selection without downloading or uploading |
| `RETRY_REJECTED` | `false` | Re-attempt assets previously rejected by Frigate | | `RETRY_REJECTED` | `false` | Re-attempt all previously rejected assets (low-confidence skips, Frigate rejections, and other permanent exclusions) |
| `RESET_PERSON` | *(unset)* | Clear upload and rejection history for one person by name | | `RESET_PERSON` | *(unset)* | Set to a person's name to clear their upload history and delete their winnow-managed Frigate training files so the next run starts fresh. Set to `*` to reset all tracked people at once. Manually added Frigate files are never touched |
| `TRACE_CROP_SIZE` | *(unset)* | Debug: print all tracked crops whose width or height matches this pixel value, then exit |
### Scheduling ### Scheduling
@@ -236,14 +252,14 @@ uv run winnow
Requires Python 3.13+ and [uv](https://astral.sh/uv). An NVIDIA, AMD, or Intel GPU is recommended — CPU mode works but embedding computation is slower. Requires Python 3.13+ and [uv](https://astral.sh/uv). An NVIDIA, AMD, or Intel GPU is recommended — CPU mode works but embedding computation is slower.
When run with a terminal attached, winnow starts an interactive session: select which people to process and choose a strategy (auto, standard, broad, or a custom count) per person. Without a TTY — Docker, cron, or `AUTO_MODE=true` — it processes all people automatically using the configured defaults. When run with a terminal attached, winnow starts an interactive session: select which people to process and choose a strategy (adaptive, standard, broad, or a custom count) per person. Without a TTY — Docker, cron, or `AUTO_MODE=true` — it processes all people unattended using the configured defaults.
--- ---
## Requirements ## Requirements
- **Immich** v1.106+ - **Immich** v1.106+
- **Frigate** v0.16+ (face mode only — object mode has no Frigate dependency) - **Frigate** v0.16+
- **GPU** recommended: NVIDIA (CUDA), AMD (ROCm), or Intel (Arc / iGPU via OpenVINO) - **GPU** recommended: NVIDIA (CUDA), AMD (ROCm), or Intel (Arc / iGPU via OpenVINO)
- **Python** 3.13+ - **Python** 3.13+
+2 -6
View File
@@ -13,19 +13,16 @@ services:
# Set AUTO_MODE=true to force auto mode in an interactive terminal. # Set AUTO_MODE=true to force auto mode in an interactive terminal.
# To run interactively: docker exec -it winnow winnow # To run interactively: docker exec -it winnow winnow
# - VERBOSE=true # Enable DEBUG-level console output # - VERBOSE=true # Enable DEBUG-level console output
# TRAINING_MODE: face = upload to Frigate face recognition API
# object = save crops to output dir for manual Frigate placement
- TRAINING_MODE=face
# STRATEGY: auto = objective diversity (recommended), standard = 30 imgs, broad = 100 imgs # STRATEGY: auto = objective diversity (recommended), standard = 30 imgs, broad = 100 imgs
- STRATEGY=auto - STRATEGY=auto
# - LIMIT=50 # Custom image count; overrides STRATEGY preset # - LIMIT=50 # Custom image count; overrides STRATEGY preset
# - OBJECT_CLASS=dog # Object label for object mode (e.g. dog, cat, car)
# ── People Filtering ────────────────────────────────────────────────── # ── People Filtering ──────────────────────────────────────────────────
# - ONLY_PEOPLE=John,Jane # Comma-separated; process only these people # - ONLY_PEOPLE=John,Jane # Comma-separated; process only these people
# - SKIP_PEOPLE=Unknown # Comma-separated; skip these people # - SKIP_PEOPLE=Unknown # Comma-separated; skip these people
# - MIN_FACE_COUNT=5 # Skip people with fewer than N assets in Immich # - MIN_FACE_COUNT=5 # Skip people with fewer than N assets in Immich
# - YEARS_FILTER=10 # Only include images from the last N years (default: 10) # - YEARS_FILTER=10 # Only include images from the last N years (default: 10)
# - MERGE_DUPLICATE_PEOPLE=true # Auto-merge Immich people with the same name (keeps most assets)
# ── Image Quality ───────────────────────────────────────────────────── # ── Image Quality ─────────────────────────────────────────────────────
# - MIN_FACE_WIDTH=50 # Minimum face width in pixels (default: 50) # - MIN_FACE_WIDTH=50 # Minimum face width in pixels (default: 50)
@@ -34,14 +31,13 @@ services:
# - USE_FULL_RESOLUTION=true # Use full-res images vs thumbnails (default: true) # - USE_FULL_RESOLUTION=true # Use full-res images vs thumbnails (default: true)
# - MIN_CONFIDENCE=0.7 # Minimum face detection confidence (default: 0.7) # - MIN_CONFIDENCE=0.7 # Minimum face detection confidence (default: 0.7)
# - BLUR_THRESHOLD=100.0 # Laplacian blur threshold; lower = accept more blur (default: 100.0) # - BLUR_THRESHOLD=100.0 # Laplacian blur threshold; lower = accept more blur (default: 100.0)
# - MAX_AUTO_IMAGES=80 # Hard cap on auto-diversity selection (default: 80) # - MAX_AUTO_IMAGES=80 # Hard cap on auto-diversity selection (default: 20)
# ── Caching & Models ────────────────────────────────────────────────── # ── Caching & Models ──────────────────────────────────────────────────
# - FORCE_CPU=true # Disable GPU, fall back to CPU # - FORCE_CPU=true # Disable GPU, fall back to CPU
# - OPENVINO_DEVICE=GPU # Intel variant only: use Arc/iGPU instead of CPU (default: CPU) # - OPENVINO_DEVICE=GPU # Intel variant only: use Arc/iGPU instead of CPU (default: CPU)
# - ENABLE_CACHE=false # Disable embedding cache (default: true) # - ENABLE_CACHE=false # Disable embedding cache (default: true)
- CACHE_DIR=/app/.if_cache - CACHE_DIR=/app/.if_cache
- HF_HOME=/models/huggingface
- INSIGHTFACE_HOME=/models/.insightface - INSIGHTFACE_HOME=/models/.insightface
# ── Tracker overrides (one-shot, remove after use) ──────────────────── # ── Tracker overrides (one-shot, remove after use) ────────────────────
+3 -22
View File
@@ -1,7 +1,7 @@
[project] [project]
name = "winnow" name = "winnow"
version = "0.2.13" version = "0.4.11"
description = "Immich to Frigate training sets" description = "Selects diverse, high-quality photos from Immich as training data for Frigate face recognition."
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"
requires-python = ">=3.13" requires-python = ">=3.13"
authors = [{ name = "Holden Salomon", email = "holden@arch.fyi" }] authors = [{ name = "Holden Salomon", email = "holden@arch.fyi" }]
@@ -23,10 +23,6 @@ dependencies = [
"python-dotenv>=1.2.1", "python-dotenv>=1.2.1",
"requests>=2.32.5", "requests>=2.32.5",
"rich>=14.2.0", "rich>=14.2.0",
"torch>=2.12.0",
"torchvision>=0.27.0",
"transformers>=4.57.6",
"ultralytics>=8.4.66",
] ]
[project.scripts] [project.scripts]
@@ -34,25 +30,13 @@ winnow = "winnow.cli:main"
[project.urls] [project.urls]
Repository = "https://github.com/sudolulo/winnow" Repository = "https://github.com/sudolulo/winnow"
Changelog = "https://github.com/sudolulo/winnow/blob/main/CHANGELOG.md"
[tool.uv] [tool.uv]
required-environments = [ required-environments = [
"sys_platform == 'linux' and platform_machine == 'x86_64'", "sys_platform == 'linux' and platform_machine == 'x86_64'",
] ]
[tool.uv.sources]
torch = [
{ index = "pytorch-cpu", marker = "sys_platform == 'linux' and platform_machine == 'x86_64'" },
]
torchvision = [
{ index = "pytorch-cpu", marker = "sys_platform == 'linux' and platform_machine == 'x86_64'" },
]
[[tool.uv.index]]
name = "pytorch-cpu"
url = "https://download.pytorch.org/whl/cpu"
explicit = true
[dependency-groups] [dependency-groups]
dev = [ dev = [
"pytest>=8.0", "pytest>=8.0",
@@ -81,9 +65,6 @@ numpy = "numpy"
onnxruntime = "onnxruntime" onnxruntime = "onnxruntime"
requests = "requests" requests = "requests"
rich = "rich" rich = "rich"
torch = "torch"
transformers = "transformers"
ultralytics = "ultralytics"
[tool.pytest.ini_options] [tool.pytest.ini_options]
testpaths = ["tests"] testpaths = ["tests"]
+3 -22
View File
@@ -1,7 +1,7 @@
[project] [project]
name = "winnow" name = "winnow"
version = "0.2.13" version = "0.4.11"
description = "Immich to Frigate training sets" description = "Selects diverse, high-quality photos from Immich as training data for Frigate face recognition."
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"
requires-python = ">=3.13" requires-python = ">=3.13"
authors = [{ name = "Holden Salomon", email = "holden@arch.fyi" }] authors = [{ name = "Holden Salomon", email = "holden@arch.fyi" }]
@@ -23,10 +23,6 @@ dependencies = [
"python-dotenv>=1.2.1", "python-dotenv>=1.2.1",
"requests>=2.32.5", "requests>=2.32.5",
"rich>=14.2.0", "rich>=14.2.0",
"torch>=2.12.0",
"torchvision>=0.27.0",
"transformers>=4.57.6",
"ultralytics>=8.4.66",
] ]
[project.scripts] [project.scripts]
@@ -34,6 +30,7 @@ winnow = "winnow.cli:main"
[project.urls] [project.urls]
Repository = "https://github.com/sudolulo/winnow" Repository = "https://github.com/sudolulo/winnow"
Changelog = "https://github.com/sudolulo/winnow/blob/main/CHANGELOG.md"
[tool.uv] [tool.uv]
conflicts = [ conflicts = [
@@ -47,19 +44,6 @@ required-environments = [
"sys_platform == 'linux' and platform_machine == 'x86_64'", "sys_platform == 'linux' and platform_machine == 'x86_64'",
] ]
[tool.uv.sources]
torch = [
{ index = "pytorch-cpu", marker = "sys_platform == 'linux' and platform_machine == 'x86_64'" },
]
torchvision = [
{ index = "pytorch-cpu", marker = "sys_platform == 'linux' and platform_machine == 'x86_64'" },
]
[[tool.uv.index]]
name = "pytorch-cpu"
url = "https://download.pytorch.org/whl/cpu"
explicit = true
[dependency-groups] [dependency-groups]
dev = [ dev = [
"pytest>=8.0", "pytest>=8.0",
@@ -88,9 +72,6 @@ numpy = "numpy"
onnxruntime-openvino = "onnxruntime" onnxruntime-openvino = "onnxruntime"
requests = "requests" requests = "requests"
rich = "rich" rich = "rich"
torch = "torch"
transformers = "transformers"
ultralytics = "ultralytics"
[tool.deptry.per_rule_ignores] [tool.deptry.per_rule_ignores]
DEP002 = ["onnxruntime-openvino"] DEP002 = ["onnxruntime-openvino"]
+3 -21
View File
@@ -1,7 +1,7 @@
[project] [project]
name = "winnow" name = "winnow"
version = "0.2.13" version = "0.4.11"
description = "Immich to Frigate training sets" description = "Selects diverse, high-quality photos from Immich as training data for Frigate face recognition."
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"
requires-python = ">=3.13" requires-python = ">=3.13"
authors = [{ name = "Holden Salomon", email = "holden@arch.fyi" }] authors = [{ name = "Holden Salomon", email = "holden@arch.fyi" }]
@@ -23,10 +23,6 @@ dependencies = [
"python-dotenv>=1.2.1", "python-dotenv>=1.2.1",
"requests>=2.32.5", "requests>=2.32.5",
"rich>=14.2.0", "rich>=14.2.0",
"torch>=2.5.0",
"torchvision>=0.20.0",
"transformers>=4.57.6",
"ultralytics>=8.4.66",
] ]
[project.scripts] [project.scripts]
@@ -34,6 +30,7 @@ winnow = "winnow.cli:main"
[project.urls] [project.urls]
Repository = "https://github.com/sudolulo/winnow" Repository = "https://github.com/sudolulo/winnow"
Changelog = "https://github.com/sudolulo/winnow/blob/main/CHANGELOG.md"
[tool.uv] [tool.uv]
index-strategy = "unsafe-best-match" index-strategy = "unsafe-best-match"
@@ -48,18 +45,6 @@ required-environments = [
"sys_platform == 'linux' and platform_machine == 'x86_64'", "sys_platform == 'linux' and platform_machine == 'x86_64'",
] ]
[tool.uv.sources]
torch = [
{ index = "pytorch-rocm63", marker = "sys_platform == 'linux' and platform_machine == 'x86_64'" },
]
torchvision = [
{ index = "pytorch-rocm63", marker = "sys_platform == 'linux' and platform_machine == 'x86_64'" },
]
[[tool.uv.index]]
name = "pytorch-rocm63"
url = "https://download.pytorch.org/whl/rocm6.3"
[dependency-groups] [dependency-groups]
dev = [ dev = [
"pytest>=8.0", "pytest>=8.0",
@@ -88,9 +73,6 @@ numpy = "numpy"
onnxruntime-rocm = "onnxruntime" onnxruntime-rocm = "onnxruntime"
requests = "requests" requests = "requests"
rich = "rich" rich = "rich"
torch = "torch"
transformers = "transformers"
ultralytics = "ultralytics"
[tool.deptry.per_rule_ignores] [tool.deptry.per_rule_ignores]
DEP002 = ["onnxruntime-rocm"] DEP002 = ["onnxruntime-rocm"]
+2 -30
View File
@@ -1,7 +1,7 @@
[project] [project]
name = "winnow" name = "winnow"
version = "0.3.1" version = "0.4.11"
description = "Selects diverse, high-quality photos from Immich as training data for Frigate face recognition and object classification." description = "Selects diverse, high-quality photos from Immich as training data for Frigate face recognition."
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"
requires-python = ">=3.13" requires-python = ">=3.13"
authors = [{ name = "Holden Salomon", email = "holden@arch.fyi" }] authors = [{ name = "Holden Salomon", email = "holden@arch.fyi" }]
@@ -27,10 +27,6 @@ dependencies = [
"python-dotenv>=1.2.1", "python-dotenv>=1.2.1",
"requests>=2.32.5", "requests>=2.32.5",
"rich>=14.2.0", "rich>=14.2.0",
"torch>=2.12.0",
"torchvision>=0.27.0",
"transformers>=4.57.6",
"ultralytics>=8.4.66",
] ]
[project.scripts] [project.scripts]
@@ -53,27 +49,6 @@ required-environments = [
"sys_platform == 'linux' and platform_machine == 'aarch64'", "sys_platform == 'linux' and platform_machine == 'aarch64'",
] ]
[tool.uv.sources]
torch = [
{ index = "pytorch-cu126", marker = "sys_platform == 'linux' and platform_machine == 'x86_64'" },
{ index = "pytorch-cpu", marker = "sys_platform == 'linux' and platform_machine == 'aarch64'" },
{ index = "pytorch-cpu", marker = "sys_platform != 'linux'" },
]
torchvision = [
{ index = "pytorch-cu126", marker = "sys_platform == 'linux' and platform_machine == 'x86_64'" },
{ index = "pytorch-cpu", marker = "sys_platform == 'linux' and platform_machine == 'aarch64'" },
{ index = "pytorch-cpu", marker = "sys_platform != 'linux'" },
]
[[tool.uv.index]]
name = "pytorch-cu126"
url = "https://download.pytorch.org/whl/cu126"
explicit = true
[[tool.uv.index]]
name = "pytorch-cpu"
url = "https://download.pytorch.org/whl/cpu"
explicit = true
[dependency-groups] [dependency-groups]
dev = [ dev = [
@@ -104,9 +79,6 @@ nvidia-cudnn-cu12 = "nvidia.cudnn"
onnxruntime-gpu = "onnxruntime" onnxruntime-gpu = "onnxruntime"
requests = "requests" requests = "requests"
rich = "rich" rich = "rich"
torch = "torch"
transformers = "transformers"
ultralytics = "ultralytics"
[tool.deptry.per_rule_ignores] [tool.deptry.per_rule_ignores]
DEP002 = ["onnxruntime-gpu", "nvidia-cudnn-cu12"] DEP002 = ["onnxruntime-gpu", "nvidia-cudnn-cu12"]
-4
View File
@@ -16,7 +16,6 @@ except ImportError:
from winnow.cli import main from winnow.cli import main
SCHEDULE = os.environ["CRON_SCHEDULE"] SCHEDULE = os.environ["CRON_SCHEDULE"]
MODELS_DIR = os.environ.get("HF_HOME", "/models/huggingface")
INSIGHTFACE_HOME = os.environ.get("INSIGHTFACE_HOME", "/models/.insightface") INSIGHTFACE_HOME = os.environ.get("INSIGHTFACE_HOME", "/models/.insightface")
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -24,11 +23,8 @@ logger = logging.getLogger(__name__)
def check_models() -> None: def check_models() -> None:
buffalo = Path(INSIGHTFACE_HOME) / "models" / "buffalo_l" buffalo = Path(INSIGHTFACE_HOME) / "models" / "buffalo_l"
hf_hub = Path(MODELS_DIR) / "hub"
if not buffalo.exists(): if not buffalo.exists():
print(" InsightFace Buffalo_L not found — will download on first run", flush=True) print(" InsightFace Buffalo_L not found — will download on first run", flush=True)
if not (hf_hub.exists() and any(hf_hub.iterdir())):
print(" HuggingFace models not found — will download on first run", flush=True)
NOW = time.time() NOW = time.time()
+132
View File
@@ -0,0 +1,132 @@
#!/usr/bin/env python3
"""
winnow inference benchmark: GPU vs CPU throughput.
Measures InsightFace (ArcFace) latency and throughput.
Run with FORCE_CPU=true for CPU-only baseline.
Usage inside container:
# GPU mode:
docker exec winnow python /app/scripts/benchmark.py
# CPU mode:
docker exec -e FORCE_CPU=true winnow python /app/scripts/benchmark.py
"""
import os
import sys
import time
import numpy as np
from PIL import Image, ImageDraw
def _mode_label() -> str:
if os.getenv("FORCE_CPU", "").lower() in ("true", "1", "yes"):
return "CPU (FORCE_CPU=true)"
return "GPU (auto)"
def make_face_image(size: int = 640) -> Image.Image:
"""Synthetic face-like image: skin-tone rectangle with landmark blobs."""
img = Image.new("RGB", (size, size), (200, 170, 140))
draw = ImageDraw.Draw(img)
# Head oval
cx, cy = size // 2, size // 2
hw, hh = int(size * 0.3), int(size * 0.38)
draw.ellipse([cx - hw, cy - hh, cx + hw, cy + hh], fill=(220, 185, 155))
# Eyes
for ex in [cx - int(size * 0.1), cx + int(size * 0.1)]:
ey = cy - int(size * 0.05)
r = max(4, size // 40)
draw.ellipse([ex - r, ey - r, ex + r, ey + r], fill=(40, 30, 20))
# Nose
draw.ellipse([cx - 5, cy + 5, cx + 5, cy + 15], fill=(180, 140, 110))
# Mouth
draw.arc([cx - 20, cy + 25, cx + 20, cy + 45], start=0, end=180, fill=(160, 80, 80), width=3)
return img
def _stats(times_s: list[float]) -> dict:
arr = np.array(times_s) * 1000 # ms
return {
"median_ms": float(np.median(arr)),
"mean_ms": float(np.mean(arr)),
"min_ms": float(np.min(arr)),
"p95_ms": float(np.percentile(arr, 95)),
"ips": 1000.0 / float(np.median(arr)),
}
def bench_insightface(n_warmup: int = 5, n_runs: int = 30) -> None:
import cv2
import winnow.embeddings as emb_mod
from winnow.embeddings import get_insightface_app
# Reset singleton so we get a fresh load
emb_mod._insightface_app = None
emb_mod._insightface_loaded = False
print(" Loading model...")
t_load = time.perf_counter()
app = get_insightface_app()
load_s = time.perf_counter() - t_load
if app is None:
print(" SKIP: InsightFace failed to load")
return
img_pil = make_face_image(640)
img_bgr = cv2.cvtColor(np.asarray(img_pil), cv2.COLOR_RGB2BGR)
# Warmup
for _ in range(n_warmup):
app.get(img_bgr)
# Timed — single image 640×640
times: list[float] = []
for _ in range(n_runs):
t0 = time.perf_counter()
app.get(img_bgr)
times.append(time.perf_counter() - t0)
s = _stats(times)
print(f" Model load time : {load_s:.2f} s")
print(" Input size : 640×640")
print(f" Runs : {n_runs} (after {n_warmup} warmup)")
print(f" Median latency : {s['median_ms']:.1f} ms")
print(f" Mean / p95 : {s['mean_ms']:.1f} ms / {s['p95_ms']:.1f} ms")
print(f" Min latency : {s['min_ms']:.1f} ms")
print(f" Throughput : {s['ips']:.1f} images/s")
# Also test at 320×320
img_sm = make_face_image(320)
img_sm_bgr = cv2.cvtColor(np.asarray(img_sm), cv2.COLOR_RGB2BGR)
for _ in range(n_warmup):
app.get(img_sm_bgr)
times_sm: list[float] = []
for _ in range(n_runs):
t0 = time.perf_counter()
app.get(img_sm_bgr)
times_sm.append(time.perf_counter() - t0)
s2 = _stats(times_sm)
print(f" 320×320 median : {s2['median_ms']:.1f} ms ({s2['ips']:.1f} img/s)")
def main() -> None:
print("=" * 56)
print(" winnow inference benchmark")
print(f" Mode: {_mode_label()}")
print("=" * 56)
print()
print("── InsightFace Buffalo_L (face detection + ArcFace) ──")
bench_insightface()
print()
if __name__ == "__main__":
# Add winnow to path when run directly inside container
sys.path.insert(0, "/app")
main()
+4 -4
View File
@@ -17,11 +17,11 @@ def test_config_loads_defaults(monkeypatch):
assert cfg.API_KEY == "test-key" assert cfg.API_KEY == "test-key"
assert cfg.OUTPUT_DIR == "./frigate_train" assert cfg.OUTPUT_DIR == "./frigate_train"
assert cfg.YEARS_FILTER == 10 assert cfg.YEARS_FILTER == 10
assert cfg.MIN_FACE_WIDTH == 50 assert cfg.MIN_FACE_WIDTH == 90
assert cfg.MIN_FACE_COUNT == 0 assert cfg.MIN_FACE_COUNT == 3
assert cfg.BLUR_THRESHOLD == 100.0 assert cfg.BLUR_THRESHOLD == 120.0
assert cfg.MIN_CONFIDENCE == 0.7 assert cfg.MIN_CONFIDENCE == 0.7
assert cfg.MAX_AUTO_IMAGES == 80 assert cfg.MAX_AUTO_IMAGES == 20
assert cfg.QUALITY_REPLACEMENT is True assert cfg.QUALITY_REPLACEMENT is True
assert cfg.FACE_MARGIN == 0.15 assert cfg.FACE_MARGIN == 0.15
assert cfg.USE_FULL_RESOLUTION is True assert cfg.USE_FULL_RESOLUTION is True
+42
View File
@@ -218,3 +218,45 @@ def test_get_lowest_quality_exclude_all_returns_none():
mark_uploaded("asset-a", person_name="Alice", score=0.50) mark_uploaded("asset-a", person_name="Alice", score=0.50)
record_frigate_file("Alice", "Alice-a.webp", "asset-a") record_frigate_file("Alice", "Alice-a.webp", "asset-a")
assert get_lowest_quality_mapped_file("Alice", exclude={"Alice-a.webp"}) is None assert get_lowest_quality_mapped_file("Alice", exclude={"Alice-a.webp"}) is None
# ── get_most_redundant_mapped_file ────────────────────────────────────────────
def test_get_most_redundant_none_when_no_frigate_scores():
from winnow.upload_tracker import get_most_redundant_mapped_file, mark_uploaded, record_frigate_file
mark_uploaded("asset-a", person_name="Alice", score=0.80)
record_frigate_file("Alice", "Alice-a.webp", "asset-a")
# blur score only, no frigate_score → no candidates
assert get_most_redundant_mapped_file("Alice") is None
def test_get_most_redundant_returns_highest_frigate_score():
from winnow.upload_tracker import get_most_redundant_mapped_file, mark_uploaded, record_frigate_file
mark_uploaded("asset-novel", person_name="Alice", score=0.50, frigate_score=0.31)
mark_uploaded("asset-redundant", person_name="Alice", score=0.90, frigate_score=0.88)
record_frigate_file("Alice", "Alice-novel.webp", "asset-novel")
record_frigate_file("Alice", "Alice-redundant.webp", "asset-redundant")
result = get_most_redundant_mapped_file("Alice")
assert result is not None
frigate_filename, asset_id, score = result
assert frigate_filename == "Alice-redundant.webp"
assert asset_id == "asset-redundant"
assert score == pytest.approx(0.88, abs=0.001)
def test_get_most_redundant_exclude_skips_file():
from winnow.upload_tracker import get_most_redundant_mapped_file, mark_uploaded, record_frigate_file
mark_uploaded("asset-hi", person_name="Alice", score=0.9, frigate_score=0.85)
mark_uploaded("asset-lo", person_name="Alice", score=0.5, frigate_score=0.40)
record_frigate_file("Alice", "Alice-hi.webp", "asset-hi")
record_frigate_file("Alice", "Alice-lo.webp", "asset-lo")
result = get_most_redundant_mapped_file("Alice", exclude={"Alice-hi.webp"})
assert result is not None
assert result[1] == "asset-lo" # hi excluded; lo is next highest
def test_get_most_redundant_exclude_all_returns_none():
from winnow.upload_tracker import get_most_redundant_mapped_file, mark_uploaded, record_frigate_file
mark_uploaded("asset-a", person_name="Alice", score=0.5, frigate_score=0.70)
record_frigate_file("Alice", "Alice-a.webp", "asset-a")
assert get_most_redundant_mapped_file("Alice", exclude={"Alice-a.webp"}) is None
+1 -1
View File
@@ -1873,7 +1873,7 @@ requires-dist = [
{ name = "torch", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'", specifier = ">=2.12.0", index = "https://download.pytorch.org/whl/cpu" }, { name = "torch", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'", specifier = ">=2.12.0", index = "https://download.pytorch.org/whl/cpu" },
{ name = "torchvision", marker = "platform_machine != 'x86_64' or sys_platform != 'linux'", specifier = ">=0.27.0" }, { name = "torchvision", marker = "platform_machine != 'x86_64' or sys_platform != 'linux'", specifier = ">=0.27.0" },
{ name = "torchvision", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'", specifier = ">=0.27.0", index = "https://download.pytorch.org/whl/cpu" }, { name = "torchvision", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'", specifier = ">=0.27.0", index = "https://download.pytorch.org/whl/cpu" },
{ name = "transformers", specifier = ">=4.57.6" }, { name = "transformers", specifier = ">=5.12.0" },
{ name = "ultralytics", specifier = ">=8.4.66" }, { name = "ultralytics", specifier = ">=8.4.66" },
] ]
+1 -1
View File
@@ -1930,7 +1930,7 @@ requires-dist = [
{ name = "torch", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'", specifier = ">=2.12.0", index = "https://download.pytorch.org/whl/cpu" }, { name = "torch", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'", specifier = ">=2.12.0", index = "https://download.pytorch.org/whl/cpu" },
{ name = "torchvision", marker = "platform_machine != 'x86_64' or sys_platform != 'linux'", specifier = ">=0.27.0" }, { name = "torchvision", marker = "platform_machine != 'x86_64' or sys_platform != 'linux'", specifier = ">=0.27.0" },
{ name = "torchvision", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'", specifier = ">=0.27.0", index = "https://download.pytorch.org/whl/cpu" }, { name = "torchvision", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'", specifier = ">=0.27.0", index = "https://download.pytorch.org/whl/cpu" },
{ name = "transformers", specifier = ">=4.57.6" }, { name = "transformers", specifier = ">=5.12.0" },
{ name = "ultralytics", specifier = ">=8.4.66" }, { name = "ultralytics", specifier = ">=8.4.66" },
] ]
+1 -1
View File
@@ -1922,7 +1922,7 @@ requires-dist = [
{ name = "torch", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'", specifier = ">=2.5.0", index = "https://download.pytorch.org/whl/rocm6.3" }, { name = "torch", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'", specifier = ">=2.5.0", index = "https://download.pytorch.org/whl/rocm6.3" },
{ name = "torchvision", marker = "platform_machine != 'x86_64' or sys_platform != 'linux'", specifier = ">=0.20.0" }, { name = "torchvision", marker = "platform_machine != 'x86_64' or sys_platform != 'linux'", specifier = ">=0.20.0" },
{ name = "torchvision", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'", specifier = ">=0.20.0", index = "https://download.pytorch.org/whl/rocm6.3" }, { name = "torchvision", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'", specifier = ">=0.20.0", index = "https://download.pytorch.org/whl/rocm6.3" },
{ name = "transformers", specifier = ">=4.57.6" }, { name = "transformers", specifier = ">=5.12.0" },
{ name = "ultralytics", specifier = ">=8.4.66" }, { name = "ultralytics", specifier = ">=8.4.66" },
] ]
Generated
+2 -1537
View File
File diff suppressed because it is too large Load Diff
+1 -2
View File
@@ -1,8 +1,7 @@
"""Immich to Frigate training set curator. """Immich to Frigate training set curator.
AI-powered tool to extract high-quality, diverse training images from your AI-powered tool to extract high-quality, diverse training images from your
Immich library for Frigate's Face Recognition (ArcFace) and Object/State Immich library for Frigate's face recognition (ArcFace/Buffalo_L).
Classification models.
""" """
from importlib.metadata import PackageNotFoundError, version from importlib.metadata import PackageNotFoundError, version
-1
View File
@@ -15,7 +15,6 @@ logger = logging.getLogger(__name__)
# Model versions — bump these when the upstream model changes # Model versions — bump these when the upstream model changes
MODEL_VERSIONS = { MODEL_VERSIONS = {
"insightface": "buffalo_l_v1", "insightface": "buffalo_l_v1",
"siglip": "siglip-base-patch16-224_v1",
"immich": "immich_buffalo_l_v1", "immich": "immich_buffalo_l_v1",
} }
+161 -3
View File
@@ -9,25 +9,165 @@ from rich.prompt import Confirm
from .config import Config, ConfigManager from .config import Config, ConfigManager
from .executor import execute_jobs, upload_to_frigate from .executor import execute_jobs, upload_to_frigate
from .immich_api import get_people from .immich_api import get_people, merge_people
from .jobs import _show_preview, auto_configure, interactive_configure from .jobs import _show_preview, auto_configure, interactive_configure
from .log_config import console, setup_logging from .log_config import console, setup_logging
from .upload_tracker import get_person_summary, reset_person from .upload_tracker import find_by_crop_dimension, get_person_summary, reset_person
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def _handle_trace_crop(size_str: str) -> None:
"""Print tracker records whose crop dimension matches the given pixel size and exit."""
try:
size = int(size_str)
except ValueError:
rprint(f"[bold red]TRACE_CROP_SIZE must be an integer, got: {size_str!r}[/bold red]")
sys.exit(1)
immich_url = os.environ.get("IMMICH_URL", "").rstrip("/")
matches = find_by_crop_dimension(size)
if not matches:
rprint(f"[yellow]No crops with dimension {size}px found in tracker.[/yellow]")
rprint("[dim]Note: crop dimensions are only recorded for uploads made after this feature was added.[/dim]")
sys.exit(0)
rprint(f"\n[bold]Crops matching dimension {size}px:[/bold] ({len(matches)} found)\n")
for m in matches:
rprint(f" [bold cyan]{m['person']}[/bold cyan]")
rprint(f" Dimensions: {m['width']}×{m['height']}px")
rprint(f" Asset ID: {m['asset_id']}")
if immich_url:
rprint(f" Immich URL: {immich_url}/photos/{m['asset_id']}")
blur = m.get("blur_score")
rprint(f" Blur score: {blur:.1f}" if blur is not None else " Blur score: unknown")
fscore = m.get("frigate_score")
rprint(f" Frigate score: {fscore:.2f}" if fscore is not None else " Frigate score: unknown")
if m.get("frigate_filename"):
rprint(f" Frigate file: {m['frigate_filename']}")
else:
rprint(" Frigate file: [dim]unmapped (reconciliation race)[/dim]")
rprint()
sys.exit(0)
def _handle_duplicate_people(people: list[dict]) -> list[dict]:
"""Warn about or merge Immich people that share the same name.
Duplicates arise when Immich creates separate person records for the same
individual (e.g. unmerged face clusters). Without handling, winnow would
run multiple jobs for the same Frigate folder and overwrite its own output,
leaving far fewer training images than expected.
With MERGE_DUPLICATE_PEOPLE=false (default): prints a warning, skips the
smaller duplicates so only the person with the most assets is processed,
and returns a deduplicated people list.
With MERGE_DUPLICATE_PEOPLE=true: merges each duplicate group inside
Immich via its API (permanently combines the face records), then
re-fetches the people list so the rest of the run sees the merged state.
"""
from collections import defaultdict
by_name: dict[str, list[dict]] = defaultdict(list)
for p in people:
name = (p.get("name") or "").strip()
if name:
by_name[name].append(p)
duplicates = {name: ps for name, ps in by_name.items() if len(ps) > 1}
if not duplicates:
return people
if not Config.MERGE_DUPLICATE_PEOPLE:
rprint("\n[bold yellow]⚠ Duplicate person names detected in Immich:[/bold yellow]")
for name, ps in sorted(duplicates.items()):
ordered = sorted(ps, key=lambda x: x.get("assetCount", 0), reverse=True)
entries = ", ".join(
f"[dim]{p['id'][:8]}…[/dim] ({p.get('assetCount', 0)} assets)"
for p in ordered
)
rprint(f" [yellow]{name}[/yellow] → {len(ps)} people: {entries}")
skipped = ordered[1:]
rprint(
f" [dim] Processing largest only "
f"({ordered[0].get('assetCount', 0)} assets). "
f"Skipping {len(skipped)} smaller duplicate(s) to avoid overwriting output.[/dim]"
)
rprint(
" [dim]Set MERGE_DUPLICATE_PEOPLE=true to permanently merge duplicates "
"inside Immich (keeps the person with the most assets).[/dim]\n"
)
# Return deduplicated list — keep only the largest per name so that
# downstream job creation never runs two jobs for the same Frigate folder.
skip_ids = {
p["id"]
for ps in duplicates.values()
for p in sorted(ps, key=lambda x: x.get("assetCount", 0), reverse=True)[1:]
}
return [p for p in people if p["id"] not in skip_ids]
# Auto-merge: survivor = largest asset count, rest merge into it inside Immich
merged_any = False
for name, ps in sorted(duplicates.items()):
ordered = sorted(ps, key=lambda x: x.get("assetCount", 0), reverse=True)
survivor = ordered[0]
merge_ids = [p["id"] for p in ordered[1:]]
rprint(
f" [cyan]Merging {name!r} inside Immich:[/cyan] keeping "
f"[dim]{survivor['id'][:8]}…[/dim] ({survivor.get('assetCount', 0)} assets), "
f"absorbing {len(merge_ids)} smaller duplicate(s)..."
)
if merge_people(survivor["id"], merge_ids):
rprint(f" [green]✓ Merged {name!r}[/green]")
merged_any = True
else:
rprint(f" [red]✗ Failed to merge {name!r}[/red]")
if merged_any:
rprint(" [dim]Re-fetching people after merge...[/dim]")
return get_people()
return people
_UNSUPPORTED_VARS = [
"ENABLE_FRIGATE_SCORES",
"FRIGATE_SCORE_CEILING",
"MIN_FACE_WIDTH",
"FACE_MARGIN",
"ENABLE_FACE_ALIGNMENT",
"USE_FULL_RESOLUTION",
"MIN_CONFIDENCE",
"BLUR_THRESHOLD",
]
def main() -> None: def main() -> None:
"""Entry point for winnow CLI.""" """Entry point for winnow CLI."""
try: try:
verbose = os.environ.get("VERBOSE", "").lower() in ("true", "1", "yes") verbose = os.environ.get("VERBOSE", "").lower() in ("true", "1", "yes")
setup_logging(verbose=verbose) setup_logging(verbose=verbose)
trace_size = os.environ.get("TRACE_CROP_SIZE", "").strip()
if trace_size:
_handle_trace_crop(trace_size)
console.print(r""" console.print(r"""
[bold blue]winnow[/bold blue] [bold blue]winnow[/bold blue]
[dim]Immich -> Frigate Training Data Curator[/dim] [dim]Immich -> Frigate Training Data Curator[/dim]
""") """)
set_unsupported = [v for v in _UNSUPPORTED_VARS if os.environ.get(v)]
if set_unsupported:
console.print(
f"[bold yellow]⚠ Advanced tuning vars set: "
f"{', '.join(set_unsupported)}[/bold yellow]"
)
console.print(
"[dim] These defaults are calibrated for Frigate's ArcFace requirements. "
"Image quality issues caused by non-default values will not be investigated.[/dim]\n"
)
ConfigManager.get().interactive_setup() ConfigManager.get().interactive_setup()
try: try:
@@ -39,9 +179,25 @@ def main() -> None:
rprint(f"Server: [dim]{Config.IMMICH_URL}[/dim]") rprint(f"Server: [dim]{Config.IMMICH_URL}[/dim]")
rprint(f"Output: [dim]{Config.OUTPUT_DIR}[/dim]") rprint(f"Output: [dim]{Config.OUTPUT_DIR}[/dim]")
# Handle RESET_PERSON before anything else # Handle RESET_PERSON before anything else.
# RESET_PERSON=* resets every tracked person; any other value resets
# that specific person by name.
reset_person_name = os.environ.get("RESET_PERSON", "").strip() reset_person_name = os.environ.get("RESET_PERSON", "").strip()
if reset_person_name: if reset_person_name:
if reset_person_name == "*":
names = list(get_person_summary().keys())
if "*" in names:
rprint(
"[yellow]Note: a person literally named '*' exists in the tracker "
"and will be reset along with everyone else.[/yellow]"
)
if names:
for name in names:
reset_person(name)
rprint(f"[bold yellow]Reset tracking data for all {len(names)} people.[/bold yellow]")
else:
rprint("[dim]No tracking data to reset.[/dim]")
else:
reset_person(reset_person_name) reset_person(reset_person_name)
rprint(f"[bold yellow]Reset tracking data for: {reset_person_name}[/bold yellow]") rprint(f"[bold yellow]Reset tracking data for: {reset_person_name}[/bold yellow]")
@@ -65,6 +221,8 @@ def main() -> None:
rprint("[bold red]Could not fetch people from Immich. Check URL/Key.[/bold red]") rprint("[bold red]Could not fetch people from Immich. Check URL/Key.[/bold red]")
return return
people = _handle_duplicate_people(people)
# Auto mode when no TTY (Docker, cron, pipes) — the primary use case. # Auto mode when no TTY (Docker, cron, pipes) — the primary use case.
# A TTY means local interactive use; AUTO_MODE=true overrides that for scripting. # A TTY means local interactive use; AUTO_MODE=true overrides that for scripting.
auto_mode = not sys.stdin.isatty() or os.environ.get("AUTO_MODE", "").lower() in ("true", "1", "yes") auto_mode = not sys.stdin.isatty() or os.environ.get("AUTO_MODE", "").lower() in ("true", "1", "yes")
+15 -8
View File
@@ -26,14 +26,17 @@ class _Config:
YEARS_FILTER: int = 10 YEARS_FILTER: int = 10
# Quality filtering # Quality filtering
MIN_FACE_WIDTH: int = 50 MIN_FACE_WIDTH: int = 90
BLUR_THRESHOLD: float = 100.0 BLUR_THRESHOLD: float = 120.0
MIN_CONFIDENCE: float = 0.7 MIN_CONFIDENCE: float = 0.7
MAX_AUTO_IMAGES: int = 80 MAX_AUTO_IMAGES: int = 20
QUALITY_REPLACEMENT: bool = True QUALITY_REPLACEMENT: bool = True
FRIGATE_SCORE_CEILING: float | None = None
ENABLE_FRIGATE_SCORES: bool = True
# People filtering # People filtering
MIN_FACE_COUNT: int = 0 MIN_FACE_COUNT: int = 3
MERGE_DUPLICATE_PEOPLE: bool = False
# Output quality # Output quality
FACE_MARGIN: float = 0.15 FACE_MARGIN: float = 0.15
@@ -56,12 +59,16 @@ class _Config:
self.API_KEY = os.getenv("API_KEY") self.API_KEY = os.getenv("API_KEY")
self.OUTPUT_DIR = os.getenv("OUTPUT_DIR", "./frigate_train") self.OUTPUT_DIR = os.getenv("OUTPUT_DIR", "./frigate_train")
self.YEARS_FILTER = int(os.getenv("YEARS_FILTER", "10")) self.YEARS_FILTER = int(os.getenv("YEARS_FILTER", "10"))
self.MIN_FACE_WIDTH = int(os.getenv("MIN_FACE_WIDTH", "50")) self.MIN_FACE_WIDTH = int(os.getenv("MIN_FACE_WIDTH", "90"))
self.MIN_FACE_COUNT = int(os.getenv("MIN_FACE_COUNT", "0")) self.MIN_FACE_COUNT = int(os.getenv("MIN_FACE_COUNT", "3"))
self.BLUR_THRESHOLD = float(os.getenv("BLUR_THRESHOLD", "100.0")) self.MERGE_DUPLICATE_PEOPLE = os.getenv("MERGE_DUPLICATE_PEOPLE", "false").lower() in ("true", "1", "yes")
self.BLUR_THRESHOLD = float(os.getenv("BLUR_THRESHOLD", "120.0"))
self.MIN_CONFIDENCE = float(os.getenv("MIN_CONFIDENCE", "0.7")) self.MIN_CONFIDENCE = float(os.getenv("MIN_CONFIDENCE", "0.7"))
self.MAX_AUTO_IMAGES = int(os.getenv("MAX_AUTO_IMAGES", "80")) self.MAX_AUTO_IMAGES = int(os.getenv("MAX_AUTO_IMAGES", "20"))
self.QUALITY_REPLACEMENT = os.getenv("QUALITY_REPLACEMENT", "true").lower() in ("true", "1", "yes") self.QUALITY_REPLACEMENT = os.getenv("QUALITY_REPLACEMENT", "true").lower() in ("true", "1", "yes")
_ceiling_env = os.getenv("FRIGATE_SCORE_CEILING", "").strip()
self.FRIGATE_SCORE_CEILING = float(_ceiling_env) if _ceiling_env else None
self.ENABLE_FRIGATE_SCORES = os.getenv("ENABLE_FRIGATE_SCORES", "true").lower() in ("true", "1", "yes")
self.FACE_MARGIN = float(os.getenv("FACE_MARGIN", "0.15")) self.FACE_MARGIN = float(os.getenv("FACE_MARGIN", "0.15"))
self.USE_FULL_RESOLUTION = os.getenv("USE_FULL_RESOLUTION", "true").lower() in ("true", "1", "yes") self.USE_FULL_RESOLUTION = os.getenv("USE_FULL_RESOLUTION", "true").lower() in ("true", "1", "yes")
self.ENABLE_FACE_ALIGNMENT = os.getenv("ENABLE_FACE_ALIGNMENT", "true").lower() in ("true", "1", "yes") self.ENABLE_FACE_ALIGNMENT = os.getenv("ENABLE_FACE_ALIGNMENT", "true").lower() in ("true", "1", "yes")
+91 -41
View File
@@ -5,7 +5,7 @@ Selection pipeline:
1. Concurrent thumbnail download 1. Concurrent thumbnail download
2. Quality filtering (blur, IR, exposure, confidence, face size) 2. Quality filtering (blur, IR, exposure, confidence, face size)
3. Face crop extraction (embed person's face, not full image) 3. Face crop extraction (embed person's face, not full image)
4. Embedding computation (InsightFace or SigLIP) 4. Embedding computation (InsightFace)
5. Cluster-aware selection (K-Medoids + FPS with hard example weighting) 5. Cluster-aware selection (K-Medoids + FPS with hard example weighting)
""" """
@@ -28,7 +28,6 @@ def select_diverse_assets(
limit: int | str, limit: int | str,
entity_name: str, entity_name: str,
selection_mode: str = "smart", selection_mode: str = "smart",
entity_type: str = "face",
person_id: str | None = None, person_id: str | None = None,
progress_callback=None, progress_callback=None,
) -> list: ) -> list:
@@ -38,9 +37,8 @@ def select_diverse_assets(
Args: Args:
assets: List of asset dicts from Immich API assets: List of asset dicts from Immich API
limit: Number to select, or "auto" for dynamic selection limit: Number to select, or "auto" for dynamic selection
entity_name: Name of the person/object for logging entity_name: Name of the person for logging
selection_mode: 'smart' (embedding-based) or 'time' (time spread) selection_mode: 'smart' (embedding-based) or 'time' (time spread)
entity_type: 'face' or 'object' - determines embedding model
progress_callback: Optional callback(current, total) for progress progress_callback: Optional callback(current, total) for progress
Returns: Returns:
@@ -53,14 +51,13 @@ def select_diverse_assets(
# Sort by creation time # Sort by creation time
assets = sorted(assets, key=lambda x: x.get("fileCreatedAt", "")) assets = sorted(assets, key=lambda x: x.get("fileCreatedAt", ""))
if selection_mode != "smart" or not is_embedding_available(entity_type): if selection_mode != "smart" or not is_embedding_available():
if selection_mode == "smart": if selection_mode == "smart":
model_name = "InsightFace" if entity_type == "face" else "SigLIP" logger.warning("InsightFace unavailable. Falling back to time spread.")
logger.warning(f"{model_name} unavailable. Falling back to time spread.")
return _select_time_spread(assets, limit) return _select_time_spread(assets, limit)
try: try:
return _select_by_embedding(assets, limit, entity_type, person_id, progress_callback) return _select_by_embedding(assets, limit, person_id, progress_callback)
except Exception as e: except Exception as e:
logger.error(f"Smart Diversity failed: {e}. Falling back to time spread.") logger.error(f"Smart Diversity failed: {e}. Falling back to time spread.")
return _select_time_spread(assets, limit) return _select_time_spread(assets, limit)
@@ -179,7 +176,6 @@ def _crop_face_from_thumbnail(
def _select_by_embedding( def _select_by_embedding(
assets: list, assets: list,
limit: int | str, limit: int | str,
entity_type: str,
person_id: str | None = None, person_id: str | None = None,
progress_callback=None, progress_callback=None,
) -> list: ) -> list:
@@ -188,7 +184,7 @@ def _select_by_embedding(
Pipeline: Pipeline:
1. Concurrent thumbnail download 1. Concurrent thumbnail download
2. Quality filtering 2. Quality filtering
3. Face crop extraction (face mode only) 3. Face crop extraction
4. Embedding computation 4. Embedding computation
5. Cluster-aware selection with hard example weighting 5. Cluster-aware selection with hard example weighting
""" """
@@ -243,7 +239,6 @@ def _select_by_embedding(
confidence = _get_face_confidence(asset, person_id=person_id) confidence = _get_face_confidence(asset, person_id=person_id)
if entity_type == "face":
face_bbox = _get_face_bbox(asset, person_id=person_id) face_bbox = _get_face_bbox(asset, person_id=person_id)
quality = assess_quality( quality = assess_quality(
img, img,
@@ -261,10 +256,8 @@ def _select_by_embedding(
asset["quality_score"] = quality.blur_score asset["quality_score"] = quality.blur_score
face_crop = _crop_face_from_thumbnail(img, asset, person_id=person_id) face_crop = _crop_face_from_thumbnail(img, asset, person_id=person_id)
embed_img = face_crop if face_crop is not None else img embed_img = face_crop if face_crop is not None else img
else:
embed_img = img
emb = get_embedding(embed_img, entity_type, asset_id=asset["id"]) emb = get_embedding(embed_img, asset_id=asset["id"])
if emb is not None: if emb is not None:
embeddings.append(emb) embeddings.append(emb)
valid_candidates.append(asset) valid_candidates.append(asset)
@@ -281,16 +274,86 @@ def _select_by_embedding(
logger.warning(f"Only {len(valid_candidates)} valid embeddings. Returning all.") logger.warning(f"Only {len(valid_candidates)} valid embeddings. Returning all.")
return valid_candidates return valid_candidates
# --- Phase 5: Cluster-aware selection --- # --- Phase 5: Near-duplicate removal ---
# Burst shots and repeated near-identical photos produce embeddings that are
# close but not identical, so FPS doesn't filter them out on its own.
# Greedily drop any candidate within DEDUP_THRESHOLD cosine distance of a
# higher-quality image already in the kept set.
embeddings, valid_candidates, confidence_scores = _dedup_embeddings(
embeddings, valid_candidates, confidence_scores
)
# Re-check after dedup: pool may have shrunk below limit
if limit != "auto" and len(valid_candidates) < limit:
logger.warning(f"Only {len(valid_candidates)} embeddings after near-duplicate removal. Returning all.")
return valid_candidates
# --- Phase 6: Cluster-aware selection ---
return _cluster_aware_selection( return _cluster_aware_selection(
embeddings, embeddings,
valid_candidates, valid_candidates,
limit, limit,
entity_type=entity_type,
confidence_scores=confidence_scores, confidence_scores=confidence_scores,
) )
# =============================================================================
# Near-Duplicate Removal
# =============================================================================
_DEDUP_THRESHOLD = 0.20 # cosine distance — burst shots ~0.01-0.05, same-event similar shots ~0.10-0.20
def _dedup_embeddings(
embeddings: list,
candidates: list,
confidence_scores: list,
) -> tuple[list, list, list]:
"""Greedy near-duplicate removal before clustering.
Sorts by quality score descending (best first), then for each candidate
drops it if any already-kept embedding is within _DEDUP_THRESHOLD cosine
distance. This eliminates burst-shot near-duplicates while preserving the
highest-quality representative from each near-identical group.
"""
if len(embeddings) < 2:
return embeddings, candidates, confidence_scores
emb_matrix = np.vstack(embeddings)
norms = np.linalg.norm(emb_matrix, axis=1, keepdims=True)
emb_normed = emb_matrix / np.maximum(norms, 1e-8)
# Sort by quality descending so the best image in each near-duplicate group wins.
# Use explicit None check so a legitimate quality_score=0.0 isn't treated as missing.
quality_scores = [qs if (qs := c.get("quality_score")) is not None else 0.0 for c in candidates]
order = sorted(range(len(candidates)), key=lambda i: quality_scores[i], reverse=True)
kept_indices = []
# Pre-allocate a max-size buffer and fill row-by-row — eliminates the O(K²)
# copy overhead from vstack-on-keep while keeping identical arithmetic.
kept_buf = np.empty((len(order), emb_normed.shape[1]), dtype=emb_normed.dtype)
n_kept = 0
for i in order:
if n_kept > 0:
sims = emb_normed[i] @ kept_buf[:n_kept].T
if np.any(sims > 1 - _DEDUP_THRESHOLD):
continue
kept_buf[n_kept] = emb_normed[i]
n_kept += 1
kept_indices.append(i)
dropped = len(embeddings) - len(kept_indices)
if dropped:
logger.info(f"Near-duplicate removal dropped {dropped} images (threshold {_DEDUP_THRESHOLD}).")
return (
[embeddings[i] for i in kept_indices],
[candidates[i] for i in kept_indices],
[confidence_scores[i] for i in kept_indices],
)
# ============================================================================= # =============================================================================
# K-Medoids (Lightweight Implementation) # K-Medoids (Lightweight Implementation)
# ============================================================================= # =============================================================================
@@ -322,7 +385,7 @@ def _kmedoids(dist_matrix: np.ndarray, k: int, max_iter: int = 50) -> tuple[list
# Iterative swap step # Iterative swap step
medoids = list(medoids) medoids = list(medoids)
labels = np.argmin(dist_matrix[:, medoids], axis=1) labels = np.argmin(dist_matrix[:, medoids], axis=1)
cost = sum(dist_matrix[i, medoids[labels[i]]] for i in range(n)) cost = dist_matrix[np.arange(n), np.array(medoids)[labels]].sum()
for _ in range(max_iter): for _ in range(max_iter):
improved = False improved = False
@@ -337,7 +400,7 @@ def _kmedoids(dist_matrix: np.ndarray, k: int, max_iter: int = 50) -> tuple[list
new_medoids = medoids.copy() new_medoids = medoids.copy()
new_medoids[m_idx] = cand new_medoids[m_idx] = cand
new_labels = np.argmin(dist_matrix[:, new_medoids], axis=1) new_labels = np.argmin(dist_matrix[:, new_medoids], axis=1)
new_cost = sum(dist_matrix[i, new_medoids[new_labels[i]]] for i in range(n)) new_cost = dist_matrix[np.arange(n), np.array(new_medoids)[new_labels]].sum()
if new_cost < cost: if new_cost < cost:
medoids = new_medoids medoids = new_medoids
labels = new_labels labels = new_labels
@@ -358,11 +421,11 @@ def _kmedoids(dist_matrix: np.ndarray, k: int, max_iter: int = 50) -> tuple[list
# ============================================================================= # =============================================================================
def _compute_adaptive_threshold(emb_normed: np.ndarray, entity_type: str) -> float: def _compute_adaptive_threshold(emb_normed: np.ndarray) -> float:
"""Compute adaptive FPS stop threshold based on actual embedding distribution. """Compute adaptive FPS stop threshold based on actual embedding distribution.
Instead of a hardcoded threshold, samples pairwise distances and sets Instead of a hardcoded threshold, samples pairwise distances and sets
the threshold as a fraction of the median pairwise distance. the threshold as 20% of the median pairwise distance.
""" """
n = len(emb_normed) n = len(emb_normed)
sample_size = min(200, n) sample_size = min(200, n)
@@ -370,20 +433,14 @@ def _compute_adaptive_threshold(emb_normed: np.ndarray, entity_type: str) -> flo
indices = rng.choice(n, sample_size, replace=False) if n > sample_size else np.arange(n) indices = rng.choice(n, sample_size, replace=False) if n > sample_size else np.arange(n)
sample = emb_normed[indices] sample = emb_normed[indices]
# Compute pairwise cosine distances for the sample
pairwise = 1 - sample @ sample.T pairwise = 1 - sample @ sample.T
upper_tri = pairwise[np.triu_indices(len(sample), k=1)] upper_tri = pairwise[np.triu_indices(len(sample), k=1)]
if len(upper_tri) == 0:
return 0.05
median_dist = float(np.median(upper_tri)) median_dist = float(np.median(upper_tri))
threshold = max(0.05, median_dist * 0.20)
# Faces: 20% of median (tighter — want fewer, more distinct images) logger.debug(f"Adaptive threshold: {threshold:.4f} (median_dist={median_dist:.4f})")
# Objects: 10% of median (wider — want more diversity)
fraction = 0.20 if entity_type == "face" else 0.10
threshold = max(0.05, median_dist * fraction)
logger.debug(
f"Adaptive threshold: {threshold:.4f} "
f"(median_dist={median_dist:.4f}, fraction={fraction}, type={entity_type})"
)
return threshold return threshold
@@ -391,7 +448,6 @@ def _cluster_aware_selection(
embeddings: list, embeddings: list,
candidates: list, candidates: list,
limit: int | str, limit: int | str,
entity_type: str = "face",
confidence_scores: list | None = None, confidence_scores: list | None = None,
) -> list: ) -> list:
"""Two-stage selection: K-Medoids clustering → FPS with hard example weighting. """Two-stage selection: K-Medoids clustering → FPS with hard example weighting.
@@ -411,17 +467,17 @@ def _cluster_aware_selection(
# Build confidence weight array for hard example boosting # Build confidence weight array for hard example boosting
conf_array = np.ones(n) conf_array = np.ones(n)
if confidence_scores and entity_type == "face": if confidence_scores:
for i, c in enumerate(confidence_scores): for i, c in enumerate(confidence_scores):
if c is not None: if c is not None:
conf_array[i] = c conf_array[i] = c
# Compute adaptive threshold for auto mode # Compute adaptive threshold for auto mode
auto_threshold = _compute_adaptive_threshold(emb_normed, entity_type) if limit == "auto" else 0.0 auto_threshold = _compute_adaptive_threshold(emb_normed) if limit == "auto" else 0.0
target = Config.MAX_AUTO_IMAGES if limit == "auto" else limit target = Config.MAX_AUTO_IMAGES if limit == "auto" else limit
# --- Stage 1: K-Medoids clustering --- # --- Stage 1: K-Medoids clustering ---
k = min(max(5, target // 4), n // 3, n) # e.g., 5-20 clusters k = min(max(5, target // 4), max(1, n // 3), n) # e.g., 1-20 clusters
logger.debug(f"Clustering {n} embeddings into {k} groups (K-Medoids)...") logger.debug(f"Clustering {n} embeddings into {k} groups (K-Medoids)...")
# Compute full cosine distance matrix # Compute full cosine distance matrix
@@ -469,15 +525,9 @@ def _cluster_aware_selection(
min_dists = np.minimum(min_dists, dists_to_new) min_dists = np.minimum(min_dists, dists_to_new)
min_dists[best_idx] = -np.inf min_dists[best_idx] = -np.inf
# Log hard example stats
if entity_type == "face":
selected_conf = [conf_array[i] for i in selected if conf_array[i] < 1.0] selected_conf = [conf_array[i] for i in selected if conf_array[i] < 1.0]
hard_count = sum(1 for c in selected_conf if c < 0.85) hard_count = sum(1 for c in selected_conf if c < 0.85)
logger.info( logger.info(f"Selection complete: {len(selected)} images ({hard_count} hard examples with confidence < 0.85).")
f"Selection complete: {len(selected)} images " f"({hard_count} hard examples with confidence < 0.85)."
)
else:
logger.info(f"Selection complete: {len(selected)} diverse images.")
return [candidates[i] for i in selected] return [candidates[i] for i in selected]
+13 -171
View File
@@ -1,8 +1,7 @@
""" """
Unified embedding interface for faces and objects. Embedding interface for face diversity selection.
- Faces: InsightFace (ArcFace/Buffalo_L) — or reuse from Immich - Faces: InsightFace (ArcFace/Buffalo_L) — or reuse from Immich
- Objects: SigLIP (Vision Transformer via transformers)
- Caching: Disk-based cache avoids recomputation on reruns - Caching: Disk-based cache avoids recomputation on reruns
""" """
@@ -42,12 +41,9 @@ def _suppress_output():
os.close(saved_err) os.close(saved_err)
# Lazy-loaded singletons # Lazy-loaded singleton
_insightface_app = None _insightface_app = None
_insightface_loaded = False _insightface_loaded = False
_siglip_model = None
_siglip_processor = None
_siglip_loaded = False
def _is_force_cpu() -> bool: def _is_force_cpu() -> bool:
@@ -202,169 +198,33 @@ def get_face_embedding(img_pil: Image.Image) -> np.ndarray | None:
# ============================================================================= # =============================================================================
# SigLIP (Objects) # Embedding Interface with Caching
# =============================================================================
def get_siglip_model():
"""Singleton for SigLIP model and processor with GPU auto-detection."""
global _siglip_model, _siglip_processor, _siglip_loaded
if _siglip_loaded:
return _siglip_model, _siglip_processor
_siglip_loaded = True
try:
import warnings
import torch
from transformers import AutoImageProcessor, SiglipVisionModel
model_name = "google/siglip-base-patch16-224"
# Disk cache check — path derived from model_name using HuggingFace's slug convention
hf_home = os.environ.get("HF_HOME", os.path.join(os.path.expanduser("~"), ".cache", "huggingface"))
cache_slug = "models--" + model_name.replace("/", "--")
model_cache = Path(hf_home) / "hub" / cache_slug
if model_cache.exists() and any(model_cache.iterdir()):
logger.info(f"SigLIP {model_name}: found in model cache")
else:
logger.info(f"SigLIP {model_name}: not cached — downloading now (~380 MB)")
logger.info(f"SigLIP {model_name}: loading into memory...")
t0 = time.time()
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=FutureWarning)
warnings.filterwarnings("ignore", message=".*use_fast.*")
_siglip_processor = AutoImageProcessor.from_pretrained(model_name, use_fast=True)
_siglip_model = SiglipVisionModel.from_pretrained(model_name)
_siglip_model.eval()
# Move to GPU if available (ROCm builds expose torch.cuda.is_available() == True)
if not _is_force_cpu():
if torch.cuda.is_available():
_siglip_model = _siglip_model.cuda()
device_name = "CUDA GPU"
elif hasattr(torch, "xpu") and torch.xpu.is_available():
_siglip_model = _siglip_model.to("xpu")
device_name = "Intel XPU"
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
_siglip_model = _siglip_model.to("mps")
device_name = "Apple MPS"
else:
device_name = "CPU"
else:
device_name = "CPU (FORCE_CPU)"
logger.info(f"SigLIP {model_name}: ready on {device_name} ({time.time() - t0:.1f}s)")
return _siglip_model, _siglip_processor
except ImportError as e:
logger.error(f"transformers/torch not installed: {e}")
return None, None
except Exception as e:
logger.error(f"Failed to load SigLIP: {e}")
return None, None
def get_object_embedding(img_pil: Image.Image) -> np.ndarray | None:
"""Get 768-dim SigLIP embedding for an image."""
model, processor = get_siglip_model()
if model is None:
return None
try:
import torch
inputs = processor(images=img_pil, return_tensors="pt")
device = next(model.parameters()).device
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
return outputs.pooler_output.squeeze().cpu().numpy()
except Exception as e:
logger.error(f"Error getting object embedding: {e}")
return None
def get_object_embeddings_batch(images: list[Image.Image]) -> list[np.ndarray | None]:
"""Get SigLIP embeddings for a batch of images (GPU-efficient)."""
model, processor = get_siglip_model()
if model is None:
return [None] * len(images)
try:
import torch
inputs = processor(images=images, return_tensors="pt", padding=True)
device = next(model.parameters()).device
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
embeddings = outputs.pooler_output.cpu().numpy()
return [embeddings[i] for i in range(len(embeddings))]
except Exception as e:
logger.error(f"Error in batch embedding: {e}")
# Fall back to individual computation
return [get_object_embedding(img) for img in images]
# =============================================================================
# Unified Interface with Caching
# ============================================================================= # =============================================================================
def get_embedding( def get_embedding(
img_pil: Image.Image, img_pil: Image.Image,
entity_type: str = "face",
asset_id: str | None = None, asset_id: str | None = None,
immich_embedding: np.ndarray | None = None,
) -> np.ndarray | None: ) -> np.ndarray | None:
"""Get embedding for an image based on entity type. """Get embedding for a face image.
Priority: Checks disk cache first (if enabled and asset_id provided),
1. Pre-fetched Immich embedding (if provided) then falls back to local InsightFace computation.
2. Disk cache (if enabled and asset_id provided)
3. Local model computation (InsightFace or SigLIP)
Args:
img_pil: The image to embed
entity_type: 'face' or 'object'
asset_id: Optional asset ID for cache lookup
immich_embedding: Optional pre-fetched embedding from Immich API
""" """
from .config import Config from .config import Config
use_cache = Config.ENABLE_CACHE and asset_id is not None use_cache = Config.ENABLE_CACHE and asset_id is not None
cache = get_cache(Config.CACHE_DIR) if use_cache else None cache = get_cache(Config.CACHE_DIR) if use_cache else None
# Use a single consistent cache key per model so lookups and stores always match.
# "immich" was previously used as the face key on the lookup path but "insightface"
# on the store path — meaning the cache was never hit for locally-computed embeddings.
cache_key = "insightface" if entity_type == "face" else "siglip"
# 1. Use Immich embedding if provided
if immich_embedding is not None:
if cache: if cache:
cache.put(asset_id, immich_embedding, cache_key) cached = cache.get(asset_id, "insightface")
return immich_embedding
# 2. Check disk cache
if cache:
cached = cache.get(asset_id, cache_key)
if cached is not None: if cached is not None:
return cached return cached
# 3. Compute locally
if entity_type == "face":
emb = get_face_embedding(img_pil) emb = get_face_embedding(img_pil)
else:
emb = get_object_embedding(img_pil)
if emb is not None and cache: if emb is not None and cache:
cache.put(asset_id, emb, cache_key) cache.put(asset_id, emb, "insightface")
return emb return emb
@@ -378,36 +238,18 @@ def _is_module_available(module_name: str) -> bool:
return False return False
def is_embedding_available(entity_type: str = "face", *, load: bool = False) -> bool: def is_embedding_available(*, load: bool = False) -> bool:
"""Check if embedding model is available for the given entity type. """Check if InsightFace is available.
By default this performs a lightweight import-check only (no model loading). By default this performs a lightweight import-check only (no model loading).
Pass ``load=True`` to actually load the model (expensive, hundreds of MB). Pass ``load=True`` to actually load the model (expensive, ~300 MB).
Args:
entity_type: 'face' or 'object'
load: If True, fully load the model to verify. If False (default),
only check that the required packages are importable.
""" """
if load: if load:
if entity_type == "face":
return get_insightface_app() is not None return get_insightface_app() is not None
model, _ = get_siglip_model()
return model is not None
# Lightweight check: just verify the packages are importable
if entity_type == "face":
return _is_module_available("insightface") and _is_module_available("onnxruntime") return _is_module_available("insightface") and _is_module_available("onnxruntime")
return _is_module_available("transformers") and _is_module_available("torch")
def load_embedding_model(entity_type: str = "face") -> bool: def load_embedding_model() -> bool:
"""Explicitly load the embedding model for the given entity type. """Explicitly load InsightFace. Returns True if the model loaded successfully."""
Returns True if the model loaded successfully.
"""
if entity_type == "face":
return get_insightface_app() is not None return get_insightface_app() is not None
model, _ = get_siglip_model()
return model is not None
+217 -80
View File
@@ -13,24 +13,47 @@ from rich import print as rprint
from rich.progress import BarColumn, Progress, SpinnerColumn, TaskProgressColumn, TextColumn from rich.progress import BarColumn, Progress, SpinnerColumn, TaskProgressColumn, TextColumn
from .config import Config, get_headers from .config import Config, get_headers
from .frigate_api import delete_frigate_person_files, get_frigate_person_files from .frigate_api import (
from .image_processing import process_face_mode, process_full_mode, process_object_mode delete_frigate_person_files,
get_all_frigate_person_files,
get_frigate_person_files,
recognize_face,
)
from .image_processing import process_face_mode
from .immich_api import fetch_face_data, fetch_full_image from .immich_api import fetch_face_data, fetch_full_image
from .log_config import console from .log_config import console
from .quality import assess_quality from .quality import assess_quality
from .upload_tracker import ( from .upload_tracker import (
get_lowest_quality_mapped_file, get_lowest_quality_mapped_file,
get_most_redundant_mapped_file,
get_tracked_frigate_file_count, get_tracked_frigate_file_count,
get_tracked_frigate_filenames, get_tracked_frigate_filenames,
has_frigate_scores,
mark_rejected, mark_rejected,
mark_uploaded, mark_uploaded,
record_frigate_file, record_frigate_files_batch,
remove_frigate_file, remove_frigate_file,
) )
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def _safe_person_dir(output_dir: str, person_name: str) -> str:
"""Return the output subdirectory for a person, raising ValueError on path traversal.
os.path.join silently discards output_dir when person_name is absolute,
and '../..' sequences resolve outside the tree. Both are rejected here.
"""
candidate = os.path.realpath(os.path.join(output_dir, person_name))
base = os.path.realpath(output_dir)
# Use the base path as its own prefix when it's the filesystem root ("/"),
# otherwise append os.sep — avoids the false "//" double-slash when base == "/".
base_prefix = base if base == os.sep else base + os.sep
if not candidate.startswith(base_prefix) and candidate != base:
raise ValueError(f"Person name {person_name!r} escapes output directory — skipping")
return candidate
def _reconcile_frigate_mappings( def _reconcile_frigate_mappings(
person_name: str, person_name: str,
known_files_before: set[str], known_files_before: set[str],
@@ -77,10 +100,12 @@ def _reconcile_frigate_mappings(
except (ValueError, IndexError): except (ValueError, IndexError):
return 0.0 return 0.0
for (fname, asset_id), frigate_file in zip(uploaded, sorted(new_files, key=_ts)): mappings = {
if asset_id: frigate_file: asset_id
record_frigate_file(person_name, frigate_file, asset_id) for (_, asset_id), frigate_file in zip(uploaded, sorted(new_files, key=_ts))
logger.debug(f"{person_name}: batch-mapped {target} Frigate file(s)") if asset_id
}
record_frigate_files_batch(person_name, mappings)
elif len(new_files) > target: elif len(new_files) > target:
logger.info( logger.info(
f"{person_name}: {len(new_files)} new Frigate files for {target} uploads" f"{person_name}: {len(new_files)} new Frigate files for {target} uploads"
@@ -148,6 +173,18 @@ def execute_jobs(jobs: list[dict]) -> None:
use_full_res = Config.USE_FULL_RESOLUTION use_full_res = Config.USE_FULL_RESOLUTION
# Load InsightFace app for landmark-based crop alignment.
# The model is already resident from the diversity/embedding phase, so this
# is just a singleton lookup — no load cost.
insightface_app = None
if Config.ENABLE_FACE_ALIGNMENT:
try:
from .embeddings import get_insightface_app
insightface_app = get_insightface_app()
except Exception as e:
logger.debug(f"InsightFace unavailable for crop alignment: {e}")
with Progress( with Progress(
SpinnerColumn(), SpinnerColumn(),
TextColumn("[progress.description]{task.description}"), TextColumn("[progress.description]{task.description}"),
@@ -159,28 +196,43 @@ def execute_jobs(jobs: list[dict]) -> None:
overall_task = progress.add_task("[green]Overall Progress", total=grand_total) overall_task = progress.add_task("[green]Overall Progress", total=grand_total)
for job in jobs: for job in jobs:
person, assets, config = job["person"], job["assets"], job["config"] person, assets = job["person"], job["assets"]
name, mode = person["name"], config.get("mode", "face") name = person["name"]
job_task = progress.add_task(f"Processing {name}...", total=len(assets)) job_task = progress.add_task(f"Processing {name}...", total=len(assets))
person_dir = os.path.join(Config.OUTPUT_DIR, name) try:
person_dir = _safe_person_dir(Config.OUTPUT_DIR, name)
except ValueError as e:
logger.error(str(e))
continue
# Face crops are transient (uploaded then discarded); wipe before each run. # Face crops are transient (uploaded then discarded); wipe before each run.
# Object crops are the deliverable; preserve them across runs. if os.path.isdir(person_dir):
if mode == "face" and os.path.isdir(person_dir):
shutil.rmtree(person_dir) shutil.rmtree(person_dir)
os.makedirs(person_dir, exist_ok=True) os.makedirs(person_dir, exist_ok=True)
# Track filename → asset_id and filename → confidence score # Track filename → asset_id, filename → confidence score, filename → crop dims
asset_map: dict[str, str] = {} asset_map: dict[str, str] = {}
score_map: dict[str, float | None] = {} score_map: dict[str, float | None] = {}
dims_map: dict[str, tuple[int, int]] = {}
count = 0 count = 0
for asset in assets: for asset in assets:
try: try:
# For face mode, enrich the asset with face bounding box data # Enrich the asset with face bounding box data from the Immich
# from the Immich faces API (not included in search/metadata results) # faces API (not included in search/metadata results).
if mode == "face":
asset = _enrich_asset_with_face_data(asset, person) asset = _enrich_asset_with_face_data(asset, person)
# Skip download if detection confidence already disqualifies
# the asset — avoids fetching a large image we'll discard.
conf = asset.get("face_confidence")
if conf is not None and conf < Config.MIN_CONFIDENCE:
progress.console.print(
f"[yellow]Skipped {asset['id']}"
f" (detection confidence {conf:.2f} < {Config.MIN_CONFIDENCE})[/yellow]"
)
mark_rejected(asset["id"], person_name=name)
progress.advance(job_task)
progress.advance(overall_task)
continue
# Use full-resolution for final output when configured # Use full-resolution for final output when configured
if use_full_res: if use_full_res:
@@ -196,24 +248,21 @@ def execute_jobs(jobs: list[dict]) -> None:
if img is None: if img is None:
progress.console.print(f"[red]Failed download {asset['id']}[/red]") progress.console.print(f"[red]Failed download {asset['id']}[/red]")
else: else:
saved = ( saved = process_face_mode(
process_face_mode(img, asset, person, person_dir, count) img, asset, person, person_dir, count, insightface_app=insightface_app
if mode == "face"
else process_object_mode(img, config, person_dir, count)
if mode == "object"
else process_full_mode(img, person_dir, count)
) )
if saved: if saved:
# Record which asset produced which output file
filename = f"{count}.jpg" filename = f"{count}.jpg"
asset_map[filename] = asset["id"] asset_map[filename] = asset["id"]
score_map[filename] = asset.get("quality_score") score_map[filename] = asset.get("quality_score")
if isinstance(saved, tuple):
dims_map[filename] = saved
# Time-spread path: compute blur score from the downloaded # Time-spread path: compute blur score from the downloaded
# image. Cap at 1440px so the scale matches the preview # image. Cap at 1440px so the scale matches the preview
# thumbnails the embedding path uses for scoring — Laplacian # thumbnails the embedding path uses for scoring — Laplacian
# variance grows with resolution, making full-res and # variance grows with resolution, making full-res and
# thumbnail scores incomparable if left uncapped. # thumbnail scores incomparable if left uncapped.
if mode == "face" and score_map[filename] is None: if score_map[filename] is None:
try: try:
score_img = img.convert("RGB") if img.mode != "RGB" else img score_img = img.convert("RGB") if img.mode != "RGB" else img
if score_img.width > 1440 or score_img.height > 1440: if score_img.width > 1440 or score_img.height > 1440:
@@ -223,12 +272,6 @@ def execute_jobs(jobs: list[dict]) -> None:
except Exception as exc: except Exception as exc:
logger.debug(f"Quality score fallback for {asset['id']}: {exc}") logger.debug(f"Quality score fallback for {asset['id']}: {exc}")
score_map[filename] = 0.0 # unknown quality — treat as lowest score_map[filename] = 0.0 # unknown quality — treat as lowest
# Also record object-mode variant filenames
if mode == "object":
for f in sorted(os.listdir(person_dir)):
if f.startswith(f"{count}_") and f not in asset_map:
asset_map[f] = asset["id"]
score_map[f] = asset.get("face_confidence")
count += 1 count += 1
else: else:
@@ -244,6 +287,7 @@ def execute_jobs(jobs: list[dict]) -> None:
# Store maps on the job so upload_to_frigate can use them # Store maps on the job so upload_to_frigate can use them
job["asset_map"] = asset_map job["asset_map"] = asset_map
job["score_map"] = score_map job["score_map"] = score_map
job["dims_map"] = dims_map
progress.remove_task(job_task) progress.remove_task(job_task)
@@ -255,25 +299,13 @@ def execute_jobs(jobs: list[dict]) -> None:
def upload_to_frigate(jobs: list[dict]) -> None: def upload_to_frigate(jobs: list[dict]) -> None:
"""Upload processed face crops to Frigate via API with detailed logging. """Upload processed face crops to Frigate via API with detailed logging.
Only runs for face-mode jobs. Object-mode crops are saved to the output
directory as the deliverable and must be copied to Frigate manually.
After each successful upload, records the Immich asset ID in the After each successful upload, records the Immich asset ID in the
upload tracker so it is skipped on future runs. upload tracker so it is skipped on future runs.
""" """
face_jobs = [j for j in jobs if j["config"].get("mode", "face") == "face"] if not jobs:
rprint("[dim]No jobs to upload.[/dim]")
if not face_jobs:
rprint("[dim]No face-mode jobs to upload.[/dim]")
return return
# Notify user about object-mode jobs that were skipped
object_jobs = [j for j in jobs if j["config"].get("mode") == "object"]
for job in object_jobs:
name = job["person"]["name"]
person_dir = os.path.join(Config.OUTPUT_DIR, name)
rprint(f" [dim]📁 {name} (object): crops saved to {person_dir} — copy to Frigate manually[/dim]")
frigate_url = os.environ.get("FRIGATE_URL", "") frigate_url = os.environ.get("FRIGATE_URL", "")
if not frigate_url: if not frigate_url:
rprint("[yellow]⚠️ FRIGATE_URL not set, skipping upload.[/yellow]") rprint("[yellow]⚠️ FRIGATE_URL not set, skipping upload.[/yellow]")
@@ -286,7 +318,7 @@ def upload_to_frigate(jobs: list[dict]) -> None:
# from the asset_map stored on each job during execute_jobs() # from the asset_map stored on each job during execute_jobs()
filename_to_asset_id: dict[str, dict[str, str]] = {} filename_to_asset_id: dict[str, dict[str, str]] = {}
total_files = 0 total_files = 0
for job in face_jobs: for job in jobs:
name = job["person"]["name"] name = job["person"]["name"]
asset_map = job.get("asset_map", {}) asset_map = job.get("asset_map", {})
filename_to_asset_id[name] = asset_map filename_to_asset_id[name] = asset_map
@@ -296,11 +328,15 @@ def upload_to_frigate(jobs: list[dict]) -> None:
rprint(" [yellow]No images found to upload.[/yellow]") rprint(" [yellow]No images found to upload.[/yellow]")
return return
rprint(f" People: [bold]{len(face_jobs)}[/bold], Total images: [bold]{total_files}[/bold]") rprint(f" People: [bold]{len(jobs)}[/bold], Total images: [bold]{total_files}[/bold]")
uploaded, failed = 0, 0 uploaded, failed = 0, 0
max_retries = 2 max_retries = 2
# Fetch all Frigate training files once — avoids one GET /api/faces per person.
# Falls back to per-person calls inside the loop if this fetch fails.
all_frigate_files = get_all_frigate_person_files()
with Progress( with Progress(
SpinnerColumn(), SpinnerColumn(),
TextColumn("[progress.description]{task.description}"), TextColumn("[progress.description]{task.description}"),
@@ -310,20 +346,25 @@ def upload_to_frigate(jobs: list[dict]) -> None:
) as progress: ) as progress:
upload_task = progress.add_task("[green]Uploading to Frigate", total=total_files) upload_task = progress.add_task("[green]Uploading to Frigate", total=total_files)
for job in face_jobs: for job in jobs:
name = job["person"]["name"] name = job["person"]["name"]
# URL-encode the name for the API (handles spaces, special chars) # URL-encode the name for the API (handles spaces, special chars)
encoded_name = quote(name, safe="") encoded_name = quote(name, safe="")
if " " in name: if " " in name:
progress.console.print(f" ℹ️ URL-encoded name for Frigate API: '{name}' → '{encoded_name}'") progress.console.print(f" ℹ️ URL-encoded name for Frigate API: '{name}' → '{encoded_name}'")
person_dir = os.path.join(Config.OUTPUT_DIR, name) try:
person_dir = _safe_person_dir(Config.OUTPUT_DIR, name)
except ValueError as e:
logger.error(str(e))
continue
if not os.path.isdir(person_dir): if not os.path.isdir(person_dir):
progress.console.print(f" [dim]⏭️ {name}: no output directory, skipping[/dim]") progress.console.print(f" [dim]⏭️ {name}: no output directory, skipping[/dim]")
continue continue
asset_map = filename_to_asset_id.get(name, {}) asset_map = filename_to_asset_id.get(name, {})
score_map = job.get("score_map", {}) score_map = job.get("score_map", {})
dims_map = job.get("dims_map", {})
person_files = sorted(asset_map.keys()) person_files = sorted(asset_map.keys())
if not person_files: if not person_files:
@@ -337,7 +378,12 @@ def upload_to_frigate(jobs: list[dict]) -> None:
# Snapshot live Frigate files for post-upload reconciliation diff only. # Snapshot live Frigate files for post-upload reconciliation diff only.
# effective_count is sourced from the tracker (mapped files) so that # effective_count is sourced from the tracker (mapped files) so that
# manually-added Frigate files don't consume winnow's managed quota. # manually-added Frigate files don't consume winnow's managed quota.
_snapshot = get_frigate_person_files(name) # Replacement targets also come exclusively from the tracker, so manually
# added files are never selected for deletion — only winnow-uploaded ones.
_snapshot = (
all_frigate_files.get(name, []) if all_frigate_files is not None
else get_frigate_person_files(name)
)
if _snapshot is None: if _snapshot is None:
# Frigate GET is down; fall back to the tracker's mapped filenames # Frigate GET is down; fall back to the tracker's mapped filenames
# as the pre-upload baseline. reconciliation will still work unless # as the pre-upload baseline. reconciliation will still work unless
@@ -349,11 +395,28 @@ def upload_to_frigate(jobs: list[dict]) -> None:
known_frigate_files_at_start: set[str] = get_tracked_frigate_filenames(name) known_frigate_files_at_start: set[str] = get_tracked_frigate_filenames(name)
else: else:
known_frigate_files_at_start: set[str] = set(_snapshot) known_frigate_files_at_start: set[str] = set(_snapshot)
# Remove tracker mappings for files that no longer exist in Frigate
# (manually deleted, or cleaned up outside winnow). This corrects the
# effective_count so those slots are available for new uploads.
stale = get_tracked_frigate_filenames(name) - known_frigate_files_at_start
for stale_fn in stale:
remove_frigate_file(name, stale_fn)
if stale:
progress.console.print(
f" [dim]{name}: cleared {len(stale)} stale mapping(s)"
" (file(s) no longer in Frigate)[/dim]"
)
effective_count = get_tracked_frigate_file_count(name) effective_count = get_tracked_frigate_file_count(name)
pre_run_count = effective_count
quality_replacement = job.get("config", {}).get("quality_replacement", False) quality_replacement = job.get("config", {}).get("quality_replacement", False)
if Config.ENABLE_FRIGATE_SCORES and pre_run_count == 0:
progress.console.print(
f" [dim]{name}: first run — Frigate diversity scoring will apply from the next run[/dim]"
)
actually_uploaded: list[tuple[str, str | None]] = [] actually_uploaded: list[tuple[str, str | None]] = []
failed_deletes: set[str] = set() failed_deletes: set[str] = set()
min_quality_score_for_slot: float | None = None min_quality_score_for_slot: float | None = None
person_has_fscores: bool = has_frigate_scores(name)
for fname in person_files: for fname in person_files:
fpath = os.path.join(person_dir, fname) fpath = os.path.join(person_dir, fname)
@@ -373,38 +436,101 @@ def upload_to_frigate(jobs: list[dict]) -> None:
continue continue
at_cap = effective_count >= Config.MAX_AUTO_IMAGES at_cap = effective_count >= Config.MAX_AUTO_IMAGES
# Pre-upload Frigate score — clean measurement (image not yet in training set).
# Called for all below-cap uploads (seeds frigate_scores for future at-cap
# replacement) and for at-cap uploads when scores already exist. Skipped on
# the first run (pre_run_count == 0) since Frigate has no model yet.
# recognize_face returns (face_name, score); we only use the score when the
# best match is for the correct person. Mismatches (or "unknown") are treated
# as None so a wrong-person score never drives a ceiling skip or replacement.
# Frigate rebuilds its model asynchronously after any delete (clear + background
# thread), so the first recognize call after a deletion returns None — our code
# handles this conservatively by skipping that candidate until the next run.
pre_fscore: float | None = None
if Config.ENABLE_FRIGATE_SCORES and pre_run_count > 0:
if not at_cap or person_has_fscores:
_result = recognize_face(fpath)
if _result is not None and (_result[0] or "").casefold() == name.casefold():
pre_fscore = _result[1]
# Below-cap novelty gate: skip candidates already covered by the Frigate model,
# including conditions learned from manually-added images winnow can't track.
# pre_fscore is None on the first run (pre_run_count == 0 skips recognize_face
# above), so this block never fires on the first run without an extra guard.
if not at_cap and pre_fscore is not None:
_ceiling = Config.FRIGATE_SCORE_CEILING
if _ceiling is None:
# Dynamic default: bar = most-redundant tracked file's Frigate score.
# Falls back to uploading freely when no tracked scores exist yet.
_bar = get_most_redundant_mapped_file(name)
_skip = _bar is not None and pre_fscore > _bar[2]
_bar_str = f"most redundant tracked {_bar[2]:.2f}" if _bar else ""
elif _ceiling == 0.0:
_skip = False # explicitly disabled
_bar_str = ""
else:
_skip = pre_fscore > _ceiling
_bar_str = f"ceiling {_ceiling:.2f}"
if _skip:
progress.console.print(
f" [dim]⏭ {fname}: Frigate score {pre_fscore:.2f}"
f" > {_bar_str}, already covered[/dim]"
)
progress.advance(upload_task)
continue
if at_cap: if at_cap:
if not quality_replacement: if not quality_replacement:
progress.console.print(f" [dim]⏭ {fname}: at cap, quality replacement disabled[/dim]") progress.console.print(f" [dim]⏭ {fname}: at cap, quality replacement disabled[/dim]")
progress.advance(upload_task) progress.advance(upload_task)
continue continue
new_score = score_map.get(fname)
if new_score is None: using_fscore = person_has_fscores and Config.ENABLE_FRIGATE_SCORES
progress.console.print(f" [dim]⏭ {fname}: no confidence score, skipping replacement[/dim]") if using_fscore:
progress.advance(upload_task) candidate_score = pre_fscore
continue get_target = get_most_redundant_mapped_file
worst = get_lowest_quality_mapped_file(name, exclude=failed_deletes) score_label, better_note = "frigate", " (more novel)"
if worst is None or new_score <= worst[2]: no_score_msg = "Frigate recognize unavailable, skipping replacement"
worst_score_str = f"{worst[2]:.3f}" if worst is not None else "N/A"
progress.console.print(
f" [dim]⏭ {fname}: score {new_score:.3f} ≤ worst mapped"
f" {worst_score_str}, skipping[/dim]"
)
progress.advance(upload_task)
continue
# Delete the worst mapped file to make room for the better one
worst_frigate_file, _worst_asset_id, worst_score = worst
progress.console.print(
f" 🔄 {fname}: score {new_score:.3f} > {worst_score:.3f},"
f" replacing {worst_frigate_file}"
)
if delete_frigate_person_files(name, [worst_frigate_file]):
remove_frigate_file(name, worst_frigate_file)
effective_count -= 1
min_quality_score_for_slot = worst_score
else: else:
logger.warning(f"Failed to delete {worst_frigate_file} for {name}, skipping replacement") candidate_score = score_map.get(fname)
failed_deletes.add(worst_frigate_file) get_target = get_lowest_quality_mapped_file
score_label, better_note = "blur", ""
no_score_msg = "no quality score, skipping replacement"
if candidate_score is None:
progress.console.print(f" [dim]⏭ {fname}: {no_score_msg}[/dim]")
progress.advance(upload_task)
continue
target = get_target(name, exclude=failed_deletes)
not_better = target is None or (
candidate_score >= target[2] if using_fscore else candidate_score <= target[2]
)
if not_better:
target_str = f"{target[2]:.3f}" if target is not None else "N/A"
op = "<" if using_fscore else ">"
progress.console.print(
f" [dim]⏭ {fname}: {score_label} {candidate_score:.3f}"
f" not {op} {target_str}, skipping[/dim]"
)
progress.advance(upload_task)
continue
target_frigate_file, _target_asset_id, target_score = target
op = "<" if using_fscore else ">"
progress.console.print(
f" 🔄 {fname}: {score_label} {candidate_score:.3f} {op} {target_score:.3f},"
f" replacing {target_frigate_file}{better_note}"
)
if delete_frigate_person_files(name, [target_frigate_file]):
remove_frigate_file(name, target_frigate_file)
person_has_fscores = has_frigate_scores(name)
effective_count -= 1
min_quality_score_for_slot = None if using_fscore else candidate_score
else:
logger.warning(f"Failed to delete {target_frigate_file} for {name}, skipping replacement")
failed_deletes.add(target_frigate_file)
progress.advance(upload_task) progress.advance(upload_task)
continue continue
@@ -424,7 +550,15 @@ def upload_to_frigate(jobs: list[dict]) -> None:
asset_id = asset_map.get(fname) asset_id = asset_map.get(fname)
if asset_id: if asset_id:
mark_uploaded(asset_id, person_name=name, score=score_map.get(fname)) mark_uploaded(
asset_id,
person_name=name,
score=score_map.get(fname),
crop_dims=dims_map.get(fname),
frigate_score=pre_fscore,
)
if pre_fscore is not None:
person_has_fscores = True
actually_uploaded.append((fname, asset_id)) actually_uploaded.append((fname, asset_id))
break break
@@ -440,13 +574,16 @@ def upload_to_frigate(jobs: list[dict]) -> None:
progress.console.print( progress.console.print(
f" [red]✗ {fname}: HTTP {resp.status_code} (after {max_retries} attempts)[/red]" f" [red]✗ {fname}: HTTP {resp.status_code} (after {max_retries} attempts)[/red]"
) )
full_body = resp.text
try: try:
error_detail = resp.json().get("message", resp.text[:100]) error_detail = resp.json().get("message", full_body[:100])
progress.console.print(f" [dim]{error_detail}[/dim]")
except Exception: except Exception:
error_detail = resp.text[:100] error_detail = full_body[:100]
if resp.status_code == 400:
progress.console.print(f" [dim]{error_detail}[/dim]") progress.console.print(f" [dim]{error_detail}[/dim]")
if resp.status_code == 400 and "face" in error_detail.lower(): else:
logger.debug(f"{fname} HTTP {resp.status_code}: {error_detail}")
if resp.status_code == 400 and "face" in full_body.lower():
asset_id = asset_map.get(fname) asset_id = asset_map.get(fname)
if asset_id: if asset_id:
mark_rejected(asset_id, person_name=name) mark_rejected(asset_id, person_name=name)
+49 -5
View File
@@ -22,11 +22,11 @@ def _get_faces_data() -> dict | None:
return None return None
def get_frigate_face_counts() -> dict[str, int] | None: def get_all_frigate_person_files() -> dict[str, list[str]] | None:
"""Return {person_name: training_image_count} from Frigate's train directory. """Return {person_name: [filename, ...]} for every person in Frigate.
Returns None if FRIGATE_URL is not set or the API is unreachable, so callers Single call used to build per-person snapshots before the upload loop,
can distinguish "API unavailable" from "person has 0 images." avoiding one GET /api/faces per person. Returns None if unavailable.
""" """
data = _get_faces_data() data = _get_faces_data()
if data is None: if data is None:
@@ -34,12 +34,24 @@ def get_frigate_face_counts() -> dict[str, int] | None:
# Response: {person_name: [file, ...], "train": [...], ...} # Response: {person_name: [file, ...], "train": [...], ...}
# "train" is a flat pending list, not a person — skip it. # "train" is a flat pending list, not a person — skip it.
return { return {
name: len(files) name: files
for name, files in data.items() for name, files in data.items()
if name != "train" and isinstance(files, list) if name != "train" and isinstance(files, list)
} }
def get_frigate_face_counts() -> dict[str, int] | None:
"""Return {person_name: training_image_count} from Frigate's train directory.
Returns None if FRIGATE_URL is not set or the API is unreachable, so callers
can distinguish "API unavailable" from "person has 0 images."
"""
all_files = get_all_frigate_person_files()
if all_files is None:
return None
return {name: len(files) for name, files in all_files.items()}
def get_frigate_person_files(person_name: str) -> list[str] | None: def get_frigate_person_files(person_name: str) -> list[str] | None:
"""Return the list of training filenames for a person in Frigate. """Return the list of training filenames for a person in Frigate.
@@ -53,6 +65,38 @@ def get_frigate_person_files(person_name: str) -> list[str] | None:
return files if isinstance(files, list) else [] return files if isinstance(files, list) else []
def recognize_face(file_path: str) -> tuple[str | None, float] | None:
"""Submit an image to Frigate's recognize endpoint.
Returns (face_name, score) where face_name is the best-matching person
(may be "unknown" if below Frigate's confidence threshold) and score is
the sigmoid-mapped cosine similarity (0-1) against that person's mean
embedding.
Returns None if FRIGATE_URL is unset, the API is unreachable, no face is
detected, or face recognition is not enabled in Frigate.
"""
frigate_url = os.environ.get("FRIGATE_URL", "").rstrip("/")
if not frigate_url:
return None
try:
with open(file_path, "rb") as f:
resp = requests.post(
f"{frigate_url}/api/faces/recognize",
files={"file": (os.path.basename(file_path), f, "image/jpeg")},
timeout=15,
)
if not resp.ok:
return None
data = resp.json()
if data.get("success") and "score" in data:
return (data.get("face_name"), round(float(data["score"]), 4))
return None
except Exception as e:
logger.debug(f"Frigate recognize failed for {file_path}: {e}")
return None
def delete_frigate_person_files(person_name: str, filenames: list[str]) -> bool: def delete_frigate_person_files(person_name: str, filenames: list[str]) -> bool:
"""Delete specific training files for a person from Frigate. """Delete specific training files for a person from Frigate.
+51 -71
View File
@@ -1,7 +1,8 @@
"""Image processing functions for cropping faces and objects.""" """Image processing functions for cropping faces."""
import logging import logging
import os import os
import warnings
import numpy as np import numpy as np
from PIL import Image from PIL import Image
@@ -10,9 +11,6 @@ from .config import Config
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# Lazy singleton
_yolo_model = None
def _save_jpeg(img: Image.Image, path: str) -> None: def _save_jpeg(img: Image.Image, path: str) -> None:
if img.mode != "RGB": if img.mode != "RGB":
@@ -20,17 +18,6 @@ def _save_jpeg(img: Image.Image, path: str) -> None:
img.save(path, format="JPEG") img.save(path, format="JPEG")
def get_yolo_model():
"""Singleton for YOLO model."""
global _yolo_model
if _yolo_model is None:
from ultralytics import YOLO
logger.info("Loading YOLOv9c model...")
_yolo_model = YOLO("yolov9c.pt")
return _yolo_model
def align_face(img: Image.Image, landmarks: list[list[float]] | np.ndarray) -> Image.Image | None: def align_face(img: Image.Image, landmarks: list[list[float]] | np.ndarray) -> Image.Image | None:
"""Align face using 5-point landmarks to standard ArcFace input format (112x112). """Align face using 5-point landmarks to standard ArcFace input format (112x112).
@@ -69,12 +56,15 @@ def process_face_mode(
output_dir: str, output_dir: str,
count: int, count: int,
min_width: int | None = None, min_width: int | None = None,
) -> bool: insightface_app=None,
) -> tuple[int, int] | None:
"""Crop face based on Immich metadata and save to output directory. """Crop face based on Immich metadata and save to output directory.
If face alignment is enabled and landmarks are available, produces Returns (width, height) of the saved crop, or None if no crop was saved.
an aligned 112x112 crop. Otherwise falls back to bounding box crop When insightface_app is provided and ENABLE_FACE_ALIGNMENT is True,
with configurable margin. re-detects the face in the Immich bbox region using InsightFace to get
precise landmarks for a proper 112x112 aligned crop. Falls back to
bounding box crop with configurable margin if alignment is unavailable.
""" """
min_width = min_width or Config.MIN_FACE_WIDTH min_width = min_width or Config.MIN_FACE_WIDTH
@@ -90,7 +80,7 @@ def process_face_mode(
if not face_info: if not face_info:
logger.debug(f"No face info for {person.get('name')} in asset {asset.get('id')}") logger.debug(f"No face info for {person.get('name')} in asset {asset.get('id')}")
return False return None
img_w, img_h = img.size img_w, img_h = img.size
meta_w = face_info.get("imageWidth") or img_w meta_w = face_info.get("imageWidth") or img_w
@@ -106,20 +96,55 @@ def process_face_mode(
face_w, face_h = x2 - x1, y2 - y1 face_w, face_h = x2 - x1, y2 - y1
if face_w < min_width or face_h < min_width: if face_w < min_width or face_h < min_width:
logger.debug(f"Face too small ({face_w:.1f}x{face_h:.1f})") logger.debug(f"Face too small ({face_w:.1f}x{face_h:.1f})")
return False return None
# Try face alignment if enabled and landmarks available # Re-detect face with InsightFace for landmark-based alignment.
# Immich's /api/faces endpoint does not include landmarks, so the
# align_face fallback below never fires without this step.
if insightface_app is not None and Config.ENABLE_FACE_ALIGNMENT:
try:
# Expand the Immich bbox by 50% to give InsightFace enough context
# for detection and alignment, then search for the face nearest the
# centre of that region (handles group photos at the boundary).
pad_x, pad_y = face_w * 0.5, face_h * 0.5
search_box = (
max(0, x1 - pad_x),
max(0, y1 - pad_y),
min(img_w, x2 + pad_x),
min(img_h, y2 + pad_y),
)
search_crop = img.crop(search_box)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", message=".*estimate.*is deprecated", category=FutureWarning)
detected = insightface_app.get(np.asarray(search_crop))
if detected:
cx, cy = search_crop.width / 2, search_crop.height / 2
best = min(
detected,
key=lambda f: abs((f.bbox[0] + f.bbox[2]) / 2 - cx)
+ abs((f.bbox[1] + f.bbox[3]) / 2 - cy),
)
kps = getattr(best, "kps", None)
if kps is not None and np.asarray(kps).shape == (5, 2):
aligned = align_face(search_crop, kps)
if aligned is not None:
_save_jpeg(aligned, os.path.join(output_dir, f"{count}.jpg"))
return aligned.size
except Exception as e:
logger.debug(f"InsightFace re-detection failed for {asset.get('id')}: {e}")
# Landmark alignment from Immich metadata (Immich does not currently
# expose landmarks, so this path is a future-proofing fallback)
if Config.ENABLE_FACE_ALIGNMENT: if Config.ENABLE_FACE_ALIGNMENT:
landmarks = face_info.get("landmarks") or face_info.get("landmark") landmarks = face_info.get("landmarks") or face_info.get("landmark")
if landmarks: if landmarks:
# Scale landmarks
scaled_landmarks = [[lm[0] * scale_x, lm[1] * scale_y] for lm in landmarks] scaled_landmarks = [[lm[0] * scale_x, lm[1] * scale_y] for lm in landmarks]
aligned = align_face(img, scaled_landmarks) aligned = align_face(img, scaled_landmarks)
if aligned is not None: if aligned is not None:
_save_jpeg(aligned, os.path.join(output_dir, f"{count}.jpg")) _save_jpeg(aligned, os.path.join(output_dir, f"{count}.jpg"))
return True return aligned.size
# Fall back to bounding box crop with configurable margin # Final fallback: bounding box crop with configurable margin
margin = Config.FACE_MARGIN margin = Config.FACE_MARGIN
margin_x, margin_y = face_w * margin, face_h * margin margin_x, margin_y = face_w * margin, face_h * margin
crop_box = ( crop_box = (
@@ -131,52 +156,7 @@ def process_face_mode(
face_crop = img.crop(crop_box) face_crop = img.crop(crop_box)
_save_jpeg(face_crop, os.path.join(output_dir, f"{count}.jpg")) _save_jpeg(face_crop, os.path.join(output_dir, f"{count}.jpg"))
return True return face_crop.size
def process_object_mode(
img: Image.Image,
config: dict,
output_dir: str,
count: int,
) -> bool:
"""Detect and crop objects using YOLO."""
try:
model = get_yolo_model()
target_class = config.get("object_class", "dog")
import torch
if os.getenv("FORCE_CPU", "").lower() in ("true", "1", "yes"):
device = "cpu"
elif hasattr(torch, "xpu") and torch.xpu.is_available():
device = "xpu"
else:
device = None # YOLO auto-selects (CUDA/ROCm/CPU)
results = model(img, verbose=False, device=device)
found = False
class_idx = 0 # Sequential counter per target class (Issue #10)
for box in (box for r in results for box in r.boxes):
cls_id = int(box.cls[0])
conf = float(box.conf[0])
if 0 <= cls_id < len(model.names) and model.names[cls_id] == target_class and conf > 0.5:
x1, y1, x2, y2 = box.xyxy[0].tolist()
_save_jpeg(
img.crop((x1, y1, x2, y2)),
os.path.join(output_dir, f"{count}_{class_idx}.jpg"),
)
class_idx += 1
found = True
return found
except Exception as e:
logger.error(f"YOLO processing failed: {e}")
return False
def process_full_mode(img: Image.Image, output_dir: str, count: int) -> bool:
"""Save full image."""
_save_jpeg(img, os.path.join(output_dir, f"{count}.jpg"))
return True
+25 -13
View File
@@ -5,7 +5,6 @@ from dataclasses import dataclass
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from io import BytesIO from io import BytesIO
import numpy as np
import requests import requests
from PIL import Image, ImageOps from PIL import Image, ImageOps
@@ -14,13 +13,13 @@ from .config import Config, get_headers
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
MAX_PAGES = 1000 # Safety limit for pagination MAX_PAGES = 1000 # Safety limit for pagination
_MAX_ASSETS_PER_PERSON = 5000 # Stop fetching after this many — diversity pool is capped at 3000 anyway
@dataclass @dataclass
class FaceData: class FaceData:
"""Pre-computed face data from Immich.""" """Pre-computed face data from Immich."""
embedding: np.ndarray | None
bbox: tuple[float, float, float, float] # (x1, y1, x2, y2) bbox: tuple[float, float, float, float] # (x1, y1, x2, y2)
confidence: float | None confidence: float | None
image_width: int image_width: int
@@ -45,6 +44,26 @@ def get_people() -> list[dict]:
return [] return []
def merge_people(survivor_id: str, merge_ids: list[str]) -> bool:
"""Merge duplicate people into survivor via Immich's merge endpoint.
The survivor (identified by survivor_id) absorbs all faces and assets
from the people in merge_ids, which are then removed from Immich.
"""
try:
resp = requests.put(
f"{Config.IMMICH_URL}/api/people/{survivor_id}/merge",
headers={**get_headers(), "Content-Type": "application/json"},
json={"ids": merge_ids},
timeout=30,
)
resp.raise_for_status()
return True
except requests.RequestException as e:
logger.error(f"Failed to merge people into {survivor_id}: {e}")
return False
def fetch_all_assets(person: dict) -> list[dict]: def fetch_all_assets(person: dict) -> list[dict]:
"""Fetch all assets for a person with pagination.""" """Fetch all assets for a person with pagination."""
name = person.get("name", "Unknown") name = person.get("name", "Unknown")
@@ -75,10 +94,10 @@ def fetch_all_assets(person: dict) -> list[dict]:
if not page_assets: if not page_assets:
break break
assets.extend(page_assets) assets.extend(a for a in page_assets if isinstance(a, dict))
logger.debug(f"Fetched page {page}, total: {len(assets)}") logger.debug(f"Fetched page {page}, total: {len(assets)}")
if len(page_assets) < page_size: if len(page_assets) < page_size or len(assets) >= _MAX_ASSETS_PER_PERSON:
break break
except (requests.RequestException, ValueError) as e: except (requests.RequestException, ValueError) as e:
@@ -89,7 +108,7 @@ def fetch_all_assets(person: dict) -> list[dict]:
def fetch_face_data(asset_id: str, person_id: str | None = None) -> FaceData | None: def fetch_face_data(asset_id: str, person_id: str | None = None) -> FaceData | None:
"""Fetch pre-computed face data (embedding, bbox, confidence) from Immich. """Fetch pre-computed face data (bbox, confidence) from Immich.
Queries GET /api/faces?id={asset_id} to retrieve face detection results Queries GET /api/faces?id={asset_id} to retrieve face detection results
that Immich already computed using InsightFace Buffalo_L. that Immich already computed using InsightFace Buffalo_L.
@@ -99,7 +118,7 @@ def fetch_face_data(asset_id: str, person_id: str | None = None) -> FaceData | N
person_id: Optional person ID to match the specific face person_id: Optional person ID to match the specific face
Returns: Returns:
FaceData with embedding, bbox, and confidence, or None if unavailable FaceData with bbox and confidence, or None if unavailable
""" """
try: try:
resp = requests.get( resp = requests.get(
@@ -127,12 +146,6 @@ def fetch_face_data(asset_id: str, person_id: str | None = None) -> FaceData | N
if face is None: if face is None:
face = faces[0] # Fall back to first/largest face face = faces[0] # Fall back to first/largest face
# Extract embedding if available
embedding = None
if "embedding" in face:
embedding = np.array(face["embedding"], dtype=np.float32)
# Extract bounding box
bbox = ( bbox = (
face.get("boundingBoxX1", 0), face.get("boundingBoxX1", 0),
face.get("boundingBoxY1", 0), face.get("boundingBoxY1", 0),
@@ -142,7 +155,6 @@ def fetch_face_data(asset_id: str, person_id: str | None = None) -> FaceData | N
score = face.get("score") score = face.get("score")
return FaceData( return FaceData(
embedding=embedding,
bbox=bbox, bbox=bbox,
confidence=score if score is not None else face.get("confidence"), confidence=score if score is not None else face.get("confidence"),
image_width=face.get("imageWidth", 0), image_width=face.get("imageWidth", 0),
+19 -47
View File
@@ -20,18 +20,16 @@ logger = logging.getLogger(__name__)
# Strategy presets: (limit, mode_name) # Strategy presets: (limit, mode_name)
STRATEGY_PRESETS = { STRATEGY_PRESETS = {
"1": ("auto", "Auto Diversity"), "1": ("auto", "Adaptive Diversity"),
"2": (30, "Standard (30)"), "2": (30, "Standard (30)"),
"3": (100, "Broad (100)"), "3": (100, "Broad (100)"),
} }
def _get_strategy_choice(has_embedding: bool, entity_type: str) -> tuple[int | str, str]: def _get_strategy_choice(has_embedding: bool) -> tuple[int | str, str]:
"""Prompt user for training strategy and return (limit, selection_mode).""" """Prompt user for training strategy and return (limit, selection_mode)."""
model_name = "InsightFace" if entity_type == "face" else "SigLIP"
if has_embedding: if has_embedding:
rprint(" [bold]1.[/bold] Auto (Objective Diversity) [green][Recommended][/green]") rprint(" [bold]1.[/bold] Adaptive Diversity [green][Recommended][/green]")
rprint(" [dim]• Dynamically selects images until redundancy starts[/dim]") rprint(" [dim]• Dynamically selects images until redundancy starts[/dim]")
rprint(" [bold]2.[/bold] Standard (30 images)") rprint(" [bold]2.[/bold] Standard (30 images)")
rprint(" [bold]3.[/bold] Broad (100 images)") rprint(" [bold]3.[/bold] Broad (100 images)")
@@ -51,7 +49,7 @@ def _get_strategy_choice(has_embedding: bool, entity_type: str) -> tuple[int | s
return 30, "smart" return 30, "smart"
# Fallback when embedding model not available # Fallback when embedding model not available
rprint(f" [yellow]Note: {model_name} not available. Using Time Spread.[/yellow]") rprint(" [yellow]Note: InsightFace not available. Using Time Spread.[/yellow]")
rprint(" [bold]1.[/bold] Standard (30 images) [green][Recommended][/green]") rprint(" [bold]1.[/bold] Standard (30 images) [green][Recommended][/green]")
rprint(" [bold]2.[/bold] Broad (100 images)") rprint(" [bold]2.[/bold] Broad (100 images)")
rprint(" [bold]3.[/bold] Custom Count") rprint(" [bold]3.[/bold] Custom Count")
@@ -77,7 +75,8 @@ def _resolve_strategy(strategy: str, has_embedding: bool) -> tuple[int | str, st
return int(custom_limit), "smart" return int(custom_limit), "smart"
strategy_map = { strategy_map = {
"auto": ("auto", "smart"), "adaptive": ("auto", "smart"),
"auto": ("auto", "smart"), # legacy alias for adaptive
"standard": (30, "smart"), "standard": (30, "smart"),
"broad": (100, "smart"), "broad": (100, "smart"),
} }
@@ -85,15 +84,13 @@ def _resolve_strategy(strategy: str, has_embedding: bool) -> tuple[int | str, st
def _perform_selection( def _perform_selection(
assets: list, limit: int | str, name: str, selection_mode: str, entity_type: str, person_id: str | None = None assets: list, limit: int | str, name: str, selection_mode: str, person_id: str | None = None
) -> list: ) -> list:
"""Run diversity selection with progress display.""" """Run diversity selection with progress display."""
if selection_mode == "smart": if selection_mode == "smart":
model_display = "InsightFace (face embeddings)" if entity_type == "face" else "SigLIP (visual embeddings)" rprint("\n[cyan]Using InsightFace (face embeddings) for diversity analysis...[/cyan]")
rprint(f"\n[cyan]Using {model_display} for diversity analysis...[/cyan]")
# Pre-load model explicitly (separate from availability check) load_embedding_model()
load_embedding_model(entity_type)
with Progress( with Progress(
SpinnerColumn(), SpinnerColumn(),
@@ -108,7 +105,6 @@ def _perform_selection(
limit, limit,
name, name,
selection_mode=selection_mode, selection_mode=selection_mode,
entity_type=entity_type,
person_id=person_id, person_id=person_id,
progress_callback=lambda c, t: progress.update(task, completed=c, total=t), progress_callback=lambda c, t: progress.update(task, completed=c, total=t),
) )
@@ -119,9 +115,7 @@ def _perform_selection(
rprint(f"\n[cyan]Using time-spread selection for {limit} images...[/cyan]") rprint(f"\n[cyan]Using time-spread selection for {limit} images...[/cyan]")
with console.status(f"[bold]Selecting {limit} images evenly distributed over time...[/bold]"): with console.status(f"[bold]Selecting {limit} images evenly distributed over time...[/bold]"):
selected = select_diverse_assets( selected = select_diverse_assets(assets, limit, name, selection_mode="time", person_id=person_id)
assets, limit, name, selection_mode="time", entity_type=entity_type, person_id=person_id
)
rprint(f" [green]Selected {len(selected)} images using time spread.[/green]") rprint(f" [green]Selected {len(selected)} images using time spread.[/green]")
return selected return selected
@@ -131,22 +125,12 @@ def _configure_person(person: dict, people: list[dict]) -> dict | None:
name = person["name"] name = person["name"]
console.print(f"\nSelected: [bold green]{name}[/bold green]") console.print(f"\nSelected: [bold green]{name}[/bold green]")
# Select training mode config = {"name": name, "quality_replacement": Config.QUALITY_REPLACEMENT}
rprint("\n[bold cyan]Training Mode:[/bold cyan]")
rprint(" [bold]1.[/bold] Face (Frigate Face Recognition)")
rprint(" [bold]2.[/bold] Object (Frigate Object Classification)")
mode_choice = Prompt.ask("Choice", choices=["1", "2"], default="1")
entity_type = "face" if mode_choice == "1" else "object"
config = {"name": name, "mode": entity_type, "quality_replacement": Config.QUALITY_REPLACEMENT}
if entity_type == "object":
config["object_class"] = Prompt.ask("Enter Object Class (e.g. dog, cat, car)", default="dog")
# Fetch and filter assets # Fetch and filter assets
years = IntPrompt.ask("Filter images older than (years)", default=Config.YEARS_FILTER) years = IntPrompt.ask("Filter images older than (years)", default=Config.YEARS_FILTER)
console.print(f"Scanning for {name} ({entity_type})...") console.print(f"Scanning for {name}...")
with console.status("[bold green]Fetching assets...[/bold green]"): with console.status("[bold green]Fetching assets...[/bold green]"):
all_assets = fetch_all_assets(person) all_assets = fetch_all_assets(person)
recent_assets = filter_recent_assets(all_assets, years=years) recent_assets = filter_recent_assets(all_assets, years=years)
@@ -170,17 +154,15 @@ def _configure_person(person: dict, people: list[dict]) -> dict | None:
return None return None
# Strategy selection # Strategy selection
has_embedding = is_embedding_available(entity_type) has_embedding = is_embedding_available()
rprint(f"\n[bold cyan]Select Training Strategy for {name}:[/bold cyan]") rprint(f"\n[bold cyan]Select Training Strategy for {name}:[/bold cyan]")
limit, selection_mode = _get_strategy_choice(has_embedding, entity_type) limit, selection_mode = _get_strategy_choice(has_embedding)
if selection_mode == "skip": if selection_mode == "skip":
return None return None
# Perform selection # Perform selection
selected_assets = _perform_selection( selected_assets = _perform_selection(recent_assets, limit, name, selection_mode, person_id=person["id"])
recent_assets, limit, name, selection_mode, entity_type, person_id=person["id"]
)
rprint(f" [green]Queued {len(selected_assets)} images for {name}.[/green]") rprint(f" [green]Queued {len(selected_assets)} images for {name}.[/green]")
return {"person": person, "assets": selected_assets, "limit": len(selected_assets), "config": config} return {"person": person, "assets": selected_assets, "limit": len(selected_assets), "config": config}
@@ -230,7 +212,6 @@ def auto_configure(people: list[dict]) -> list[dict]:
rprint("[red]No people found with names in Immich.[/red]") rprint("[red]No people found with names in Immich.[/red]")
return [] return []
mode = os.environ.get("TRAINING_MODE", "face")
strategy = os.environ.get("STRATEGY", "auto") strategy = os.environ.get("STRATEGY", "auto")
skip = os.environ.get("SKIP_PEOPLE", "").split(",") if os.environ.get("SKIP_PEOPLE") else [] skip = os.environ.get("SKIP_PEOPLE", "").split(",") if os.environ.get("SKIP_PEOPLE") else []
only = os.environ.get("ONLY_PEOPLE", "").split(",") if os.environ.get("ONLY_PEOPLE") else [] only = os.environ.get("ONLY_PEOPLE", "").split(",") if os.environ.get("ONLY_PEOPLE") else []
@@ -259,11 +240,7 @@ def auto_configure(people: list[dict]) -> list[dict]:
jobs = [] jobs = []
for person in valid_people: for person in valid_people:
name = person["name"] name = person["name"]
entity_type = mode config = {"name": name}
config = {"name": name, "mode": entity_type}
if entity_type == "object":
config["object_class"] = os.environ.get("OBJECT_CLASS", "dog")
all_assets = fetch_all_assets(person) all_assets = fetch_all_assets(person)
recent_assets = filter_recent_assets(all_assets, years=Config.YEARS_FILTER) recent_assets = filter_recent_assets(all_assets, years=Config.YEARS_FILTER)
@@ -306,7 +283,7 @@ def auto_configure(people: list[dict]) -> list[dict]:
config["quality_replacement"] = quality_replacement_only or Config.QUALITY_REPLACEMENT config["quality_replacement"] = quality_replacement_only or Config.QUALITY_REPLACEMENT
has_embedding = is_embedding_available(entity_type) has_embedding = is_embedding_available()
limit, selection_mode = _resolve_strategy(strategy, has_embedding) limit, selection_mode = _resolve_strategy(strategy, has_embedding)
# Cap selection to remaining capacity (no cap when replacement-only — executor # Cap selection to remaining capacity (no cap when replacement-only — executor
@@ -322,9 +299,7 @@ def auto_configure(people: list[dict]) -> list[dict]:
if selection_mode == "skip": if selection_mode == "skip":
continue continue
selected_assets = _perform_selection( selected_assets = _perform_selection(recent_assets, limit, name, selection_mode, person_id=person["id"])
recent_assets, limit, name, selection_mode, entity_type, person_id=person["id"]
)
if auto_cap is not None: if auto_cap is not None:
selected_assets = selected_assets[:auto_cap] selected_assets = selected_assets[:auto_cap]
@@ -339,20 +314,17 @@ def _show_preview(jobs: list[dict]) -> None:
"""Show a summary table of all queued jobs before execution.""" """Show a summary table of all queued jobs before execution."""
table = Table(title="📋 Training Job Preview", show_header=True, header_style="bold cyan") table = Table(title="📋 Training Job Preview", show_header=True, header_style="bold cyan")
table.add_column("Person", style="bold") table.add_column("Person", style="bold")
table.add_column("Mode", style="dim")
table.add_column("Images", justify="right") table.add_column("Images", justify="right")
table.add_column("Date Range", style="dim") table.add_column("Date Range", style="dim")
for job in jobs: for job in jobs:
name = job["person"]["name"] name = job["person"]["name"]
mode = job["config"].get("mode", "face")
count = str(job["limit"]) count = str(job["limit"])
# Date range
dates = sorted(a.get("fileCreatedAt", "")[:10] for a in job["assets"] if a.get("fileCreatedAt")) dates = sorted(a.get("fileCreatedAt", "")[:10] for a in job["assets"] if a.get("fileCreatedAt"))
date_range = f"{dates[0]} → {dates[-1]}" if len(dates) >= 2 else (dates[0] if dates else "—") date_range = f"{dates[0]} → {dates[-1]}" if len(dates) >= 2 else (dates[0] if dates else "—")
table.add_row(name, mode, count, date_range) table.add_row(name, count, date_range)
console.print() console.print()
console.print(table) console.print(table)
-4
View File
@@ -13,12 +13,9 @@ console = Console()
NOISY_LOGGERS = ( NOISY_LOGGERS = (
"urllib3", "urllib3",
"PIL", "PIL",
"ultralytics",
"insightface", "insightface",
"onnxruntime", "onnxruntime",
"matplotlib", "matplotlib",
"transformers",
"torch",
) )
@@ -51,7 +48,6 @@ def setup_logging(verbose: bool = False) -> logging.Logger:
# Suppress Python warnings from ML libraries # Suppress Python warnings from ML libraries
warnings.filterwarnings("ignore", category=UserWarning, module="onnxruntime") warnings.filterwarnings("ignore", category=UserWarning, module="onnxruntime")
warnings.filterwarnings("ignore", category=FutureWarning, module="transformers")
return root return root
+169 -27
View File
@@ -13,23 +13,37 @@ by_person schema (frigate_uploaded_ids.json):
{ {
"asset_ids": ["immich-id-1", ...], # all assets we attempted to upload "asset_ids": ["immich-id-1", ...], # all assets we attempted to upload
"scores": {"immich-id-1": 450.3}, # Laplacian blur variance at upload time "scores": {"immich-id-1": 450.3}, # Laplacian blur variance at upload time
"frigate_scores": {"immich-id-1": 0.87}, # Frigate recognition confidence (0-1) pre-upload
"frigate_files": {"PersonName-123.webp": "immich-id-1"}, # Frigate filename → asset ID "frigate_files": {"PersonName-123.webp": "immich-id-1"}, # Frigate filename → asset ID
"crop_dims": {"immich-id-1": [640, 480]}, # crop pixel dimensions at upload time
"frigate_count": 42 # last known Frigate training image count "frigate_count": 42 # last known Frigate training image count
} }
frigate_scores stores pre-upload recognize scores (0-1 sigmoid-mapped cosine
similarity). High score = the existing training set already covers this face
condition well. Low score = a gap — novel/diverse for the training set.
frigate_files only contains files winnow uploaded — files added manually through frigate_files only contains files winnow uploaded — files added manually through
Frigate's UI are never mapped here and are never touched by quality replacement. Frigate's UI are never mapped here and are never touched by quality replacement.
""" """
import json import json
import logging import logging
import os
from pathlib import Path from pathlib import Path
from .frigate_api import delete_frigate_person_files
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
UPLOAD_TRACKER_FILE = "frigate_uploaded_ids.json" UPLOAD_TRACKER_FILE = "frigate_uploaded_ids.json"
REJECT_TRACKER_FILE = "frigate_rejected_ids.json" REJECT_TRACKER_FILE = "frigate_rejected_ids.json"
# Write-through in-memory cache keyed by the resolved file path.
# Reduces per-call JSON reads from O(calls) to O(1) after the first load.
# Keyed by full path so tests with isolated tmp dirs never share entries.
_cache: dict[str, dict] = {}
def _tracker_path(filename: str) -> Path: def _tracker_path(filename: str) -> Path:
try: try:
@@ -41,18 +55,23 @@ def _tracker_path(filename: str) -> Path:
def _load(filename: str) -> dict: def _load(filename: str) -> dict:
path = _tracker_path(filename) path = _tracker_path(filename)
if not path.exists(): key = str(path)
return {} if key in _cache:
return _cache[key]
data: dict = {}
if path.exists():
try: try:
with open(path) as f: with open(path) as f:
return json.load(f) data = json.load(f)
except (json.JSONDecodeError, OSError) as e: except (json.JSONDecodeError, OSError) as e:
logger.warning(f"Could not load tracker {filename}: {e}") logger.warning(f"Could not load tracker {filename}: {e}")
return {} _cache[key] = data
return data
def _save(filename: str, data: dict) -> None: def _save(filename: str, data: dict) -> None:
path = _tracker_path(filename) path = _tracker_path(filename)
_cache[str(path)] = data # keep cache consistent with what we write
path.parent.mkdir(parents=True, exist_ok=True) path.parent.mkdir(parents=True, exist_ok=True)
with open(path, "w") as f: with open(path, "w") as f:
json.dump(data, f, indent=2) json.dump(data, f, indent=2)
@@ -76,14 +95,23 @@ def _get_ids(entry: list | dict) -> list[str]:
def _migrate_entry(entry: list | dict) -> dict: def _migrate_entry(entry: list | dict) -> dict:
"""Ensure by_person entry is in the current dict format.""" """Ensure by_person entry is in the current dict format."""
if isinstance(entry, list): if isinstance(entry, list):
return {"asset_ids": sorted(entry), "scores": {}, "frigate_files": {}} return {"asset_ids": sorted(entry), "scores": {}, "frigate_scores": {}, "frigate_files": {}, "crop_dims": {}}
entry.setdefault("asset_ids", []) entry.setdefault("asset_ids", [])
entry.setdefault("scores", {}) entry.setdefault("scores", {})
entry.setdefault("frigate_scores", {})
entry.setdefault("frigate_files", {}) entry.setdefault("frigate_files", {})
entry.setdefault("crop_dims", {})
return entry return entry
def _mark(filename: str, asset_id: str, person_name: str | None, score: float | None = None) -> None: def _mark(
filename: str,
asset_id: str,
person_name: str | None,
score: float | None = None,
crop_dims: tuple[int, int] | None = None,
frigate_score: float | None = None,
) -> None:
data = _load(filename) data = _load(filename)
flat_key = _flat_key(filename) flat_key = _flat_key(filename)
flat = set(data.get(flat_key, [])) flat = set(data.get(flat_key, []))
@@ -97,6 +125,10 @@ def _mark(filename: str, asset_id: str, person_name: str | None, score: float |
entry["asset_ids"] = sorted(ids) entry["asset_ids"] = sorted(ids)
if score is not None: if score is not None:
entry["scores"][asset_id] = round(score, 4) entry["scores"][asset_id] = round(score, 4)
if crop_dims is not None:
entry["crop_dims"][asset_id] = [crop_dims[0], crop_dims[1]]
if frigate_score is not None:
entry["frigate_scores"][asset_id] = round(frigate_score, 4)
by_person[person_name] = entry by_person[person_name] = entry
_save(filename, data) _save(filename, data)
@@ -111,8 +143,14 @@ def load_rejected_ids() -> set[str]:
return _load_flat(REJECT_TRACKER_FILE) return _load_flat(REJECT_TRACKER_FILE)
def mark_uploaded(asset_id: str, person_name: str | None = None, score: float | None = None) -> None: def mark_uploaded(
_mark(UPLOAD_TRACKER_FILE, asset_id, person_name, score=score) asset_id: str,
person_name: str | None = None,
score: float | None = None,
crop_dims: tuple[int, int] | None = None,
frigate_score: float | None = None,
) -> None:
_mark(UPLOAD_TRACKER_FILE, asset_id, person_name, score=score, crop_dims=crop_dims, frigate_score=frigate_score)
logger.debug(f"Marked {asset_id} as uploaded ({person_name})") logger.debug(f"Marked {asset_id} as uploaded ({person_name})")
@@ -121,6 +159,7 @@ def mark_rejected(asset_id: str, person_name: str | None = None) -> None:
logger.debug(f"Marked {asset_id} as rejected ({person_name})") logger.debug(f"Marked {asset_id} as rejected ({person_name})")
def record_frigate_file(person_name: str, frigate_filename: str, asset_id: str) -> None: def record_frigate_file(person_name: str, frigate_filename: str, asset_id: str) -> None:
"""Record the mapping from a Frigate training filename to an Immich asset ID.""" """Record the mapping from a Frigate training filename to an Immich asset ID."""
data = _load(UPLOAD_TRACKER_FILE) data = _load(UPLOAD_TRACKER_FILE)
@@ -132,6 +171,19 @@ def record_frigate_file(person_name: str, frigate_filename: str, asset_id: str)
logger.debug(f"Mapped Frigate file {frigate_filename} → {asset_id} ({person_name})") logger.debug(f"Mapped Frigate file {frigate_filename} → {asset_id} ({person_name})")
def record_frigate_files_batch(person_name: str, mappings: dict[str, str]) -> None:
"""Record multiple Frigate filename → asset_id mappings in a single load/save."""
if not mappings:
return
data = _load(UPLOAD_TRACKER_FILE)
by_person = data.setdefault("by_person", {})
entry = _migrate_entry(by_person.get(person_name, {}))
entry["frigate_files"].update(mappings)
by_person[person_name] = entry
_save(UPLOAD_TRACKER_FILE, data)
logger.debug(f"Batch-mapped {len(mappings)} Frigate file(s) for {person_name}")
def remove_frigate_file(person_name: str, frigate_filename: str) -> None: def remove_frigate_file(person_name: str, frigate_filename: str) -> None:
"""Remove a Frigate filename from the mapping after it has been deleted. """Remove a Frigate filename from the mapping after it has been deleted.
@@ -141,7 +193,9 @@ def remove_frigate_file(person_name: str, frigate_filename: str) -> None:
data = _load(UPLOAD_TRACKER_FILE) data = _load(UPLOAD_TRACKER_FILE)
by_person = data.get("by_person", {}) by_person = data.get("by_person", {})
entry = _migrate_entry(by_person.get(person_name, {})) entry = _migrate_entry(by_person.get(person_name, {}))
entry["frigate_files"].pop(frigate_filename, None) asset_id = entry["frigate_files"].pop(frigate_filename, None)
if asset_id:
entry["frigate_scores"].pop(asset_id, None)
by_person[person_name] = entry by_person[person_name] = entry
_save(UPLOAD_TRACKER_FILE, data) _save(UPLOAD_TRACKER_FILE, data)
logger.debug(f"Removed Frigate file mapping {frigate_filename} ({person_name})") logger.debug(f"Removed Frigate file mapping {frigate_filename} ({person_name})")
@@ -169,27 +223,93 @@ def get_tracked_frigate_filenames(person_name: str) -> set[str]:
return set(entry["frigate_files"].keys()) return set(entry["frigate_files"].keys())
def has_frigate_scores(person_name: str) -> bool:
"""Return True if any mapped file for this person has a stored Frigate recognition score."""
data = _load(UPLOAD_TRACKER_FILE)
entry = _migrate_entry(data.get("by_person", {}).get(person_name, {}))
frigate_files = entry.get("frigate_files", {})
frigate_scores = entry.get("frigate_scores", {})
return any(asset_id in frigate_scores for asset_id in frigate_files.values())
def _pick_mapped_file(
person_name: str, score_key: str, *, highest: bool, exclude: set[str] | None = None
) -> tuple[str, str, float] | None:
data = _load(UPLOAD_TRACKER_FILE)
entry = _migrate_entry(data.get("by_person", {}).get(person_name, {}))
scores = entry.get(score_key, {})
candidates = [
(ff, asset_id, scores[asset_id])
for ff, asset_id in entry.get("frigate_files", {}).items()
if (exclude is None or ff not in exclude) and asset_id in scores
]
if not candidates:
return None
return max(candidates, key=lambda x: x[2]) if highest else min(candidates, key=lambda x: x[2])
def get_lowest_quality_mapped_file( def get_lowest_quality_mapped_file(
person_name: str, exclude: set[str] | None = None person_name: str, exclude: set[str] | None = None
) -> tuple[str, str, float] | None: ) -> tuple[str, str, float] | None:
"""Return (frigate_filename, asset_id, score) for the mapped file with the lowest """Return (frigate_filename, asset_id, score) for the mapped file with the lowest
quality score, or None if no mapped files with known scores exist. blur score, or None if no mapped files with known scores exist.
Pass `exclude` to skip files that failed to delete this run without removing Used for quality replacement when no Frigate scores are available.
them from the tracker — they remain candidates on the next run. Pass `exclude` to skip files that failed to delete this run.
""" """
return _pick_mapped_file(person_name, "scores", highest=False, exclude=exclude)
def get_most_redundant_mapped_file(
person_name: str, exclude: set[str] | None = None
) -> tuple[str, str, float] | None:
"""Return (frigate_filename, asset_id, score) for the mapped file with the highest
Frigate recognition score, or None if no mapped files with Frigate scores exist.
High Frigate score = the training set already covers this face condition well
= the most redundant file and therefore the best replacement target.
Pass `exclude` to skip files that failed to delete this run.
"""
return _pick_mapped_file(person_name, "frigate_scores", highest=True, exclude=exclude)
def get_frigate_filename_for_asset(person_name: str, asset_id: str) -> str | None:
"""Return the Frigate training filename mapped to this asset ID, or None."""
data = _load(UPLOAD_TRACKER_FILE) data = _load(UPLOAD_TRACKER_FILE)
entry = _migrate_entry(data.get("by_person", {}).get(person_name, {})) entry = _migrate_entry(data.get("by_person", {}).get(person_name, {}))
frigate_files = entry.get("frigate_files", {}) for frigate_filename, aid in entry["frigate_files"].items():
scores = entry.get("scores", {}) if aid == asset_id:
candidates = [ return frigate_filename
(frigate_filename, asset_id, scores[asset_id])
for frigate_filename, asset_id in frigate_files.items()
if asset_id in scores and (exclude is None or frigate_filename not in exclude)
]
if not candidates:
return None return None
return min(candidates, key=lambda x: x[2])
def find_by_crop_dimension(size: int) -> list[dict]:
"""Return all tracked crops whose width or height matches `size` pixels.
Returns a list of dicts: {person, asset_id, width, height, blur_score, frigate_filename}.
frigate_filename is None when the Frigate mapping was lost to a reconciliation race.
"""
data = _load(UPLOAD_TRACKER_FILE)
results = []
for person_name, raw_entry in data.get("by_person", {}).items():
entry = _migrate_entry(raw_entry)
scores = entry.get("scores", {})
frigate_files = entry.get("frigate_files", {})
asset_to_frigate = {v: k for k, v in frigate_files.items()}
frigate_scores = entry.get("frigate_scores", {})
for asset_id, dims in entry.get("crop_dims", {}).items():
w, h = dims[0], dims[1]
if w == size or h == size:
results.append({
"person": person_name,
"asset_id": asset_id,
"width": w,
"height": h,
"blur_score": scores.get(asset_id),
"frigate_score": frigate_scores.get(asset_id),
"frigate_filename": asset_to_frigate.get(asset_id),
})
return results
def update_frigate_count(person_name: str, count: int) -> None: def update_frigate_count(person_name: str, count: int) -> None:
@@ -203,19 +323,41 @@ def update_frigate_count(person_name: str, count: int) -> None:
def reset_person(person_name: str) -> None: def reset_person(person_name: str) -> None:
"""Remove all uploaded and rejected records for a given person.""" """Remove all uploaded and rejected records for a given person.
for filename in (UPLOAD_TRACKER_FILE, REJECT_TRACKER_FILE):
data = _load(filename) Also deletes winnow-managed Frigate training files so the next run starts
clean rather than uploading on top of orphaned files. Manually-added Frigate
files (not in frigate_files) are never touched. Proceeds with tracker reset
even if Frigate is unreachable.
"""
upload_data = _load(UPLOAD_TRACKER_FILE)
entry = _migrate_entry(upload_data.get("by_person", {}).get(person_name, {}))
frigate_filenames = list(entry.get("frigate_files", {}).keys())
if frigate_filenames:
if not os.environ.get("FRIGATE_URL", "").strip():
logger.info(f"FRIGATE_URL not set — skipping Frigate file deletion for {person_name}")
elif delete_frigate_person_files(person_name, frigate_filenames):
logger.info(f"Deleted {len(frigate_filenames)} Frigate file(s) for {person_name}")
else:
logger.warning(f"Could not delete Frigate files for {person_name} — tracker reset proceeding anyway")
changed = False
tracker_files = ((UPLOAD_TRACKER_FILE, upload_data), (REJECT_TRACKER_FILE, _load(REJECT_TRACKER_FILE)))
for filename, data in tracker_files:
flat_key = _flat_key(filename) flat_key = _flat_key(filename)
by_person = data.get("by_person", {}) by_person = data.get("by_person", {})
entry = by_person.pop(person_name, None) tracker_entry = by_person.pop(person_name, None)
if entry is not None: if tracker_entry is not None:
person_ids = set(_get_ids(entry)) person_ids = set(_get_ids(tracker_entry))
flat = set(data.get(flat_key, [])) - person_ids flat = set(data.get(flat_key, [])) - person_ids
data[flat_key] = sorted(flat) data[flat_key] = sorted(flat)
data["by_person"] = by_person data["by_person"] = by_person
_save(filename, data) _save(filename, data)
changed = True
if changed:
logger.info(f"Reset tracking data for {person_name}") logger.info(f"Reset tracking data for {person_name}")
else:
logger.debug(f"reset_person: no tracking data found for {person_name}")
def get_person_summary() -> dict[str, dict]: def get_person_summary() -> dict[str, dict]: