mirror of
https://github.com/storytold/lightcraft.git
synced 2026-11-28 09:19:25 +01:00
The first search reads a thumbnail-level source of every photo in the library, one at a time on the thread that draws the window. In a large library the window stopped responding for as long as that took, and Windows offered to close the program (#3468). The command is split into plan / measure / keep / apply, as photo.reload is. The app measures on a worker thread under an activity row that counts and can stop; a stopped search leaves the view alone and keeps what it read. A control request gets the same {photos} when the search ends. Opening another library stops the search. Fixes #3468 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CvoftM8ZqGRb62UWw9DAKP
130 lines
6.2 KiB
Rust
130 lines
6.2 KiB
Rust
//! Assisted culling: score photos for focus and clipping, group similar shots taken close
|
|
//! together (bursts) and mark the sharpest of each group; optionally reject blurry ones and pick
|
|
//! each group's best. Scores are kept on the photos (rules / smart albums: `sharpness`,
|
|
//! `bestOfGroup`).
|
|
|
|
use lightcraft_catalog::{Analysis, Flag, Op, PhotoId};
|
|
use serde_json::{Value, json};
|
|
|
|
use super::{CommandSpec, always, cmd};
|
|
use crate::{Result, Session};
|
|
|
|
/// Seconds between shots of the same burst at most (default of `burstGap`).
|
|
const BURST_GAP: f64 = 10.0;
|
|
/// Signature similarity for "the same scene" (default of `similarity`).
|
|
const SAME: f64 = 0.93;
|
|
/// The longest `burstGap` accepted: a day.
|
|
const MAX_GAP: f64 = 86_400.0;
|
|
|
|
fn secs(iso: &str) -> Option<i64> {
|
|
let d = lightcraft_meta::DateTime::parse_iso(iso)?;
|
|
Some(d.unix_seconds())
|
|
}
|
|
|
|
fn analyze(s: &mut Session, p: &Value) -> Result<Value> {
|
|
let ids: Vec<PhotoId> = if p.get("ids").is_some() || s.selection.ids.len() > 1 { s.targets(p)? } else { s.visible_cloned() };
|
|
// a photo listed twice is one photo, not a burst of two look-alikes (first occurrence kept)
|
|
let mut seen = std::collections::HashSet::new();
|
|
let ids: Vec<PhotoId> = ids.into_iter().filter(|id| seen.insert(*id)).collect();
|
|
let reject_below = p.get("rejectBelow").and_then(Value::as_f64).map(|v| v as f32);
|
|
let pick_best = p.get("pickBest").and_then(Value::as_bool).unwrap_or(false);
|
|
let (gap, same) = burst_params(p)?;
|
|
// measure (thumbnail-level sources: fast, and enough for focus at 512 px)
|
|
let mut rows: Vec<(PhotoId, Option<i64>, f32, f32, [f32; 64])> = Vec::new();
|
|
let mut failed = Vec::new();
|
|
for id in ids {
|
|
match s.source_now(id, crate::media::SourceLevel::Thumb) {
|
|
Ok(src) => {
|
|
let t = s.catalog.photo(id).and_then(|p| p.captured.as_deref().and_then(secs));
|
|
use lightcraft_pipeline::cull;
|
|
rows.push((id, t, cull::sharpness(&src), cull::clipped(&src), cull::signature(&src)));
|
|
}
|
|
Err(e) => failed.push(json!([id.0, e])),
|
|
}
|
|
}
|
|
// bursts: consecutive (by capture time) look-alike shots
|
|
rows.sort_by_key(|r| (r.1.unwrap_or(i64::MAX), r.0));
|
|
let mut group_of: Vec<Option<u32>> = vec![None; rows.len()];
|
|
let mut next = 1u32;
|
|
for i in 1..rows.len() {
|
|
let (a, b) = (&rows[i - 1], &rows[i]);
|
|
let close = matches!((a.1, b.1), (Some(x), Some(y)) if (y.saturating_sub(x)).unsigned_abs() as f64 <= gap);
|
|
if close && lightcraft_pipeline::cull::similarity(&a.4, &b.4) >= same {
|
|
let g = *group_of[i - 1].get_or_insert_with(|| {
|
|
next += 1;
|
|
next - 1
|
|
});
|
|
group_of[i] = Some(g);
|
|
}
|
|
}
|
|
let mut best: std::collections::HashMap<u32, (usize, f32)> = Default::default();
|
|
for (i, g) in group_of.iter().enumerate() {
|
|
if let Some(g) = g {
|
|
let e = best.entry(*g).or_insert((i, -1.0));
|
|
if rows[i].2 > e.1 {
|
|
*e = (i, rows[i].2);
|
|
}
|
|
}
|
|
}
|
|
let mut ops = Vec::new();
|
|
let (mut rejected, mut picked) = (0, 0);
|
|
let mut out = Vec::new();
|
|
for (i, (id, _, sharp, clip, _)) in rows.iter().enumerate() {
|
|
let g = group_of[i];
|
|
let is_best = g.is_some_and(|g| best.get(&g).is_some_and(|b| b.0 == i));
|
|
ops.push(Op::SetAnalysis { id: *id, analysis: Some(Analysis { sharpness: *sharp, clipped: *clip, group: g, best: is_best }) });
|
|
let flag = s.catalog.photo(*id).map(|p| p.flag).unwrap_or_default();
|
|
if reject_below.is_some_and(|t| *sharp < t) && flag != Flag::Reject {
|
|
ops.push(Op::SetFlag { id: *id, flag: Flag::Reject });
|
|
rejected += 1;
|
|
} else if pick_best && is_best && flag == Flag::None {
|
|
ops.push(Op::SetFlag { id: *id, flag: Flag::Pick });
|
|
picked += 1;
|
|
}
|
|
out.push(json!({"id": id.0, "sharpness": (sharp * 10.0).round() / 10.0, "clipped": clip, "group": g, "best": is_best}));
|
|
}
|
|
if !ops.is_empty() {
|
|
s.commit("Assisted Culling", Op::Batch { ops })?;
|
|
}
|
|
Ok(json!({"photos": out, "groups": best.len(), "rejected": rejected, "picked": picked, "failed": failed}))
|
|
}
|
|
|
|
/// `burstGap` (seconds, 0..=a day) and `similarity` (0..=1) of `photo.analyze`, refused before
|
|
/// anything is measured when they are not numbers in range.
|
|
fn burst_params(p: &Value) -> Result<(f64, f32)> {
|
|
const C: &str = "photo.analyze";
|
|
let num = |key: &str, d: f64, max: f64| -> Result<f64> {
|
|
match p.get(key) {
|
|
None | Some(Value::Null) => Ok(d),
|
|
Some(v) => match v.as_f64() {
|
|
Some(x) if x.is_finite() && (0.0..=max).contains(&x) => Ok(x),
|
|
_ => Err(super::bad(C, format!("`{key}` must be a number from 0 to {max}"))),
|
|
},
|
|
}
|
|
};
|
|
Ok((num("burstGap", BURST_GAP, MAX_GAP)?, num("similarity", SAME, 1.0)? as f32))
|
|
}
|
|
|
|
pub fn specs() -> Vec<CommandSpec> {
|
|
vec![
|
|
cmd!(
|
|
"library.findSimilar",
|
|
"Find Similar Photos",
|
|
["Photo"],
|
|
None,
|
|
"{id?, similarity?: 0..1 (0.8), filter?: bool (true)} — photos that look like the active one (composition and tones), most similar first; filters the view to them (Clear Filters to go back) → {photos: [{id, similarity}]}. In the app the photos are read on a worker thread under an activity row: a control request is answered with this when the search ends, and from a menu the answer is {background: true, count}",
|
|
super::similar::no_search_running,
|
|
super::similar::run
|
|
),
|
|
cmd!(
|
|
"photo.analyze",
|
|
"Assisted Culling",
|
|
[],
|
|
None,
|
|
"{ids?, rejectBelow?: sharpness 0..100, pickBest?: bool, burstGap?: seconds 0..86400 (10), similarity?: 0..1 (0.93)} — score the selected photos (else all in view) for focus (0..100) and clipping, group look-alike shots (signature similarity at least `similarity`) taken at most `burstGap` seconds apart and mark the sharpest of each; optionally reject blurry photos and pick each group's best; one undo step → {photos: [{id, sharpness, clipped, group, best}], groups, rejected, picked}",
|
|
always,
|
|
analyze
|
|
),
|
|
]
|
|
}
|