Skip to content

armgit / ente

Public Imported from GitHub
0

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

Funnel the same way

Manav Rathied1c9df007ad
diff --git a/web/packages/new/photos/services/ml/cgroups.ts b/web/packages/new/photos/services/ml/cgroups.ts
index d6c3e0fbaa..e7316e11f6 100644
--- a/web/packages/new/photos/services/ml/cgroups.ts
+++ b/web/packages/new/photos/services/ml/cgroups.ts
@@ -1,10 +1,11 @@
import { masterKeyFromSession } from "@/base/session-store";
-import { fileIDFromFaceID, wipClusterEnable } from ".";
+import { wipClusterEnable } from ".";
import type { EnteFile } from "../../types/file";
import { getLocalFiles } from "../files";
import { pullCGroups } from "../user-entity";
import type { FaceCluster } from "./cluster";
import { getClusterGroups, getFaceIndexes } from "./db";
+import { fileIDFromFaceID } from "./face";
/**
* A cgroup ("cluster group") is a group of clusters (possibly containing just a
@@ -78,12 +79,15 @@ export interface CGroup {
}
/**
- * A massaged version of {@link CGroup} suitable for being shown in the UI.
+ * A massaged version of {@link CGroup} or a {@link FaceCluster} suitable for
+ * being shown in the UI.
+ *
+ * We transform both both remote cluster groups and local-only face clusters
+ * into the same "person" object that can be shown in the UI.
*
* The cgroups synced with remote do not directly correspond to "people".
- * CGroups represent both positive and negative feedback, where the negations
- * are specifically feedback meant so that we do not show the corresponding
- * cluster in the UI.
+ * CGroups represent both positive and negative feedback (i.e, the user does not
+ * wish a particular cluster group to be shown in the UI).
*
* So while each person has an underlying cgroups, not all cgroups have a
* corresponding person.
@@ -95,13 +99,15 @@ export interface CGroup {
*/
export interface Person {
/**
- * Nanoid of the underlying {@link CGroup}.
+ * Nanoid of the underlying {@link CGroup} or {@link FaceCluster}.
*/
id: string;
/**
* The name of the person.
+ *
+ * This will only be set for named cgroups.
*/
- name: string;
+ name: string | undefined;
/**
* IDs of the (unique) files in which this face occurs.
*/
@@ -117,22 +123,6 @@ export interface Person {
displayFaceFile: EnteFile;
}
-// TODO-Cluster remove me
-/**
- * A {@link CGroup} annotated with various in-memory state to make it easier for
- * the upper layers of our code to directly use it.
- */
-export type AnnotatedCGroup = CGroup & {
- /**
- * Locally determined ID of the "best" face that should be used as the
- * display face, to represent this cluster group in the UI.
- *
- * This property is not synced with remote. For more details, see
- * {@link avatarFaceID}.
- */
- displayFaceID: string | undefined;
-};
-
/**
* Fetch existing cgroups for the user from remote and save them to DB.
*/
diff --git a/web/packages/new/photos/services/ml/cluster.ts b/web/packages/new/photos/services/ml/cluster.ts
index ffe631da97..4dabad3907 100644
--- a/web/packages/new/photos/services/ml/cluster.ts
+++ b/web/packages/new/photos/services/ml/cluster.ts
@@ -1,11 +1,15 @@
-import { assertionFailed } from "@/base/assert";
import { newNonSecureID } from "@/base/id-worker";
import log from "@/base/log";
import { ensure } from "@/utils/ensure";
import { wait } from "@/utils/promise";
import type { EnteFile } from "../../types/file";
-import type { AnnotatedCGroup } from "./cgroups";
-import { faceDirection, type Face, type FaceIndex } from "./face";
+import type { Person } from "./cgroups";
+import {
+ faceDirection,
+ fileIDFromFaceID,
+ type Face,
+ type FaceIndex,
+} from "./face";
import { dotProduct } from "./math";
/**
@@ -236,21 +240,6 @@ const isSidewaysFace = (face: Face) =>
/** Generate a new cluster ID. */
const newClusterID = () => newNonSecureID("cluster_");
-/**
- * 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 ClusteringState {
clusterIDForFaceID: Map<string, string>;
clusterIndexForFaceID: Map<string, number>;
@@ -344,56 +333,35 @@ const toPeople = (
clusters: FaceCluster[],
localFileByID: Map<number, EnteFile>,
faceForFaceID: Map<string, ClusterFace>,
-) => {
- // TODO-Cluster - Currently we're not syncing with remote or saving anything
- // locally, so cgroups will be empty. Create a temporary (unsaved, unsynced)
- // cgroup, one per cluster.
-
- const cgroups: AnnotatedCGroup[] = [];
- for (const cluster of clusters) {
- const faces = cluster.faces.map((id) => ensure(faceForFaceID.get(id)));
- const topFace = faces.reduce((top, face) =>
- top.score > face.score ? top : face,
- );
- cgroups.push({
- id: cluster.id,
- name: undefined,
- assigned: [cluster],
- isHidden: false,
- avatarFaceID: undefined,
- displayFaceID: topFace.faceID,
- });
- }
+): Person[] =>
+ clusters
+ .map((cluster) => {
+ const faces = cluster.faces.map((id) =>
+ ensure(faceForFaceID.get(id)),
+ );
- const clusterByID = new Map(clusters.map((c) => [c.id, c]));
+ const faceIDs = cluster.faces;
+ const fileIDs = faceIDs.map((faceID) =>
+ ensure(fileIDFromFaceID(faceID)),
+ );
- const people = cgroups
- // TODO-Cluster
- .map((cgroup) => ({ ...cgroup, name: cgroup.id }))
- .map((cgroup) => {
- if (!cgroup.name) return undefined;
- const faceID = ensure(cgroup.displayFaceID);
- const fileID = ensure(fileIDFromFaceID(faceID));
- const file = ensure(localFileByID.get(fileID));
+ const topFace = faces.reduce((top, face) =>
+ top.score > face.score ? top : face,
+ );
- const faceIDs = cgroup.assigned
- .map(({ id }) => ensure(clusterByID.get(id)))
- .flatMap((cluster) => cluster.faces);
- const fileIDs = faceIDs
- .map((faceID) => fileIDFromFaceID(faceID))
- .filter((fileID) => fileID !== undefined);
+ const displayFaceID = topFace.faceID;
+ const displayFaceFileID = ensure(fileIDFromFaceID(displayFaceID));
+ const displayFaceFile = ensure(
+ localFileByID.get(displayFaceFileID),
+ );
return {
- id: cgroup.id,
- name: cgroup.name,
+ id: cluster.id,
+ name: undefined,
faceIDs,
fileIDs: [...new Set(fileIDs)],
- displayFaceID: faceID,
- displayFaceFile: file,
+ displayFaceID,
+ displayFaceFile,
};
})
- .filter((c) => !!c)
.sort((a, b) => b.faceIDs.length - a.faceIDs.length);
-
- return people;
-};
diff --git a/web/packages/new/photos/services/ml/face.ts b/web/packages/new/photos/services/ml/face.ts
index 80b11853c8..0de4c6c6b4 100644
--- a/web/packages/new/photos/services/ml/face.ts
+++ b/web/packages/new/photos/services/ml/face.ts
@@ -7,6 +7,7 @@
//
/* eslint-disable @typescript-eslint/no-non-null-assertion */
+import { assertionFailed } from "@/base/assert";
import type { ElectronMLWorker } from "@/base/types/ipc";
import type { EnteFile } from "@/new/photos/types/file";
import { Matrix } from "ml-matrix";
@@ -149,7 +150,7 @@ export interface Face {
* Finally, this face ID is not completely opaque. It consists of underscore
* separated components, the first of which is the ID of the
* {@link EnteFile} to which this face belongs. Client code can rely on this
- * structure and can parse it if needed.
+ * structure and can parse it if needed using {@link fileIDFromFaceID}.
*/
faceID: string;
/**
@@ -228,6 +229,19 @@ export interface Box {
height: number;
}
+/**
+ * Extract the fileID of the {@link EnteFile} to which the face belongs from its
+ * faceID.
+ */
+export 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;
+};
+
/**
* Index faces in the given file.
*
diff --git a/web/packages/new/photos/services/ml/index.ts b/web/packages/new/photos/services/ml/index.ts
index 1d84ae7500..df70bbe30d 100644
--- a/web/packages/new/photos/services/ml/index.ts
+++ b/web/packages/new/photos/services/ml/index.ts
@@ -3,7 +3,6 @@
*/
import { isDesktop } from "@/base/app";
-import { assertionFailed } from "@/base/assert";
import { blobCache } from "@/base/blob-cache";
import { ensureElectron } from "@/base/electron";
import { isDevBuild } from "@/base/env";
@@ -598,20 +597,6 @@ export const unidentifiedFaceIDs = async (
return index?.faces.map((f) => f.faceID) ?? [];
};
-/**
- * Extract the fileID of the {@link EnteFile} to which the face belongs from its
- * faceID.
- */
-// TODO-Cluster
-export 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;
-};
-
/**
* Return the cached face crop for the given face, regenerating it if needed.
*