From 7623e69de394da783418d80764646fc4a2567c31 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Fri, 10 May 2024 13:07:24 +0530 Subject: [PATCH] [mob] Return list instead of set --- mobile/lib/face/db.dart | 85 ++++++++++--------- .../face_ml/face_ml_service.dart | 16 ++-- 2 files changed, 52 insertions(+), 49 deletions(-) diff --git a/mobile/lib/face/db.dart b/mobile/lib/face/db.dart index 2fd1b6e1d..b2173552d 100644 --- a/mobile/lib/face/db.dart +++ b/mobile/lib/face/db.dart @@ -511,56 +511,61 @@ class FaceMLDataDB { }); } - Future> getFaceInfoForClustering({ + Future> getFaceInfoForClustering({ double minScore = kMinimumQualityFaceScore, int minClarity = kLaplacianHardThreshold, int maxFaces = 20000, int offset = 0, int batchSize = 10000, }) async { - final EnteWatch w = EnteWatch("getFaceEmbeddingMap")..start(); - w.logAndReset( - 'reading as float offset: $offset, maxFaces: $maxFaces, batchSize: $batchSize', - ); - final db = await instance.asyncDB; - - final Set result = {}; - while (true) { - // Query a batch of rows - final List> maps = await db.getAll( - 'SELECT $faceIDColumn, $faceEmbeddingBlob, $faceScore, $faceBlur, $isSideways FROM $facesTable' - ' WHERE $faceScore > $minScore AND $faceBlur > $minClarity' - ' ORDER BY $faceIDColumn' - ' DESC LIMIT $batchSize OFFSET $offset', + try { + final EnteWatch w = EnteWatch("getFaceEmbeddingMap")..start(); + w.logAndReset( + 'reading as float offset: $offset, maxFaces: $maxFaces, batchSize: $batchSize', ); - // Break the loop if no more rows - if (maps.isEmpty) { - break; - } - final List faceIds = []; - for (final map in maps) { - faceIds.add(map[faceIDColumn] as String); - } - final faceIdToClusterId = await getFaceIdsToClusterIds(faceIds); - for (final map in maps) { - final faceID = map[faceIDColumn] as String; - final faceInfo = FaceInfoForClustering( - faceID: faceID, - clusterId: faceIdToClusterId[faceID], - embeddingBytes: map[faceEmbeddingBlob] as Uint8List, - faceScore: map[faceScore] as double, - blurValue: map[faceBlur] as double, - isSideways: (map[isSideways] as int) == 1, + final db = await instance.asyncDB; + + final List result = {}; + while (true) { + // Query a batch of rows + final List> maps = await db.getAll( + 'SELECT $faceIDColumn, $faceEmbeddingBlob, $faceScore, $faceBlur, $isSideways FROM $facesTable' + ' WHERE $faceScore > $minScore AND $faceBlur > $minClarity' + ' ORDER BY $faceIDColumn' + ' DESC LIMIT $batchSize OFFSET $offset', ); - result.add(faceInfo); + // Break the loop if no more rows + if (maps.isEmpty) { + break; + } + final List faceIds = []; + for (final map in maps) { + faceIds.add(map[faceIDColumn] as String); + } + final faceIdToClusterId = await getFaceIdsToClusterIds(faceIds); + for (final map in maps) { + final faceID = map[faceIDColumn] as String; + final faceInfo = FaceInfoForClustering( + faceID: faceID, + clusterId: faceIdToClusterId[faceID], + embeddingBytes: map[faceEmbeddingBlob] as Uint8List, + faceScore: map[faceScore] as double, + blurValue: map[faceBlur] as double, + isSideways: (map[isSideways] as int) == 1, + ); + result.add(faceInfo); + } + if (result.length >= maxFaces) { + break; + } + offset += batchSize; } - if (result.length >= maxFaces) { - break; - } - offset += batchSize; + w.stopWithLog('done reading face embeddings ${result.length}'); + return result; + } catch (e) { + _logger.severe('Error in getFaceInfoForClustering', e); + rethrow; } - w.stopWithLog('done reading face embeddings ${result.length}'); - return result; } /// Returns a map of faceID to record of clusterId and faceEmbeddingBlob diff --git a/mobile/lib/services/machine_learning/face_ml/face_ml_service.dart b/mobile/lib/services/machine_learning/face_ml/face_ml_service.dart index b39a09af1..d36015818 100644 --- a/mobile/lib/services/machine_learning/face_ml/face_ml_service.dart +++ b/mobile/lib/services/machine_learning/face_ml/face_ml_service.dart @@ -71,7 +71,9 @@ class FaceMlService { // singleton pattern FaceMlService._privateConstructor(); + static final instance = FaceMlService._privateConstructor(); + factory FaceMlService() => instance; final _initLock = Lock(); @@ -302,19 +304,15 @@ class FaceMlService { // Get a sense of the total number of faces in the database final int totalFaces = await FaceMLDataDB.instance .getTotalFaceCount(minFaceScore: minFaceScore); - - // read the creation times from Files DB, in a map from fileID to creation time final fileIDToCreationTime = await FilesDB.instance.getFileIDToCreationTime(); - final startEmbeddingFetch = DateTime.now(); // read all embeddings - final allFaceInfoForClustering = await FaceMLDataDB.instance - .getFaceInfoForClustering( - minScore: minFaceScore, - maxFaces: totalFaces, - ) - .then((set) => set.toList()); + final allFaceInfoForClustering = + await FaceMLDataDB.instance.getFaceInfoForClustering( + minScore: minFaceScore, + maxFaces: totalFaces, + ); // sort the embeddings based on file creation time, oldest first allFaceInfoForClustering.sort((a, b) { final aFileId = getFileIdFromFaceId(a.faceID);