Add the Identity screen
A third top-level screen beside the library and develop, because naming a cluster and pulling a stranger out of it are tasks with their own rhythm and need the whole window. The screen is designed around the clustering being wrong, which is FR-CULL-10 rather than pessimism: grouping over-merges on siblings, on parents and children, and on the same person a decade apart. So Split off sits next to Confirm all rather than behind a menu, the confirm/reject pair is on the face itself, and a group the system found is drawn differently from a person the user has vouched for. Splitting rejects before it confirms. Without that the next clustering pass suggests the face straight back and the user's correction becomes an argument they keep having. Face crops come from the proxies the grid already built, one decode per image rather than per face -- a group photograph holding six faces of one family is one JPEG. Where the calibration is not fitted the screen says confidence is unavailable instead of printing a percentage that looks measured, which is FR-CULL-9's rule at the point it becomes visible. The verdict controls use drawn icons, not tick and cross characters: ui/icons.slint exists because those render as tofu on Android. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -446,6 +446,82 @@ fn group_probability(
|
||||
}
|
||||
}
|
||||
|
||||
/// A face cut out of its photograph, ready to draw.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct FaceCrop {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
/// Tightly packed RGBA.
|
||||
pub rgba: Vec<u8>,
|
||||
}
|
||||
|
||||
/// How much of the surrounding frame a face crop keeps, per side.
|
||||
///
|
||||
/// A face cut exactly to its detection box reads as a mugshot: no hair, no
|
||||
/// chin, no context, and a row of them is genuinely hard to tell apart — which
|
||||
/// matters, because telling them apart is the entire task the People screen
|
||||
/// asks of the user. A third on each side gives back the head.
|
||||
const CROP_MARGIN: f32 = 0.35;
|
||||
|
||||
/// Cut one face out of its proxy.
|
||||
///
|
||||
/// The box is normalised to the long edge (the catalog's convention), so this
|
||||
/// works whatever size the proxy happens to be now — the property that made
|
||||
/// normalising worth the trouble. The thumbnail cache is entitled to evict a
|
||||
/// proxy and regenerate it at another resolution, and a face stored in pixels
|
||||
/// would then point at the wrong part of the picture.
|
||||
pub fn crop_face(
|
||||
rgba: &[u8],
|
||||
width: u32,
|
||||
height: u32,
|
||||
face: &faces::Face,
|
||||
out_edge: u32,
|
||||
) -> Option<FaceCrop> {
|
||||
if width == 0 || height == 0 || out_edge == 0 {
|
||||
return None;
|
||||
}
|
||||
let long_edge = width.max(height) as f32;
|
||||
|
||||
// Square, centred on the face: the grid draws square cells, and cropping to
|
||||
// a square here rather than letterboxing there means the face fills the
|
||||
// cell instead of floating in it.
|
||||
let cx = (face.x + face.w * 0.5) * long_edge;
|
||||
let cy = (face.y + face.h * 0.5) * long_edge;
|
||||
let half = (face.w.max(face.h) * long_edge * 0.5) * (1.0 + CROP_MARGIN);
|
||||
if !(half.is_finite() && half > 0.5) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut out = vec![0u8; (out_edge * out_edge * 4) as usize];
|
||||
let step = (half * 2.0) / out_edge as f32;
|
||||
|
||||
for oy in 0..out_edge {
|
||||
let sy = cy - half + (oy as f32 + 0.5) * step;
|
||||
for ox in 0..out_edge {
|
||||
let sx = cx - half + (ox as f32 + 0.5) * step;
|
||||
let o = ((oy * out_edge + ox) * 4) as usize;
|
||||
// Nearest neighbour: this is a downscale of an already-small proxy
|
||||
// shown at ~96 px, and a bilinear tap would cost four reads per
|
||||
// pixel for a difference nobody can see at that size. Outside the
|
||||
// frame stays transparent, so a face at the very edge of the
|
||||
// picture is drawn short rather than smeared.
|
||||
if sx < 0.0 || sy < 0.0 || sx >= width as f32 || sy >= height as f32 {
|
||||
continue;
|
||||
}
|
||||
let i = ((sy as u32 * width + sx as u32) * 4) as usize;
|
||||
if i + 4 <= rgba.len() {
|
||||
out[o..o + 4].copy_from_slice(&rgba[i..i + 4]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some(FaceCrop {
|
||||
width: out_edge,
|
||||
height: out_edge,
|
||||
rgba: out,
|
||||
})
|
||||
}
|
||||
|
||||
/// `dr-thumbs` decodes to RGBA; `dr-face` reads packed `f32` RGB.
|
||||
fn rgba_to_rgb_f32(rgba: &[u8]) -> Vec<f32> {
|
||||
let mut out = Vec::with_capacity(rgba.len() / 4 * 3);
|
||||
@@ -470,6 +546,87 @@ mod tests {
|
||||
assert!((n[2].0 - 1.0).abs() < 1e-6);
|
||||
}
|
||||
|
||||
fn gradient(width: u32, height: u32) -> Vec<u8> {
|
||||
let mut v = vec![0u8; (width * height * 4) as usize];
|
||||
for (i, px) in v.chunks_exact_mut(4).enumerate() {
|
||||
// A gradient, so a mis-placed crop shows up as the wrong value
|
||||
// rather than as more of the same colour.
|
||||
px[0] = (i % 251) as u8;
|
||||
px[1] = 40;
|
||||
px[2] = 90;
|
||||
px[3] = 255;
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
fn stored_face(x: f32, y: f32, w: f32, h: f32) -> faces::Face {
|
||||
faces::Face {
|
||||
id: faces::FaceId(1),
|
||||
image_id: ImageId(1),
|
||||
x,
|
||||
y,
|
||||
w,
|
||||
h,
|
||||
landmarks: [(0.0, 0.0); 5],
|
||||
confidence: 0.9,
|
||||
crop_px: 120.0,
|
||||
model_id: "w600k_mbf".into(),
|
||||
person: None,
|
||||
probability: 0.0,
|
||||
confirmed: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_face_crop_is_square_and_the_size_asked_for() {
|
||||
let px = gradient(1024, 683);
|
||||
let c = crop_face(&px, 1024, 683, &stored_face(0.3, 0.2, 0.1, 0.15), 96).unwrap();
|
||||
assert_eq!((c.width, c.height), (96, 96));
|
||||
assert_eq!(c.rgba.len(), 96 * 96 * 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_degenerate_box_yields_no_crop_rather_than_a_panic() {
|
||||
let px = gradient(64, 64);
|
||||
assert!(crop_face(&px, 64, 64, &stored_face(0.5, 0.5, 0.0, 0.0), 96).is_none());
|
||||
assert!(crop_face(&px, 0, 0, &stored_face(0.1, 0.1, 0.2, 0.2), 96).is_none());
|
||||
assert!(crop_face(&px, 64, 64, &stored_face(0.1, 0.1, 0.2, 0.2), 0).is_none());
|
||||
}
|
||||
|
||||
/// A face at the very edge of the frame is drawn short, not smeared: the
|
||||
/// out-of-frame margin stays transparent.
|
||||
#[test]
|
||||
fn a_face_at_the_edge_keeps_a_transparent_margin() {
|
||||
let px = gradient(200, 200);
|
||||
let c = crop_face(&px, 200, 200, &stored_face(0.0, 0.0, 0.1, 0.1), 32).unwrap();
|
||||
assert_eq!(c.rgba[3], 0, "outside the frame should be transparent");
|
||||
let centre = ((16 * 32 + 16) * 4 + 3) as usize;
|
||||
assert_eq!(c.rgba[centre], 255, "the face itself should be opaque");
|
||||
}
|
||||
|
||||
/// The normalised box means a proxy regenerated at another resolution still
|
||||
/// crops the same part of the picture — what the catalog's normalisation is
|
||||
/// for.
|
||||
#[test]
|
||||
fn the_same_face_crops_the_same_region_at_two_proxy_sizes() {
|
||||
let face = stored_face(0.25, 0.25, 0.2, 0.2);
|
||||
let small = crop_face(&gradient(400, 400), 400, 400, &face, 16).unwrap();
|
||||
let large = crop_face(&gradient(800, 800), 800, 800, &face, 16).unwrap();
|
||||
assert!(small.rgba.chunks_exact(4).all(|p| p[3] == 255));
|
||||
assert!(large.rgba.chunks_exact(4).all(|p| p[3] == 255));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_crop_keeps_margin_around_the_detection_box() {
|
||||
// A 0.1-wide face in a 1000px frame is 100px; with the margin the crop
|
||||
// spans 100 * 1.35 = 135px of source.
|
||||
let face = stored_face(0.4, 0.4, 0.1, 0.1);
|
||||
let c = crop_face(&gradient(1000, 1000), 1000, 1000, &face, 135).unwrap();
|
||||
assert_eq!(c.width, 135);
|
||||
// Fully inside the frame, so nothing is transparent.
|
||||
assert!(c.rgba.chunks_exact(4).all(|p| p[3] == 255));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rgba_becomes_packed_rgb_dropping_alpha() {
|
||||
let rgba = [255u8, 128, 0, 255, 0, 0, 0, 128];
|
||||
|
||||
@@ -0,0 +1,574 @@
|
||||
//! TRACES: FR-CULL-10 | FR-CULL-11 | FR-CULL-12 | NFR-SEC-5
|
||||
//! The Identity screen's model: who the library knows, and how the user fixes it.
|
||||
//!
|
||||
//! A third top-level screen beside the library and develop, because it is a
|
||||
//! *place you go to work*, not a panel you glance at: naming a cluster, pulling
|
||||
//! a stranger out of it, and merging two halves of the same person are tasks
|
||||
//! with their own rhythm, and they need the whole window.
|
||||
//!
|
||||
//! # The screen exists because the clustering is wrong
|
||||
//!
|
||||
//! That is not pessimism, it is FR-CULL-10: clustering will over-merge on
|
||||
//! siblings, on parents and children, and on the same person a decade apart. A
|
||||
//! tool that could only merge would make its own errors permanent, so
|
||||
//! **splitting is as prominent as merging here**, and every suggestion is
|
||||
//! visibly a suggestion until the user says otherwise.
|
||||
//!
|
||||
//! # What this module is, and is not
|
||||
//!
|
||||
//! It builds a view model from the catalog and applies the user's decisions
|
||||
//! back to it. It holds no Slint types, so the whole of it is testable against
|
||||
//! an in-memory catalog with no window open — which matters because the
|
||||
//! operations it performs are the ones that touch user data.
|
||||
|
||||
use dr_catalog::faces::{self, FaceId, PersonId};
|
||||
use dr_catalog::Catalog;
|
||||
use dr_thumbs::ThumbStore;
|
||||
use dr_types::ImageId;
|
||||
|
||||
use crate::faces::{crop_face, FaceCrop, FACE_TIER};
|
||||
|
||||
/// Edge of a face crop in the grid, in pixels.
|
||||
///
|
||||
/// Generous for a thumbnail because the judgement being asked for — *is this
|
||||
/// the same person* — is one the user cannot make from a postage stamp.
|
||||
pub const FACE_CROP_EDGE: u32 = 128;
|
||||
|
||||
/// A row in the people rail.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct PersonRow {
|
||||
pub id: PersonId,
|
||||
/// Empty for a cluster the system found and the user has not named.
|
||||
pub name: String,
|
||||
pub confirmed_faces: u64,
|
||||
pub suggested_faces: u64,
|
||||
/// The face to show as this person's portrait, if any is loaded.
|
||||
pub cover: Option<FaceId>,
|
||||
}
|
||||
|
||||
impl PersonRow {
|
||||
/// What to draw when the person has no name yet.
|
||||
///
|
||||
/// Deliberately not a guessed name. FR-CULL-10 has the *user* name a group,
|
||||
/// and a system-chosen name would be an inference wearing a fact's clothes
|
||||
/// — the exact conflation the confirmed/suggested split exists to prevent.
|
||||
pub fn display_name(&self) -> String {
|
||||
if self.name.is_empty() {
|
||||
format!("Unnamed ({} faces)", self.confirmed_faces + self.suggested_faces)
|
||||
} else {
|
||||
self.name.clone()
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether this group is still entirely the system's opinion.
|
||||
pub fn is_unconfirmed(&self) -> bool {
|
||||
self.confirmed_faces == 0
|
||||
}
|
||||
}
|
||||
|
||||
/// One face in the grid.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct FaceCell {
|
||||
pub face: FaceId,
|
||||
pub image: ImageId,
|
||||
/// `None` while the crop is still being cut.
|
||||
pub crop: Option<FaceCrop>,
|
||||
/// The user asserted this, as against the system guessing it.
|
||||
pub confirmed: bool,
|
||||
/// Calibrated P(this face is this person).
|
||||
pub probability: f32,
|
||||
/// Whether that probability means anything — false when the library's
|
||||
/// calibration has not been fitted (FR-CULL-9).
|
||||
pub probability_known: bool,
|
||||
/// Source pixels across the aligned crop. Small faces embed worse, and the
|
||||
/// user deserves to know which of a bad suggestion's causes is in play.
|
||||
pub crop_px: f32,
|
||||
}
|
||||
|
||||
impl FaceCell {
|
||||
/// Confidence as text, or why there is none.
|
||||
///
|
||||
/// The FR-CULL-9 rule made concrete: where the calibration is not fitted,
|
||||
/// this says so rather than printing an untuned number that looks measured.
|
||||
pub fn confidence_label(&self) -> String {
|
||||
if self.confirmed {
|
||||
"Confirmed".into()
|
||||
} else if !self.probability_known {
|
||||
"Confidence unavailable".into()
|
||||
} else {
|
||||
format!("{:.0}% likely", self.probability * 100.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Everything the Identity screen draws.
|
||||
#[derive(Debug, Clone, Default, PartialEq)]
|
||||
pub struct IdentityView {
|
||||
pub people: Vec<PersonRow>,
|
||||
pub selected: Option<PersonId>,
|
||||
pub faces: Vec<FaceCell>,
|
||||
/// Faces belonging to nobody — the pool the user can pull a new person out
|
||||
/// of, and the honest answer to "why is this photo not under anyone".
|
||||
pub unassigned: usize,
|
||||
/// Whether the library's similarity calibration has been fitted.
|
||||
pub calibrated: bool,
|
||||
}
|
||||
|
||||
/// Read the people rail.
|
||||
pub fn load_people(catalog: &Catalog, model_id: &str) -> Result<IdentityView, dr_catalog::CatalogError> {
|
||||
let conn = catalog.connection();
|
||||
let people = faces::people(conn)?
|
||||
.into_iter()
|
||||
.map(|p| PersonRow {
|
||||
id: p.id,
|
||||
name: p.name,
|
||||
confirmed_faces: p.confirmed_faces,
|
||||
suggested_faces: p.suggested_faces,
|
||||
cover: None,
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(IdentityView {
|
||||
people,
|
||||
selected: None,
|
||||
faces: Vec::new(),
|
||||
unassigned: faces::unassigned(conn, model_id)?.len(),
|
||||
calibrated: faces::calibration(conn, model_id)?.is_some_and(|(c, _)| c.valid),
|
||||
})
|
||||
}
|
||||
|
||||
/// Read one person's faces, cutting a crop for each.
|
||||
///
|
||||
/// Suggestions are included and marked, never hidden: the whole purpose of this
|
||||
/// screen is ruling on them, and a screen that showed only confirmed faces
|
||||
/// would give the user nothing to do.
|
||||
///
|
||||
/// Crops come from the proxy the grid already built. An image whose proxy has
|
||||
/// been evicted yields a cell with no crop rather than being dropped — the face
|
||||
/// is still real, still counted, and still confirmable from its filename.
|
||||
pub fn load_faces(
|
||||
catalog: &Catalog,
|
||||
store: &ThumbStore,
|
||||
person: PersonId,
|
||||
calibrated: bool,
|
||||
) -> Result<Vec<FaceCell>, dr_catalog::CatalogError> {
|
||||
let conn = catalog.connection();
|
||||
let rows = faces::for_person(conn, person, true)?;
|
||||
|
||||
// One decode per *image*, not per face: a group photograph holding six
|
||||
// faces of one family is one JPEG, and decoding it six times is the kind of
|
||||
// waste that only shows up on a slow machine.
|
||||
let mut decoded: std::collections::HashMap<ImageId, Option<(u32, u32, Vec<u8>)>> =
|
||||
std::collections::HashMap::new();
|
||||
|
||||
let mut out = Vec::with_capacity(rows.len());
|
||||
for f in rows {
|
||||
let entry = decoded
|
||||
.entry(f.image_id)
|
||||
.or_insert_with(|| decode_proxy(catalog, store, f.image_id));
|
||||
|
||||
let crop = entry
|
||||
.as_ref()
|
||||
.and_then(|(w, h, rgba)| crop_face(rgba, *w, *h, &f, FACE_CROP_EDGE));
|
||||
|
||||
out.push(FaceCell {
|
||||
face: f.id,
|
||||
image: f.image_id,
|
||||
crop,
|
||||
confirmed: f.confirmed,
|
||||
probability: f.probability,
|
||||
probability_known: calibrated,
|
||||
crop_px: f.crop_px,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn decode_proxy(
|
||||
catalog: &Catalog,
|
||||
store: &ThumbStore,
|
||||
image: ImageId,
|
||||
) -> Option<(u32, u32, Vec<u8>)> {
|
||||
let file_id: i64 = catalog
|
||||
.connection()
|
||||
.query_row(
|
||||
"SELECT file_id FROM remote WHERE image_id = ?1",
|
||||
[image.0 as i64],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.ok()?;
|
||||
let thumb = store.get(file_id as u64, FACE_TIER).ok()??;
|
||||
dr_thumbs::codec::decode_rgba(&thumb.bytes).ok()
|
||||
}
|
||||
|
||||
// ── the user's decisions ──────────────────────────────────────────────────
|
||||
|
||||
/// Name a group, or rename a person.
|
||||
///
|
||||
/// The identity is untouched: FR-CULL-12 makes a *name* the user data that
|
||||
/// travels to the sidecar, while the person's UUID is what a cross-device merge
|
||||
/// keys on, and renaming must not create a second person on the other device.
|
||||
pub fn rename(
|
||||
catalog: &Catalog,
|
||||
person: PersonId,
|
||||
name: &str,
|
||||
) -> Result<(), dr_catalog::CatalogError> {
|
||||
faces::rename_person(catalog.connection(), person, name.trim())
|
||||
}
|
||||
|
||||
/// Confirm every suggestion in a group at once.
|
||||
///
|
||||
/// The operation the screen exists to make cheap. A cluster that is simply
|
||||
/// right — the common case for a well-photographed person — should cost one
|
||||
/// click, not forty.
|
||||
pub fn confirm_all(
|
||||
catalog: &Catalog,
|
||||
person: PersonId,
|
||||
) -> Result<usize, dr_catalog::CatalogError> {
|
||||
let conn = catalog.connection();
|
||||
let mut n = 0;
|
||||
for f in faces::for_person(conn, person, true)? {
|
||||
if !f.confirmed {
|
||||
faces::confirm(conn, f.id, person)?;
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
Ok(n)
|
||||
}
|
||||
|
||||
/// The user says this face is this person.
|
||||
pub fn confirm(
|
||||
catalog: &Catalog,
|
||||
face: FaceId,
|
||||
person: PersonId,
|
||||
) -> Result<(), dr_catalog::CatalogError> {
|
||||
faces::confirm(catalog.connection(), face, person)
|
||||
}
|
||||
|
||||
/// The user says this face is not this person.
|
||||
///
|
||||
/// Remembered, so the next clustering pass does not put it straight back —
|
||||
/// which is the difference between a correction and a recurring argument.
|
||||
pub fn reject(
|
||||
catalog: &Catalog,
|
||||
face: FaceId,
|
||||
person: PersonId,
|
||||
) -> Result<(), dr_catalog::CatalogError> {
|
||||
faces::reject(catalog.connection(), face, person)
|
||||
}
|
||||
|
||||
/// Fold one person into another.
|
||||
pub fn merge(
|
||||
catalog: &Catalog,
|
||||
target: PersonId,
|
||||
source: PersonId,
|
||||
) -> Result<u64, dr_catalog::CatalogError> {
|
||||
faces::merge_people(catalog.connection(), target, source)
|
||||
}
|
||||
|
||||
/// What a split would produce, without performing it.
|
||||
///
|
||||
/// Offered as a preview because a split is the destructive-feeling half of
|
||||
/// FR-CULL-10 and the user should see the groups before agreeing to them. It
|
||||
/// re-agglomerates this person's faces at a stricter threshold; a person who is
|
||||
/// genuinely one person comes back as one group and the UI can say so instead
|
||||
/// of splitting nothing.
|
||||
pub fn preview_split(
|
||||
catalog: &Catalog,
|
||||
store: &ThumbStore,
|
||||
person: PersonId,
|
||||
model_id: &str,
|
||||
strictness: f32,
|
||||
) -> Result<Vec<Vec<FaceCell>>, dr_catalog::CatalogError> {
|
||||
let conn = catalog.connection();
|
||||
let cal = faces::calibration(conn, model_id)?
|
||||
.map(|(c, _)| c)
|
||||
.unwrap_or_default();
|
||||
let calibrated = cal.valid;
|
||||
|
||||
let cells = load_faces(catalog, store, person, calibrated)?;
|
||||
if cells.len() < 2 {
|
||||
return Ok(vec![cells]);
|
||||
}
|
||||
|
||||
let model = dr_face::ModelId::new(model_id.to_string());
|
||||
let stored: std::collections::HashMap<_, _> = faces::embeddings(conn, model_id)?
|
||||
.into_iter()
|
||||
.map(|(id, image, blob, crop_px)| (id, (image, blob, crop_px)))
|
||||
.collect();
|
||||
|
||||
let mut candidates = Vec::with_capacity(cells.len());
|
||||
let mut order = Vec::with_capacity(cells.len());
|
||||
for c in &cells {
|
||||
let Some((image, blob, crop_px)) = stored.get(&c.face) else {
|
||||
continue;
|
||||
};
|
||||
let Some(emb) = dr_face::Embedding::from_f16_bytes(model.clone(), blob) else {
|
||||
continue;
|
||||
};
|
||||
candidates.push(dr_face::Candidate {
|
||||
face: c.face.0,
|
||||
image: image.0,
|
||||
embedding: emb.v.to_vec(),
|
||||
crop_px: *crop_px,
|
||||
confirmed_person: None,
|
||||
});
|
||||
order.push(c.clone());
|
||||
}
|
||||
|
||||
let groups = dr_face::split(&candidates, &cal, strictness);
|
||||
Ok(groups
|
||||
.into_iter()
|
||||
.map(|g| g.members.into_iter().map(|m| order[m].clone()).collect())
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Pull a set of faces out of a person and onto a new one.
|
||||
///
|
||||
/// The commit half of [`preview_split`], and also what "these three are
|
||||
/// actually someone else" does. The faces are **confirmed** onto the new
|
||||
/// person, because the user has just asserted they belong together — leaving
|
||||
/// them as suggestions would invite the next clustering pass to undo the
|
||||
/// correction.
|
||||
pub fn split_off(
|
||||
catalog: &Catalog,
|
||||
from: PersonId,
|
||||
members: &[FaceId],
|
||||
name: &str,
|
||||
) -> Result<PersonId, dr_catalog::CatalogError> {
|
||||
let conn = catalog.connection();
|
||||
let new_person = faces::create_person(conn, name.trim())?;
|
||||
for &face in members {
|
||||
// Rejecting first is what stops the split being undone: without it the
|
||||
// next pass sees a face that looks like `from` and suggests it straight
|
||||
// back, and the user's correction becomes an argument they keep having.
|
||||
faces::reject(conn, face, from)?;
|
||||
faces::confirm(conn, face, new_person)?;
|
||||
}
|
||||
Ok(new_person)
|
||||
}
|
||||
|
||||
/// Detach a face from everyone, asserting nothing.
|
||||
///
|
||||
/// Distinct from [`reject`]: this is "I do not know", and the face returns to
|
||||
/// the pool for the next clustering pass to place. Needed for the case where
|
||||
/// the user can see a crop is a lamp, not a person — rejecting it from *this*
|
||||
/// person would leave it free to be suggested for the next one.
|
||||
pub fn unassign(catalog: &Catalog, face: FaceId) -> Result<(), dr_catalog::CatalogError> {
|
||||
faces::unassign(catalog.connection(), face)
|
||||
}
|
||||
|
||||
/// Delete every face, person and embedding in the library (NFR-SEC-5).
|
||||
///
|
||||
/// Surfaced from this screen because this is where the user's face data
|
||||
/// visibly lives, and a control to remove it that is hidden in a settings page
|
||||
/// three levels down is a control that does not really exist.
|
||||
pub fn delete_all(catalog: &Catalog) -> Result<u64, dr_catalog::CatalogError> {
|
||||
faces::delete_all_face_data(catalog.connection())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use dr_catalog::faces::DetectedFace;
|
||||
|
||||
fn catalog() -> Catalog {
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
"dr-identity-{}-{:?}",
|
||||
std::process::id(),
|
||||
std::thread::current().id()
|
||||
));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("catalog.db");
|
||||
let _ = std::fs::remove_file(&path);
|
||||
Catalog::open(&path).unwrap()
|
||||
}
|
||||
|
||||
fn image(c: &Catalog, n: i64) -> ImageId {
|
||||
let conn = c.connection();
|
||||
conn.execute(
|
||||
"INSERT OR IGNORE INTO roots(id, kind, label) VALUES (1, 'local', 'lib')",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
conn.execute(
|
||||
"INSERT INTO images(id, root_id, source_ref, added_at) VALUES (?1, 1, ?2, 0)",
|
||||
rusqlite::params![n, format!("IMG_{n}.CR3")],
|
||||
)
|
||||
.unwrap();
|
||||
ImageId(n as u64)
|
||||
}
|
||||
|
||||
fn face(seed: u8) -> DetectedFace {
|
||||
DetectedFace {
|
||||
x: 0.1,
|
||||
y: 0.1,
|
||||
w: 0.2,
|
||||
h: 0.3,
|
||||
landmarks: [(0.1, 0.1); 5],
|
||||
confidence: 0.9,
|
||||
embedding: vec![seed; 1024],
|
||||
crop_px: 180.0,
|
||||
model_id: "w600k_mbf".into(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unnamed_group_says_how_many_faces_it_holds_rather_than_guessing_a_name() {
|
||||
let row = PersonRow {
|
||||
id: PersonId(1),
|
||||
name: String::new(),
|
||||
confirmed_faces: 0,
|
||||
suggested_faces: 12,
|
||||
cover: None,
|
||||
};
|
||||
assert_eq!(row.display_name(), "Unnamed (12 faces)");
|
||||
assert!(row.is_unconfirmed());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_named_person_shows_their_name() {
|
||||
let row = PersonRow {
|
||||
id: PersonId(1),
|
||||
name: "Anna".into(),
|
||||
confirmed_faces: 4,
|
||||
suggested_faces: 2,
|
||||
cover: None,
|
||||
};
|
||||
assert_eq!(row.display_name(), "Anna");
|
||||
assert!(!row.is_unconfirmed());
|
||||
}
|
||||
|
||||
/// FR-CULL-9's rule at the point it becomes visible: an unfitted
|
||||
/// calibration must not print a number that looks measured.
|
||||
#[test]
|
||||
fn an_uncalibrated_library_says_so_instead_of_showing_a_percentage() {
|
||||
let cell = FaceCell {
|
||||
face: FaceId(1),
|
||||
image: ImageId(1),
|
||||
crop: None,
|
||||
confirmed: false,
|
||||
probability: 0.87,
|
||||
probability_known: false,
|
||||
crop_px: 120.0,
|
||||
};
|
||||
assert_eq!(cell.confidence_label(), "Confidence unavailable");
|
||||
|
||||
let calibrated = FaceCell {
|
||||
probability_known: true,
|
||||
..cell.clone()
|
||||
};
|
||||
assert_eq!(calibrated.confidence_label(), "87% likely");
|
||||
|
||||
let confirmed = FaceCell {
|
||||
confirmed: true,
|
||||
probability_known: false,
|
||||
..cell
|
||||
};
|
||||
assert_eq!(confirmed.confidence_label(), "Confirmed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_people_rail_reports_the_unassigned_pool() {
|
||||
let c = catalog();
|
||||
let i1 = image(&c, 1);
|
||||
let i2 = image(&c, 2);
|
||||
let a = faces::record_detections(c.connection(), i1, &[face(1)]).unwrap();
|
||||
faces::record_detections(c.connection(), i2, &[face(2)]).unwrap();
|
||||
|
||||
let anna = faces::create_person(c.connection(), "Anna").unwrap();
|
||||
faces::confirm(c.connection(), a[0], anna).unwrap();
|
||||
|
||||
let view = load_people(&c, "w600k_mbf").unwrap();
|
||||
assert_eq!(view.people.len(), 1);
|
||||
assert_eq!(view.people[0].name, "Anna");
|
||||
assert_eq!(view.unassigned, 1);
|
||||
assert!(!view.calibrated, "a fresh library has no fitted calibration");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn confirm_all_promotes_every_suggestion_and_nothing_else() {
|
||||
let c = catalog();
|
||||
let i1 = image(&c, 1);
|
||||
let i2 = image(&c, 2);
|
||||
let i3 = image(&c, 3);
|
||||
let a = faces::record_detections(c.connection(), i1, &[face(1)]).unwrap();
|
||||
let b = faces::record_detections(c.connection(), i2, &[face(2)]).unwrap();
|
||||
let d = faces::record_detections(c.connection(), i3, &[face(3)]).unwrap();
|
||||
|
||||
let anna = faces::create_person(c.connection(), "Anna").unwrap();
|
||||
faces::confirm(c.connection(), a[0], anna).unwrap();
|
||||
faces::suggest(c.connection(), b[0], anna, 0.8).unwrap();
|
||||
faces::suggest(c.connection(), d[0], anna, 0.7).unwrap();
|
||||
|
||||
assert_eq!(confirm_all(&c, anna).unwrap(), 2);
|
||||
assert_eq!(confirm_all(&c, anna).unwrap(), 0, "should be idempotent");
|
||||
assert_eq!(faces::for_person(c.connection(), anna, false).unwrap().len(), 3);
|
||||
}
|
||||
|
||||
/// The correction must stick. Splitting a face off and then re-running
|
||||
/// clustering must not put it back where the user took it from.
|
||||
#[test]
|
||||
fn a_split_face_is_not_suggested_back_to_the_person_it_left() {
|
||||
let c = catalog();
|
||||
let i1 = image(&c, 1);
|
||||
let i2 = image(&c, 2);
|
||||
let a = faces::record_detections(c.connection(), i1, &[face(1)]).unwrap();
|
||||
let b = faces::record_detections(c.connection(), i2, &[face(2)]).unwrap();
|
||||
|
||||
let anna = faces::create_person(c.connection(), "Anna").unwrap();
|
||||
faces::confirm(c.connection(), a[0], anna).unwrap();
|
||||
faces::suggest(c.connection(), b[0], anna, 0.95).unwrap();
|
||||
|
||||
let bob = split_off(&c, anna, &[b[0]], "Bob").unwrap();
|
||||
assert_eq!(faces::for_person(c.connection(), bob, false).unwrap().len(), 1);
|
||||
assert_eq!(faces::for_person(c.connection(), anna, true).unwrap().len(), 1);
|
||||
|
||||
// The next clustering pass tries again and is refused.
|
||||
assert!(
|
||||
!faces::suggest(c.connection(), b[0], anna, 0.99).unwrap(),
|
||||
"the split was undone by the next inference pass"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn renaming_trims_and_keeps_the_person() {
|
||||
let c = catalog();
|
||||
let anna = faces::create_person(c.connection(), "").unwrap();
|
||||
rename(&c, anna, " Anna Smith ").unwrap();
|
||||
let people = faces::people(c.connection()).unwrap();
|
||||
assert_eq!(people[0].name, "Anna Smith");
|
||||
assert_eq!(people[0].id, anna);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn merging_folds_one_person_into_the_other() {
|
||||
let c = catalog();
|
||||
let i1 = image(&c, 1);
|
||||
let i2 = image(&c, 2);
|
||||
let a = faces::record_detections(c.connection(), i1, &[face(1)]).unwrap();
|
||||
let b = faces::record_detections(c.connection(), i2, &[face(2)]).unwrap();
|
||||
let anna = faces::create_person(c.connection(), "Anna").unwrap();
|
||||
let annie = faces::create_person(c.connection(), "Annie").unwrap();
|
||||
faces::confirm(c.connection(), a[0], anna).unwrap();
|
||||
faces::confirm(c.connection(), b[0], annie).unwrap();
|
||||
|
||||
assert_eq!(merge(&c, anna, annie).unwrap(), 1);
|
||||
assert_eq!(load_people(&c, "w600k_mbf").unwrap().people.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deleting_everything_empties_the_screen() {
|
||||
let c = catalog();
|
||||
let i1 = image(&c, 1);
|
||||
let a = faces::record_detections(c.connection(), i1, &[face(1)]).unwrap();
|
||||
let anna = faces::create_person(c.connection(), "Anna").unwrap();
|
||||
faces::confirm(c.connection(), a[0], anna).unwrap();
|
||||
|
||||
assert_eq!(delete_all(&c).unwrap(), 1);
|
||||
let view = load_people(&c, "w600k_mbf").unwrap();
|
||||
assert!(view.people.is_empty());
|
||||
assert_eq!(view.unassigned, 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,400 @@
|
||||
//! TRACES: FR-CULL-9 | FR-CULL-10 | FR-CULL-11 | NFR-SEC-5
|
||||
//! The Identity screen's wiring: catalog state onto Slint models and back.
|
||||
//!
|
||||
//! [`crate::identity`] holds the decisions and is testable without a window;
|
||||
//! this file is the part that cannot be, and it is deliberately thin. Anything
|
||||
//! here that starts making a judgement belongs over there.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use dr_catalog::faces::{FaceId, PersonId};
|
||||
use dr_catalog::Catalog;
|
||||
use dr_thumbs::ThumbStore;
|
||||
use slint::{ComponentHandle, ModelRc, VecModel};
|
||||
|
||||
use crate::identity::{self, FaceCell, PersonRow};
|
||||
use crate::{AppWindow, IdentityFace, IdentityPerson};
|
||||
|
||||
/// Which model's faces the screen is looking at.
|
||||
///
|
||||
/// A constant for now because exactly one model is supported at a time; it is
|
||||
/// named rather than inlined because every query in this file is keyed on it,
|
||||
/// and a library indexed across a model change holds faces from both.
|
||||
pub const MODEL_ID: &str = "w600k_mbf";
|
||||
|
||||
/// Screen state that outlives a single callback.
|
||||
#[derive(Default)]
|
||||
pub struct IdentityController {
|
||||
people: RefCell<Vec<PersonRow>>,
|
||||
faces: RefCell<Vec<FaceCell>>,
|
||||
selected: std::cell::Cell<Option<PersonId>>,
|
||||
/// Faces the user has ticked, which is what a split would carry.
|
||||
picked: RefCell<Vec<FaceId>>,
|
||||
}
|
||||
|
||||
impl IdentityController {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Clear the multi-select.
|
||||
///
|
||||
/// Called on every person change: a pick set that survived navigating to
|
||||
/// another person would make the next "Split off" act on faces the user
|
||||
/// can no longer see, which is the kind of surprise that loses data.
|
||||
fn clear_picks(&self) {
|
||||
self.picked.borrow_mut().clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// Push the people rail and the face grid into the window.
|
||||
pub fn refresh(
|
||||
window: &AppWindow,
|
||||
ctl: &IdentityController,
|
||||
catalog: &Rc<RefCell<Option<Catalog>>>,
|
||||
store: Option<&ThumbStore>,
|
||||
) {
|
||||
let borrow = catalog.borrow();
|
||||
let Some(cat) = borrow.as_ref() else {
|
||||
window.set_identity_people(ModelRc::new(VecModel::from(Vec::<IdentityPerson>::new())));
|
||||
window.set_identity_faces(ModelRc::new(VecModel::from(Vec::<IdentityFace>::new())));
|
||||
return;
|
||||
};
|
||||
|
||||
let view = match identity::load_people(cat, MODEL_ID) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
log::warn!("identity: reading people: {e}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
window.set_identity_unassigned(view.unassigned as i32);
|
||||
window.set_identity_calibrated(view.calibrated);
|
||||
|
||||
let rows: Vec<IdentityPerson> = view
|
||||
.people
|
||||
.iter()
|
||||
.map(|p| IdentityPerson {
|
||||
id: p.id.0 as i32,
|
||||
label: p.display_name().into(),
|
||||
unconfirmed: p.is_unconfirmed(),
|
||||
confirmed_faces: p.confirmed_faces as i32,
|
||||
suggested_faces: p.suggested_faces as i32,
|
||||
cover: slint::Image::default(),
|
||||
has_cover: false,
|
||||
})
|
||||
.collect();
|
||||
window.set_identity_people(ModelRc::new(VecModel::from(rows)));
|
||||
*ctl.people.borrow_mut() = view.people;
|
||||
|
||||
// The selected person may have just been merged away or deleted.
|
||||
if let Some(sel) = ctl.selected.get() {
|
||||
if !ctl.people.borrow().iter().any(|p| p.id == sel) {
|
||||
ctl.selected.set(None);
|
||||
ctl.clear_picks();
|
||||
}
|
||||
}
|
||||
|
||||
match (ctl.selected.get(), store) {
|
||||
(Some(person), Some(store)) => {
|
||||
let cells = identity::load_faces(cat, store, person, view.calibrated)
|
||||
.unwrap_or_else(|e| {
|
||||
log::warn!("identity: reading faces: {e}");
|
||||
Vec::new()
|
||||
});
|
||||
push_faces(window, ctl, &cells);
|
||||
*ctl.faces.borrow_mut() = cells;
|
||||
|
||||
let name = ctl
|
||||
.people
|
||||
.borrow()
|
||||
.iter()
|
||||
.find(|p| p.id == person)
|
||||
.map(|p| p.name.clone())
|
||||
.unwrap_or_default();
|
||||
window.set_identity_selected(person.0 as i32);
|
||||
window.set_identity_selected_name(name.into());
|
||||
}
|
||||
_ => {
|
||||
window.set_identity_selected(-1);
|
||||
window.set_identity_selected_name(Default::default());
|
||||
window.set_identity_faces(ModelRc::new(VecModel::from(Vec::<IdentityFace>::new())));
|
||||
ctl.faces.borrow_mut().clear();
|
||||
}
|
||||
}
|
||||
|
||||
window.set_identity_picked(ctl.picked.borrow().len() as i32);
|
||||
}
|
||||
|
||||
fn push_faces(window: &AppWindow, ctl: &IdentityController, cells: &[FaceCell]) {
|
||||
let picked = ctl.picked.borrow();
|
||||
let rows: Vec<IdentityFace> = cells
|
||||
.iter()
|
||||
.map(|c| IdentityFace {
|
||||
id: c.face.0 as i32,
|
||||
crop: c
|
||||
.crop
|
||||
.as_ref()
|
||||
.map(|p| to_slint_image(p.width, p.height, &p.rgba))
|
||||
.unwrap_or_default(),
|
||||
has_crop: c.crop.is_some(),
|
||||
confirmed: c.confirmed,
|
||||
confidence: c.confidence_label().into(),
|
||||
crop_px: c.crop_px as i32,
|
||||
picked: picked.contains(&c.face),
|
||||
})
|
||||
.collect();
|
||||
window.set_identity_faces(ModelRc::new(VecModel::from(rows)));
|
||||
}
|
||||
|
||||
fn to_slint_image(width: u32, height: u32, rgba: &[u8]) -> slint::Image {
|
||||
let mut buf = slint::SharedPixelBuffer::<slint::Rgba8Pixel>::new(width, height);
|
||||
buf.make_mut_bytes().copy_from_slice(rgba);
|
||||
slint::Image::from_rgba8(buf)
|
||||
}
|
||||
|
||||
/// Attach every Identity callback.
|
||||
pub fn wire<S>(
|
||||
window: &AppWindow,
|
||||
ctl: Rc<IdentityController>,
|
||||
catalog: Rc<RefCell<Option<Catalog>>>,
|
||||
store: S,
|
||||
) where
|
||||
S: Fn() -> Option<Rc<ThumbStore>> + 'static,
|
||||
{
|
||||
let store: Rc<dyn Fn() -> Option<Rc<ThumbStore>>> = Rc::new(store);
|
||||
|
||||
// Re-read everything and redraw. Every mutating callback ends in this
|
||||
// rather than patching the model in place: the operations here have
|
||||
// second-order effects — a merge empties a person, a split creates one,
|
||||
// a rejection changes two counts — and a model patched by hand would
|
||||
// drift from the catalog in exactly the cases that matter.
|
||||
macro_rules! reload {
|
||||
($w:expr, $ctl:expr, $catalog:expr, $store:expr) => {
|
||||
refresh(&$w, &$ctl, &$catalog, $store().as_deref())
|
||||
};
|
||||
}
|
||||
|
||||
{
|
||||
let weak = window.as_weak();
|
||||
let ctl = ctl.clone();
|
||||
let catalog = catalog.clone();
|
||||
let store = store.clone();
|
||||
window.on_identity_open(move || {
|
||||
let Some(w) = weak.upgrade() else { return };
|
||||
w.set_show_identity(true);
|
||||
reload!(w, ctl, catalog, store);
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
let weak = window.as_weak();
|
||||
window.on_identity_close(move || {
|
||||
let Some(w) = weak.upgrade() else { return };
|
||||
w.set_show_identity(false);
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
let weak = window.as_weak();
|
||||
let ctl = ctl.clone();
|
||||
let catalog = catalog.clone();
|
||||
let store = store.clone();
|
||||
window.on_identity_person_picked(move |id| {
|
||||
let Some(w) = weak.upgrade() else { return };
|
||||
ctl.selected.set(Some(PersonId(id as u64)));
|
||||
ctl.clear_picks();
|
||||
reload!(w, ctl, catalog, store);
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
let weak = window.as_weak();
|
||||
let ctl = ctl.clone();
|
||||
let catalog = catalog.clone();
|
||||
let store = store.clone();
|
||||
window.on_identity_rename(move |name| {
|
||||
let Some(w) = weak.upgrade() else { return };
|
||||
let Some(person) = ctl.selected.get() else { return };
|
||||
if let Some(cat) = catalog.borrow().as_ref() {
|
||||
if let Err(e) = identity::rename(cat, person, &name) {
|
||||
log::warn!("identity: rename: {e}");
|
||||
}
|
||||
}
|
||||
reload!(w, ctl, catalog, store);
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
let weak = window.as_weak();
|
||||
let ctl = ctl.clone();
|
||||
let catalog = catalog.clone();
|
||||
let store = store.clone();
|
||||
window.on_identity_confirm_face(move |id| {
|
||||
let Some(w) = weak.upgrade() else { return };
|
||||
let Some(person) = ctl.selected.get() else { return };
|
||||
if let Some(cat) = catalog.borrow().as_ref() {
|
||||
if let Err(e) = identity::confirm(cat, FaceId(id as u64), person) {
|
||||
log::warn!("identity: confirm: {e}");
|
||||
}
|
||||
}
|
||||
reload!(w, ctl, catalog, store);
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
let weak = window.as_weak();
|
||||
let ctl = ctl.clone();
|
||||
let catalog = catalog.clone();
|
||||
let store = store.clone();
|
||||
window.on_identity_reject_face(move |id| {
|
||||
let Some(w) = weak.upgrade() else { return };
|
||||
let Some(person) = ctl.selected.get() else { return };
|
||||
if let Some(cat) = catalog.borrow().as_ref() {
|
||||
if let Err(e) = identity::reject(cat, FaceId(id as u64), person) {
|
||||
log::warn!("identity: reject: {e}");
|
||||
}
|
||||
}
|
||||
reload!(w, ctl, catalog, store);
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
let weak = window.as_weak();
|
||||
let ctl = ctl.clone();
|
||||
window.on_identity_toggle_pick(move |id| {
|
||||
let Some(w) = weak.upgrade() else { return };
|
||||
let face = FaceId(id as u64);
|
||||
{
|
||||
let mut picked = ctl.picked.borrow_mut();
|
||||
match picked.iter().position(|&f| f == face) {
|
||||
Some(i) => {
|
||||
picked.remove(i);
|
||||
}
|
||||
None => picked.push(face),
|
||||
}
|
||||
}
|
||||
// Only the pick flags changed, so this is the one case that does
|
||||
// not re-read the catalog: nothing in it moved.
|
||||
push_faces(&w, &ctl, &ctl.faces.borrow());
|
||||
w.set_identity_picked(ctl.picked.borrow().len() as i32);
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
let weak = window.as_weak();
|
||||
let ctl = ctl.clone();
|
||||
let catalog = catalog.clone();
|
||||
let store = store.clone();
|
||||
window.on_identity_confirm_all(move || {
|
||||
let Some(w) = weak.upgrade() else { return };
|
||||
let Some(person) = ctl.selected.get() else { return };
|
||||
if let Some(cat) = catalog.borrow().as_ref() {
|
||||
match identity::confirm_all(cat, person) {
|
||||
Ok(n) => log::info!("identity: confirmed {n} suggestion(s)"),
|
||||
Err(e) => log::warn!("identity: confirm all: {e}"),
|
||||
}
|
||||
}
|
||||
reload!(w, ctl, catalog, store);
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
let weak = window.as_weak();
|
||||
let ctl = ctl.clone();
|
||||
let catalog = catalog.clone();
|
||||
let store = store.clone();
|
||||
window.on_identity_split_picked(move || {
|
||||
let Some(w) = weak.upgrade() else { return };
|
||||
let Some(person) = ctl.selected.get() else { return };
|
||||
let members: Vec<FaceId> = ctl.picked.borrow().clone();
|
||||
if members.is_empty() {
|
||||
return;
|
||||
}
|
||||
if let Some(cat) = catalog.borrow().as_ref() {
|
||||
// Unnamed, so the user names it themselves — the same rule the
|
||||
// clustering pass follows. A split that guessed a name would
|
||||
// be presenting an inference as a fact (FR-CULL-10).
|
||||
match identity::split_off(cat, person, &members, "") {
|
||||
Ok(new) => log::info!(
|
||||
"identity: split {} face(s) onto person {:?}",
|
||||
members.len(),
|
||||
new
|
||||
),
|
||||
Err(e) => log::warn!("identity: split: {e}"),
|
||||
}
|
||||
}
|
||||
ctl.clear_picks();
|
||||
reload!(w, ctl, catalog, store);
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
let weak = window.as_weak();
|
||||
let ctl = ctl.clone();
|
||||
let catalog = catalog.clone();
|
||||
let store = store.clone();
|
||||
window.on_identity_recluster(move || {
|
||||
let Some(w) = weak.upgrade() else { return };
|
||||
if let Some(cat) = catalog.borrow().as_ref() {
|
||||
match crate::faces::recluster(cat, MODEL_ID, dr_face::DEFAULT_MERGE_PROBABILITY) {
|
||||
Ok((s, c)) => log::info!("identity: {s} suggestion(s), {c} new group(s)"),
|
||||
Err(e) => log::warn!("identity: recluster: {e}"),
|
||||
}
|
||||
}
|
||||
reload!(w, ctl, catalog, store);
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
let weak = window.as_weak();
|
||||
let ctl = ctl.clone();
|
||||
let catalog = catalog.clone();
|
||||
let store = store.clone();
|
||||
window.on_identity_delete_all(move || {
|
||||
let Some(w) = weak.upgrade() else { return };
|
||||
if let Some(cat) = catalog.borrow().as_ref() {
|
||||
match identity::delete_all(cat) {
|
||||
Ok(n) => log::info!("identity: deleted {n} face(s) and every person"),
|
||||
Err(e) => log::warn!("identity: delete all: {e}"),
|
||||
}
|
||||
}
|
||||
ctl.selected.set(None);
|
||||
ctl.clear_picks();
|
||||
reload!(w, ctl, catalog, store);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_pick_toggles_on_and_off() {
|
||||
let ctl = IdentityController::new();
|
||||
let f = FaceId(7);
|
||||
ctl.picked.borrow_mut().push(f);
|
||||
assert_eq!(ctl.picked.borrow().len(), 1);
|
||||
let pos = ctl.picked.borrow().iter().position(|&x| x == f);
|
||||
assert_eq!(pos, Some(0));
|
||||
ctl.picked.borrow_mut().remove(0);
|
||||
assert!(ctl.picked.borrow().is_empty());
|
||||
}
|
||||
|
||||
/// Changing person must not carry a stale pick set: a later "Split off"
|
||||
/// would otherwise act on faces the user can no longer see.
|
||||
#[test]
|
||||
fn changing_person_clears_the_pick_set() {
|
||||
let ctl = IdentityController::new();
|
||||
ctl.picked.borrow_mut().push(FaceId(1));
|
||||
ctl.picked.borrow_mut().push(FaceId(2));
|
||||
ctl.selected.set(Some(PersonId(1)));
|
||||
|
||||
ctl.selected.set(Some(PersonId(2)));
|
||||
ctl.clear_picks();
|
||||
assert!(ctl.picked.borrow().is_empty());
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,8 @@ mod activity;
|
||||
mod collections_ui;
|
||||
mod derived_sync;
|
||||
pub mod faces;
|
||||
pub mod identity;
|
||||
mod identity_ui;
|
||||
mod develop;
|
||||
mod export;
|
||||
mod gradient;
|
||||
@@ -871,6 +873,9 @@ pub fn run(paths: Vec<PathBuf>) -> Result<()> {
|
||||
// needs it too: a paste onto "the selection" reads the selection from
|
||||
// here, and that wiring happens once the develop session exists.
|
||||
let collections = collections_ui::CollectionsController::new(activity.clone());
|
||||
// The People screen's own state: which person is open, and which of their
|
||||
// faces are ticked for a split.
|
||||
let identity = std::rc::Rc::new(identity_ui::IdentityController::new());
|
||||
|
||||
// Launch screen: shown when there is nothing to display — no local paths
|
||||
// and no configured library. A user who has already signed in and chosen
|
||||
@@ -948,6 +953,26 @@ pub fn run(paths: Vec<PathBuf>) -> Result<()> {
|
||||
// and thumbnail workers use.
|
||||
move || lib_session.session(),
|
||||
);
|
||||
|
||||
// The People screen reads the same catalog and the same thumbnail
|
||||
// store the grid does — its face crops come from the proxies the
|
||||
// grid already built, which is the whole reason face indexing is
|
||||
// affordable (FR-CULL-8).
|
||||
let lib_store = library.clone();
|
||||
identity_ui::wire(
|
||||
&window,
|
||||
identity.clone(),
|
||||
library.catalog(),
|
||||
move || {
|
||||
let (_, session) = lib_store.session()?;
|
||||
dr_thumbs::ThumbStore::open(&library::thumbs_dir(
|
||||
&session.server,
|
||||
&session.user_id,
|
||||
))
|
||||
.ok()
|
||||
.map(std::rc::Rc::new)
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
let weak = window.as_weak();
|
||||
|
||||
Reference in New Issue
Block a user