From fcd4459e6ddf703efb720a32e62402200752a363 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Sat, 18 May 2024 07:49:15 +0530 Subject: [PATCH] Move currently unused cluster stuff out of the way --- web/apps/photos/src/services/face/people.ts | 58 +++++++++++-------- .../machineLearning/machineLearningService.ts | 23 -------- 2 files changed, 35 insertions(+), 46 deletions(-) diff --git a/web/apps/photos/src/services/face/people.ts b/web/apps/photos/src/services/face/people.ts index 55281768c..0c66bcc57 100644 --- a/web/apps/photos/src/services/face/people.ts +++ b/web/apps/photos/src/services/face/people.ts @@ -1,37 +1,53 @@ import log from "@/next/log"; import mlIDbStorage from "services/face/db"; -import { Face, Person } from "services/face/types"; -import { type MLSyncContext } from "services/machineLearning/machineLearningService"; +import { Person } from "services/face/types"; import { clusterFaces } from "./cluster"; import { saveFaceCrop } from "./f-index"; import { fetchImageBitmap, getLocalFile } from "./image"; -export const syncPeopleIndex = async (syncContext: MLSyncContext) => { +export const syncPeopleIndex = async () => { + // TODO-ML(MR): Forced disable clustering. It doesn't currently work, + // need to finalize it before we move out of beta. + // + // > Error: Failed to execute 'transferToImageBitmap' on + // > 'OffscreenCanvas': ImageBitmap construction failed + /* + if ( + syncContext.outOfSyncFiles.length <= 0 || + (syncContext.nSyncedFiles === batchSize && Math.random() < 0) + ) { + await this.syncIndex(syncContext); + } + + public async syncIndex(syncContext: MLSyncContext) { + await this.getMLLibraryData(syncContext); + + // TODO-ML(MR): Ensure this doesn't run until fixed. + await syncPeopleIndex(syncContext); + + await this.persistMLLibraryData(syncContext); + } + const filesVersion = await mlIDbStorage.getIndexVersion("files"); if (filesVersion <= (await mlIDbStorage.getIndexVersion("people"))) { return; } + */ // TODO: have faces addresable through fileId + faceId // to avoid index based addressing, which is prone to wrong results // one way could be to match nearest face within threshold in the file + /* const allFacesMap = syncContext.allSyncedFacesMap ?? (syncContext.allSyncedFacesMap = await mlIDbStorage.getAllFacesMap()); - const allFaces = [...allFacesMap.values()].flat(); + */ - await runFaceClustering(syncContext, allFaces); - await syncPeopleFromClusters(syncContext, allFacesMap, allFaces); - - await mlIDbStorage.setIndexVersion("people", filesVersion); -}; - -const runFaceClustering = async ( - syncContext: MLSyncContext, - allFaces: Array, -) => { // await this.init(); + const allFacesMap = await mlIDbStorage.getAllFacesMap(); + const allFaces = [...allFacesMap.values()].flat(); + if (!allFaces || allFaces.length < 50) { log.info( `Skipping clustering since number of faces (${allFaces.length}) is less than the clustering threshold (50)`, @@ -40,21 +56,15 @@ const runFaceClustering = async ( } log.info("Running clustering allFaces: ", allFaces.length); - syncContext.mlLibraryData.faceClusteringResults = await clusterFaces( + const faceClusteringResults = await clusterFaces( allFaces.map((f) => Array.from(f.embedding)), ); log.info( "[MLService] Got face clustering results: ", - JSON.stringify(syncContext.mlLibraryData.faceClusteringResults), + JSON.stringify(faceClusteringResults), ); -}; -const syncPeopleFromClusters = async ( - syncContext: MLSyncContext, - allFacesMap: Map>, - allFaces: Array, -) => { - const clusters = syncContext.mlLibraryData.faceClusteringResults?.clusters; + const clusters = faceClusteringResults?.clusters; if (!clusters || clusters.length < 1) { return; } @@ -95,4 +105,6 @@ const syncPeopleFromClusters = async ( } await mlIDbStorage.updateFaces(allFacesMap); + + // await mlIDbStorage.setIndexVersion("people", filesVersion); }; diff --git a/web/apps/photos/src/services/machineLearning/machineLearningService.ts b/web/apps/photos/src/services/machineLearning/machineLearningService.ts index 3703f1dfd..c57866f16 100644 --- a/web/apps/photos/src/services/machineLearning/machineLearningService.ts +++ b/web/apps/photos/src/services/machineLearning/machineLearningService.ts @@ -165,20 +165,6 @@ class MachineLearningService { await this.syncFiles(syncContext); } - // TODO-ML(MR): Forced disable clustering. It doesn't currently work, - // need to finalize it before we move out of beta. - // - // > Error: Failed to execute 'transferToImageBitmap' on - // > 'OffscreenCanvas': ImageBitmap construction failed - /* - if ( - syncContext.outOfSyncFiles.length <= 0 || - (syncContext.nSyncedFiles === batchSize && Math.random() < 0) - ) { - await this.syncIndex(syncContext); - } - */ - const error = syncContext.error; const nOutOfSyncFiles = syncContext.outOfSyncFiles.length; return !error && nOutOfSyncFiles > 0; @@ -491,15 +477,6 @@ class MachineLearningService { private async persistMLLibraryData(syncContext: MLSyncContext) { return mlIDbStorage.putLibraryData(syncContext.mlLibraryData); } - - public async syncIndex(syncContext: MLSyncContext) { - await this.getMLLibraryData(syncContext); - - // TODO-ML(MR): Ensure this doesn't run until fixed. - await syncPeopleIndex(syncContext); - - await this.persistMLLibraryData(syncContext); - } } export default new MachineLearningService();