Skip to content

armgit / ente

Public Imported from GitHub
0

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

sort by photo date

Manav Rathi30d5fedb11d9
diff --git a/web/packages/new/photos/services/ml/cluster.ts b/web/packages/new/photos/services/ml/cluster.ts
index 254988dea9..0b7de59b59 100644
--- a/web/packages/new/photos/services/ml/cluster.ts
+++ b/web/packages/new/photos/services/ml/cluster.ts
@@ -1,6 +1,8 @@
+import { assertionFailed } from "@/base/assert";
import { newNonSecureID } from "@/base/id-worker";
import log from "@/base/log";
import { ensure } from "@/utils/ensure";
+import type { EnteFile } from "../../types/file";
import { type EmbeddingCluster, clusterHdbscan } from "./cluster-hdb";
import type { Face, FaceIndex } from "./face";
import { dotProduct } from "./math";
@@ -175,6 +177,7 @@ export interface ClusterPreviewFace {
*/
export const clusterFaces = (
faceIndexes: FaceIndex[],
+ localFiles: EnteFile[],
opts: ClusteringOpts,
) => {
const {
@@ -187,13 +190,33 @@ export const clusterFaces = (
} = opts;
const t = Date.now();
+ const localFileByID = new Map(localFiles.map((f) => [f.id, f]));
+
// A flattened array of faces.
const allFaces = [...enumerateFaces(faceIndexes)];
- const faces = allFaces
+ const filteredFaces = allFaces
.filter((f) => f.blur > minBlur)
.filter((f) => f.score > minScore);
// .slice(0, 2000);
+ const fileForFaceID = new Map(
+ filteredFaces.map(({ faceID }) => [
+ faceID,
+ ensure(localFileByID.get(ensure(fileIDFromFaceID(faceID)))),
+ ]),
+ );
+
+ const fileForFace = ({ faceID }: Face) => ensure(fileForFaceID.get(faceID));
+
+ // Sort faces so that photos taken temporally close together are close.
+ //
+ // This is a heuristic to help the clustering algorithm produce better results.
+ const faces = filteredFaces.sort(
+ (a, b) =>
+ fileForFace(a).metadata.creationTime -
+ fileForFace(b).metadata.creationTime,
+ );
+
// For fast reverse lookup - map from face ids to the face.
const faceForFaceID = new Map(faces.map((f) => [f.faceID, f]));
@@ -382,6 +405,7 @@ export const clusterFaces = (
filteredFaceCount,
clusteredFaceCount,
unclusteredFaceCount,
+ localFileByID,
clusterPreviews,
clusters: sortedClusters,
cgroups,
@@ -402,6 +426,21 @@ function* enumerateFaces(faceIndices: FaceIndex[]) {
}
}
+/**
+ * Extract the fileID of the {@link EnteFile} to which the face belongs from its
+ * faceID.
+ *
+ * TODO-Cluster - duplicated with ml/index.ts
+ */
+const fileIDFromFaceID = (faceID: string) => {
+ const fileID = parseInt(faceID.split("_")[0] ?? "");
+ if (isNaN(fileID)) {
+ assertionFailed(`Ignoring attempt to parse invalid faceID ${faceID}`);
+ return undefined;
+ }
+ return fileID;
+};
+
interface ClusterLinearResult {
clusters: EmbeddingCluster[];
}
diff --git a/web/packages/new/photos/services/ml/index.ts b/web/packages/new/photos/services/ml/index.ts
index 3bb6bb3eaf..37d961b165 100644
--- a/web/packages/new/photos/services/ml/index.ts
+++ b/web/packages/new/photos/services/ml/index.ts
@@ -16,7 +16,6 @@ import { ensure } from "@/utils/ensure";
import { throttled } from "@/utils/promise";
import { proxy, transfer } from "comlink";
import { isInternalUser } from "../feature-flags";
-import { getAllLocalFiles } from "../files";
import { getRemoteFlag, updateRemoteFlag } from "../remote-store";
import type { SearchPerson } from "../search/types";
import type { UploadItem } from "../upload/types";
@@ -381,11 +380,15 @@ export const wipClusterDebugPageContents = async (
_wip_searchPersons = undefined;
triggerStatusUpdate();
- const { clusterPreviews, clusters, cgroups, unclusteredFaces, ...rest } =
- await worker().then((w) => w.clusterFaces(opts));
+ const {
+ localFileByID,
+ clusterPreviews,
+ clusters,
+ cgroups,
+ unclusteredFaces,
+ ...rest
+ } = await worker().then((w) => w.clusterFaces(opts));
- const localFiles = await getAllLocalFiles();
- const localFileByID = new Map(localFiles.map((f) => [f.id, f]));
const fileForFace = ({ faceID }: Face) =>
ensure(localFileByID.get(ensure(fileIDFromFaceID(faceID))));
diff --git a/web/packages/new/photos/services/ml/worker.ts b/web/packages/new/photos/services/ml/worker.ts
index c663abc2c9..391dc672f7 100644
--- a/web/packages/new/photos/services/ml/worker.ts
+++ b/web/packages/new/photos/services/ml/worker.ts
@@ -277,7 +277,11 @@ export class MLWorker {
// TODO-Cluster
async clusterFaces(opts: ClusteringOpts) {
- return clusterFaces(await faceIndexes(), opts);
+ return clusterFaces(
+ await faceIndexes(),
+ await getAllLocalFiles(),
+ opts,
+ );
}
}