Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Issue #345: face scan in the activity stack

Andrey Vyrodov3f3c20498733
diff --git a/crates/engine/src/cmd/face_recognize.rs b/crates/engine/src/cmd/face_recognize.rs
index 9efdb146d..e099f13ec 100644
--- a/crates/engine/src/cmd/face_recognize.rs
+++ b/crates/engine/src/cmd/face_recognize.rs
@@ -515,6 +515,21 @@ mod imp {
}))
}
+ /// The scan's row in the activity stack: there while `pending` photos are left, its whole the most that were left at
+ /// once since the scan last finished (which it returns; 0 when none are left). It can't be stopped from there: the
+ /// scan is switched off in Settings.
+ fn track(s: &mut Session, pending: u64) -> u64 {
+ if pending == 0 {
+ s.faces.task = None;
+ s.faces.peak = 0;
+ return 0;
+ }
+ s.faces.peak = s.faces.peak.max(pending);
+ let task = s.faces.task.get_or_insert_with(|| s.activity.start("faces", "Finding faces", crate::activity::Cancel::No));
+ task.progress(s.faces.peak.saturating_sub(pending), s.faces.peak);
+ s.faces.peak
+ }
+
/// `faces.pump`: cheap to call every frame. Takes in what the background worker has finished and, when it is idle,
/// hands it the next photo with faces to embed. Does nothing (and says so) until recognition is switched on in
/// Settings with a model chosen.
@@ -528,14 +543,17 @@ mod imp {
finish_downloads(s);
let enabled = s.face_models_dir.clone().is_some_and(|d| enabled_cached(s, &d));
if !enabled {
+ track(s, 0);
return Ok(json!({"active": false}));
}
if s.faces.retry_at.is_some_and(|t| Instant::now() < t) {
+ track(s, 0);
return Ok(json!({"active": false}));
}
let Ok(embedder) = current_with(s, C, false) else {
// the chosen model is gone or broken: look again in a few seconds, not every frame
s.faces.retry_at = Some(Instant::now() + Duration::from_secs(3));
+ track(s, 0);
return Ok(json!({"active": false}));
};
s.faces.retry_at = None;
@@ -573,10 +591,13 @@ mod imp {
break;
}
}
+ let pending = s.faces.queue.len() + s.faces.in_flight.len();
+ let peak = track(s, pending as u64);
Ok(json!({
"active": true,
"inFlight": s.faces.in_flight.len(),
- "pendingPhotos": s.faces.queue.len() + s.faces.in_flight.len(),
+ "pendingPhotos": pending,
+ "peak": peak,
"indexedFaces": s.faces.index.len(),
"searchedPhotos": s.faces.scanned.len(),
"workers": target,
diff --git a/crates/engine/src/faces_index.rs b/crates/engine/src/faces_index.rs
index c250f8683..fd4d9f2e7 100644
--- a/crates/engine/src/faces_index.rs
+++ b/crates/engine/src/faces_index.rs
@@ -434,6 +434,10 @@ pub(crate) struct FacesState {
pub scanned: Scanned,
/// Photos that could not be searched this session (unreadable files): left alone until the next launch.
pub scan_failed: std::collections::HashSet<u64>,
+ /// The scan's row in the activity stack, while photos are left.
+ pub task: Option<crate::activity::TaskGuard>,
+ /// The most photos left at once since the scan last finished (the row's whole).
+ pub peak: u64,
}
impl FacesState {
diff --git a/crates/engine/src/tests_face_recognize.rs b/crates/engine/src/tests_face_recognize.rs
index 2fd53c679..8d14d3c61 100644
--- a/crates/engine/src/tests_face_recognize.rs
+++ b/crates/engine/src/tests_face_recognize.rs
@@ -487,3 +487,35 @@ fn every_scanned_face_is_shown_by_the_same_kind_of_box() {
assert!((v.x0 - tight.x0).abs() < 1e-3 && (v.y0 - tight.y0).abs() < 1e-3, "{v:?}");
let _ = std::fs::remove_dir_all(&d);
}
+
+#[test]
+fn face_scan_row_follows_pending_photos() {
+ let d = temp("activity");
+ let (mut s, _) = setup(&d, 64);
+ let (a, b) = two_photos(&s);
+ set_regions(&mut s, a, vec![region(0.1, None), region(0.5, None)]);
+ set_regions(&mut s, b, vec![region(0.3, None)]);
+ let faces_rows = |s: &Session| s.activity.list().into_iter().filter(|t| t.kind == "faces").collect::<Vec<_>>();
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);
+ let (mut last, mut shown) = (json!(null), false);
+ while std::time::Instant::now() < deadline {
+ last = s.execute("faces.pump", &json!({})).unwrap();
+ let rows = faces_rows(&s);
+ if last["pendingPhotos"].as_u64().unwrap_or(0) > 0 {
+ // while photos are left: one row, not cancellable, its whole the most photos left at once
+ assert_eq!(rows.len(), 1, "{last}");
+ assert_eq!((rows[0].label.as_str(), rows[0].cancellable), ("Finding faces", false));
+ assert_eq!(rows[0].total, last["peak"].as_u64().unwrap(), "{last}");
+ assert_eq!(rows[0].done, rows[0].total - last["pendingPhotos"].as_u64().unwrap());
+ shown = true;
+ }
+ if last["active"] == true && last["pendingPhotos"] == 0 && last["indexedFaces"] == 3 {
+ break;
+ }
+ std::thread::sleep(std::time::Duration::from_millis(50));
+ }
+ assert!(shown, "the scan never showed a row: {last}");
+ assert_eq!((last["pendingPhotos"].clone(), last["peak"].clone()), (json!(0), json!(0)), "{last}");
+ assert!(faces_rows(&s).is_empty(), "the row goes when the scan is done");
+ let _ = std::fs::remove_dir_all(&d);
+}
diff --git a/crates/ui-egui/locales/de.json b/crates/ui-egui/locales/de.json
index 1edf29adc..85800315f 100644
--- a/crates/ui-egui/locales/de.json
+++ b/crates/ui-egui/locales/de.json
@@ -2027,5 +2027,7 @@
"Discarding smart previews": "Smart-Vorschauen werden verworfen",
"Importing Lightroom catalog": "Lightroom-Katalog wird importiert",
"Reading Lightroom catalog": "Lightroom-Katalog wird gelesen",
- "Merging": "Zusammenfügen"
+ "Merging": "Zusammenfügen",
+ "Downloading model": "Modell wird heruntergeladen",
+ "Finding faces": "Gesichter werden gesucht"
}
diff --git a/crates/ui-egui/locales/es.json b/crates/ui-egui/locales/es.json
index 701c91bdb..457287d63 100644
--- a/crates/ui-egui/locales/es.json
+++ b/crates/ui-egui/locales/es.json
@@ -1872,5 +1872,7 @@
"Discarding smart previews": "Descartando vistas previas inteligentes",
"Importing Lightroom catalog": "Importando catálogo de Lightroom",
"Reading Lightroom catalog": "Leyendo catálogo de Lightroom",
- "Merging": "Combinando"
+ "Merging": "Combinando",
+ "Downloading model": "Descargando modelo",
+ "Finding faces": "Buscando caras"
}
diff --git a/crates/ui-egui/locales/ja.json b/crates/ui-egui/locales/ja.json
index 520452a8d..036a222fa 100644
--- a/crates/ui-egui/locales/ja.json
+++ b/crates/ui-egui/locales/ja.json
@@ -1874,5 +1874,7 @@
"Discarding smart previews": "スマートプレビューを破棄中",
"Importing Lightroom catalog": "Lightroom カタログを読み込み中",
"Reading Lightroom catalog": "Lightroom カタログを読み取り中",
- "Merging": "合成中"
+ "Merging": "合成中",
+ "Downloading model": "モデルをダウンロード中",
+ "Finding faces": "顔を検索中"
}
diff --git a/crates/ui-egui/locales/pt-br.json b/crates/ui-egui/locales/pt-br.json
index ad269f963..112a5e8a4 100644
--- a/crates/ui-egui/locales/pt-br.json
+++ b/crates/ui-egui/locales/pt-br.json
@@ -1872,5 +1872,7 @@
"Discarding smart previews": "Descartando prévias inteligentes",
"Importing Lightroom catalog": "Importando catálogo do Lightroom",
"Reading Lightroom catalog": "Lendo catálogo do Lightroom",
- "Merging": "Mesclando"
+ "Merging": "Mesclando",
+ "Downloading model": "Baixando modelo",
+ "Finding faces": "Procurando rostos"
}
diff --git a/crates/ui-egui/locales/ru.json b/crates/ui-egui/locales/ru.json
index 6a4e4d528..f350df403 100644
--- a/crates/ui-egui/locales/ru.json
+++ b/crates/ui-egui/locales/ru.json
@@ -1585,5 +1585,7 @@
"Discarding smart previews": "Удаление смарт-просмотров",
"Importing Lightroom catalog": "Импорт каталога Lightroom",
"Reading Lightroom catalog": "Чтение каталога Lightroom",
- "Merging": "Слияние"
+ "Merging": "Слияние",
+ "Downloading model": "Загрузка модели",
+ "Finding faces": "Поиск лиц"
}
diff --git a/crates/ui-egui/locales/zh-hans.json b/crates/ui-egui/locales/zh-hans.json
index 24a47421c..578637f03 100644
--- a/crates/ui-egui/locales/zh-hans.json
+++ b/crates/ui-egui/locales/zh-hans.json
@@ -1872,5 +1872,7 @@
"Discarding smart previews": "正在丢弃智能预览",
"Importing Lightroom catalog": "正在导入 Lightroom 目录",
"Reading Lightroom catalog": "正在读取 Lightroom 目录",
- "Merging": "正在合并"
+ "Merging": "正在合并",
+ "Downloading model": "正在下载模型",
+ "Finding faces": "正在查找人脸"
}
diff --git a/crates/ui-egui/locales/zh-hant.json b/crates/ui-egui/locales/zh-hant.json
index caf5f9bca..09a648c7a 100644
--- a/crates/ui-egui/locales/zh-hant.json
+++ b/crates/ui-egui/locales/zh-hant.json
@@ -1872,5 +1872,7 @@
"Discarding smart previews": "正在捨棄智慧型預覽",
"Importing Lightroom catalog": "正在匯入 Lightroom 目錄",
"Reading Lightroom catalog": "正在讀取 Lightroom 目錄",
- "Merging": "正在合併"
+ "Merging": "正在合併",
+ "Downloading model": "正在下載模型",
+ "Finding faces": "正在尋找臉孔"
}
diff --git a/crates/ui-egui/src/panels/faces.rs b/crates/ui-egui/src/panels/faces.rs
index 3117eb607..13b4b1329 100644
--- a/crates/ui-egui/src/panels/faces.rs
+++ b/crates/ui-egui/src/panels/faces.rs
@@ -669,8 +669,9 @@ pub fn pump(app: &mut LightcraftApp, ctx: &egui::Context) {
app.caches.faces_active = v["active"] == true;
app.caches.faces_indexed = v["indexedFaces"].as_u64().unwrap_or(0);
app.caches.faces_pending = v["pendingPhotos"].as_u64().unwrap_or(0);
- // the progress bar's whole: the most photos that were left at once since the scan last finished
- app.caches.faces_peak = if app.caches.faces_pending == 0 { 0 } else { app.caches.faces_peak.max(app.caches.faces_pending) };
+ // the progress bar's whole: the most photos that were left at once since the scan last finished (the engine keeps
+ // it, so Settings and the activity stack agree)
+ app.caches.faces_peak = v["peak"].as_u64().unwrap_or(0);
// Frames are drawn only when something asks for one, so nothing here wakes the window needlessly: while there is work it
// asks to be called again in 50 ms (the progress in Settings, the next photos for the workers); with recognition on and
// nothing to do it looks again every few seconds (a few wake-ups a minute); with recognition off it asks for nothing