Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Use

Manav Rathia3c2bf73ae65
diff --git a/web/packages/new/photos/services/ml/kvdb.ts b/web/packages/new/photos/services/ml/kvdb.ts
index 101df13144..438b6020e2 100644
--- a/web/packages/new/photos/services/ml/kvdb.ts
+++ b/web/packages/new/photos/services/ml/kvdb.ts
@@ -1,18 +1,22 @@
import { getKV, setKV } from "@/base/kv";
import { z } from "zod";
-/** cgroupID -> clusterIDs */
+/**
+ * Zod schema for {@link ClusterIDsByCGroupID}.
+ */
const ClusterIDsByCGroupID = z.record(z.string(), z.array(z.string()));
/**
+ * cgroupID -> clusterIDs
+ *
* A record containing an array of clusters (represented by their IDs), keyed by
* the corresponding cgroup ID.
*/
export type ClusterIDsByCGroupID = z.infer<typeof ClusterIDsByCGroupID>;
/**
- * Return the list of locally persisted clusters that the user has rejected as
- * not belonging to a particular person.
+ * Return the locally persisted record of clusters per person that the user has
+ * rejected as not belonging to that person.
*
* [Note: Persistent KV pairs related to ML]
*
@@ -30,5 +34,15 @@ export const savedRejectedClusters = async (): Promise<ClusterIDsByCGroupID> =>
*
* Sibling of {@link savedRejectedClusters}.
*/
-export const saveRejectedClusters = (entries: ClusterIDsByCGroupID[]) =>
+export const saveRejectedClusters = (entries: ClusterIDsByCGroupID) =>
setKV("rejectedClusters", entries);
+
+/**
+ * Return the list of locally persisted clusters that the user has rejected as
+ * not belonging to a particular person (as identified by the person's
+ * {@link cgroupID}).
+ */
+export const savedRejectedClustersForCGroup = async (
+ cgroupID: string,
+): Promise<string[]> =>
+ savedRejectedClusters().then((cs) => cs[cgroupID] ?? []);
diff --git a/web/packages/new/photos/services/ml/people.ts b/web/packages/new/photos/services/ml/people.ts
index 422d9d72c6..f6fca83fd8 100644
--- a/web/packages/new/photos/services/ml/people.ts
+++ b/web/packages/new/photos/services/ml/people.ts
@@ -8,6 +8,7 @@ import { savedCGroups, type CGroup } from "../user-entity";
import { type FaceCluster } from "./cluster";
import { savedFaceClusters, savedFaceIndexes } from "./db";
import { fileIDFromFaceID } from "./face";
+import { savedRejectedClustersForCGroup } from "./kvdb";
import { dotProduct } from "./math";
/**
@@ -366,8 +367,10 @@ export const _suggestionsAndChoicesForPerson = async (
const startTime = Date.now();
const personClusters = person.cgroup.data.assigned;
- // TODO-Cluster: Persist this.
- const ignoredClusters: FaceCluster[] = [];
+ const personClusterIDs = new Set(personClusters.map(({ id }) => id));
+ const rejectedClusterIDs = new Set(
+ await savedRejectedClustersForCGroup(person.cgroup.id),
+ );
const clusters = await savedFaceClusters();
const faceIndexes = await savedFaceIndexes();
@@ -382,9 +385,6 @@ export const _suggestionsAndChoicesForPerson = async (
.flat(),
);
- const personClusterIDs = new Set(personClusters.map(({ id }) => id));
- const ignoredClusterIDs = new Set(ignoredClusters.map(({ id }) => id));
-
const personFaceEmbeddings = personClusters
.map(({ faces }) => faces.map((id) => embeddingByFaceID.get(id)))
.flat()
@@ -394,13 +394,22 @@ export const _suggestionsAndChoicesForPerson = async (
const sampledPersonEmbeddings = randomSample(personFaceEmbeddings, 50);
const candidateClustersAndSimilarity: [FaceCluster, number][] = [];
+ const rejectedClusters: FaceCluster[] = [];
for (const cluster of clusters) {
const { id, faces } = cluster;
+ // Ignore singleton clusters.
if (faces.length < 2) continue;
+ // Already part of the person.
if (personClusterIDs.has(id)) continue;
- if (ignoredClusterIDs.has(id)) continue;
+
+ // User has explicitly asked us to ignore this cluster. Add it to the
+ // list of rejected clusters that we return to the UI for listing out.
+ if (rejectedClusterIDs.has(id)) {
+ rejectedClusters.push(cluster);
+ continue;
+ }
const sampledOtherEmbeddings = randomSample(faces, 50)
.map((id) => embeddingByFaceID.get(id))
@@ -472,7 +481,7 @@ export const _suggestionsAndChoicesForPerson = async (
sortBySize(acceptedChoices);
- const ignoredChoices = toPreviewableList(ignoredClusters).map((p) => ({
+ const ignoredChoices = toPreviewableList(rejectedClusters).map((p) => ({
...p,
accepted: false,
}));