π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mobile][photos] Filter unreliable face suggestions
laurenspriema9c5df6f6f9c
diff --git a/mobile/apps/photos/lib/db/ml/base.dart b/mobile/apps/photos/lib/db/ml/base.dartindex 08f3d157be..a5c70a08c3 100644--- a/mobile/apps/photos/lib/db/ml/base.dart+++ b/mobile/apps/photos/lib/db/ml/base.dart@@ -15,6 +15,8 @@ abstract class IMLDataDB<T> {Future<Map<int, int>> faceIndexedFileIds({int minimumMlVersion});Future<int> getFaceIndexedFileCount({int minimumMlVersion});Future<Map<String, int>> clusterIdToFaceCount();+ Future<Set<String>> getBadFaceSingletonClusterIDs();+ Future<Set<String>> getClustersWithThreeOrMoreNotPersonFeedback();Future<Set<String>> getPersonIgnoredClusters(String personID);Future<Map<String, Set<String>>> getPersonToRejectedSuggestions();Future<Set<String>> getPersonClusterIDs(String personID);diff --git a/mobile/apps/photos/lib/db/ml/db.dart b/mobile/apps/photos/lib/db/ml/db.dartindex 60faba8ec9..c9480bdaea 100644--- a/mobile/apps/photos/lib/db/ml/db.dart+++ b/mobile/apps/photos/lib/db/ml/db.dart@@ -502,6 +502,41 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {return result;}+ @override+ Future<Set<String>> getBadFaceSingletonClusterIDs() async {+ final db = await asyncDB;+ final rows = await db.getAll('''+ SELECT fc.$clusterIDColumn, f.$faceScore, f.$faceBlur, f.$isSideways+ FROM $faceClustersTable fc+ INNER JOIN $facesTable f+ ON fc.$faceIDColumn = f.$faceIDColumn+ GROUP BY fc.$clusterIDColumn+ HAVING COUNT(*) = 1+ ''');+ return rows+ .where(+ (row) => isBadFaceForClustering(+ faceScore: row[faceScore] as double,+ blurValue: row[faceBlur] as double,+ isSideways: (row[isSideways] as int) == 1,+ ),+ )+ .map((row) => row[clusterIDColumn] as String)+ .toSet();+ }++ @override+ Future<Set<String>> getClustersWithThreeOrMoreNotPersonFeedback() async {+ final db = await asyncDB;+ final rows = await db.getAll('''+ SELECT $clusterIDColumn+ FROM $notPersonFeedback+ GROUP BY $clusterIDColumn+ HAVING COUNT(DISTINCT $personIdColumn) >= 3+ ''');+ return rows.map((row) => row[clusterIDColumn] as String).toSet();+ }+@overrideFuture<Set<String>> getPersonIgnoredClusters(String personID) async {final db = await asyncDB;diff --git a/mobile/apps/photos/lib/services/machine_learning/face_ml/face_clustering/face_clustering_service.dart b/mobile/apps/photos/lib/services/machine_learning/face_ml/face_clustering/face_clustering_service.dartindex a418f782c2..4c0346555a 100644--- a/mobile/apps/photos/lib/services/machine_learning/face_ml/face_clustering/face_clustering_service.dart+++ b/mobile/apps/photos/lib/services/machine_learning/face_ml/face_clustering/face_clustering_service.dart@@ -352,12 +352,11 @@ ClusteringResult runLinearClustering(Map args) {faceID: face.faceID,faceScore: face.faceScore,blurValue: face.blurValue,- badFace:- face.faceScore < kMinimumQualityFaceScore ||- face.blurValue < kLaplacianSoftThreshold ||- (face.blurValue < kLaplacianVerySoftThreshold &&- face.faceScore < kMediumQualityFaceScore) ||- face.isSideways,+ badFace: isBadFaceForClustering(+ faceScore: face.faceScore,+ blurValue: face.blurValue,+ isSideways: face.isSideways,+ ),vEmbedding: Vector.fromList(EVector.fromBuffer(face.embeddingBytes).values,dtype: DType.float32,diff --git a/mobile/apps/photos/lib/services/machine_learning/face_ml/face_filtering/face_filtering_constants.dart b/mobile/apps/photos/lib/services/machine_learning/face_ml/face_filtering/face_filtering_constants.dartindex ba1d59c7a4..5ab4237897 100644--- a/mobile/apps/photos/lib/services/machine_learning/face_ml/face_filtering/face_filtering_constants.dart+++ b/mobile/apps/photos/lib/services/machine_learning/face_ml/face_filtering/face_filtering_constants.dart@@ -17,6 +17,17 @@ const kMinimumQualityFaceScore = 0.80;const kMediumQualityFaceScore = 0.85;const kHighQualityFaceScore = 0.90;+bool isBadFaceForClustering({+ required double faceScore,+ required double blurValue,+ required bool isSideways,+}) =>+ faceScore < kMinimumQualityFaceScore ||+ blurValue < kLaplacianSoftThreshold ||+ (blurValue < kLaplacianVerySoftThreshold &&+ faceScore < kMediumQualityFaceScore) ||+ isSideways;+/// The minimum cluster size for displaying a cluster in the UI by defaultconst kMinimumClusterSizeSearchResult = 10;diff --git a/mobile/apps/photos/lib/services/machine_learning/face_ml/feedback/cluster_feedback.dart b/mobile/apps/photos/lib/services/machine_learning/face_ml/feedback/cluster_feedback.dartindex 113ba1ecde..83dbd86fb7 100644--- a/mobile/apps/photos/lib/services/machine_learning/face_ml/feedback/cluster_feedback.dart+++ b/mobile/apps/photos/lib/services/machine_learning/face_ml/feedback/cluster_feedback.dart@@ -68,6 +68,14 @@ class ClusterFeedbackService<T> {lastViewedClusterID = '';}+ Future<Set<String>> _getExcludedSuggestionClusterIDs() async {+ final excludedClusterIDs = await mlDataDB.getBadFaceSingletonClusterIDs();+ excludedClusterIDs.addAll(+ await mlDataDB.getClustersWithThreeOrMoreNotPersonFeedback(),+ );+ return excludedClusterIDs;+ }+/// Returns a list of cluster suggestions for a person.Future<List<ClusterSuggestion>> getSuggestionForPerson(PersonEntity person, {@@ -392,18 +400,23 @@ class ClusterFeedbackService<T> {final personToClusterIDsFuture = mlDataDB.getPersonToClusterIDs();final personToRejectedSuggestionsFuture = mlDataDB.getPersonToRejectedSuggestions();+ final excludedSuggestionClusterIDsFuture =+ _getExcludedSuggestionClusterIDs();final canUseVectorDbFuture =_canUseClusterCentroidVectorDbForSuggestions();await Future.wait<Object?>([allClusterIdsToCountMapFuture,personToClusterIDsFuture,personToRejectedSuggestionsFuture,+ excludedSuggestionClusterIDsFuture,canUseVectorDbFuture,], eagerError: false);final allClusterIdsToCountMap = await allClusterIdsToCountMapFuture;final personToClusterIDs = await personToClusterIDsFuture;final personToRejectedSuggestions =await personToRejectedSuggestionsFuture;+ final excludedSuggestionClusterIDs =+ await excludedSuggestionClusterIDsFuture;final canUseVectorDb = await canUseVectorDbFuture;if (!canUseVectorDb) {_logger.info(@@ -414,6 +427,7 @@ class ClusterFeedbackService<T> {allClusterIdsToCountMap,personToClusterIDs,personToRejectedSuggestions,+ excludedSuggestionClusterIDs,);}@@ -446,9 +460,10 @@ class ClusterFeedbackService<T> {for (final clusters in personToClusterIDs.values) {assignedClustersToAnyPerson.addAll(clusters);}- final ignoredClustersForAverages = assignedClustersToAnyPerson.difference(- selectedPersonClusters,- );+ final ignoredClustersForAverages = <String>{+ ...assignedClustersToAnyPerson,+ ...excludedSuggestionClusterIDs,+ }.difference(selectedPersonClusters);final clusterAvg = await _getUpdateClusterAvg(allClusterIdsToCountMap,@@ -513,6 +528,7 @@ class ClusterFeedbackService<T> {allClusterIdsToCountMap,personToClusterIDs,personToRejectedSuggestions,+ excludedSuggestionClusterIDs,);}@@ -575,7 +591,8 @@ class ClusterFeedbackService<T> {final ignored = <String>{}..addAll(assignedClustersToAnyPerson)..removeAll(entry.value)- ..addAll(personToRejectedSuggestions[entry.key] ?? const <String>{});+ ..addAll(personToRejectedSuggestions[entry.key] ?? const <String>{})+ ..addAll(excludedSuggestionClusterIDs);personIDToIgnoredClusters[entry.key] = ignored;}@@ -1104,6 +1121,7 @@ class ClusterFeedbackService<T> {Map<String, int> allClusterIdsToCountMap,Map<String, Set<String>> personToClusterIDs,Map<String, Set<String>> personToRejectedSuggestions,+ Set<String> excludedSuggestionClusterIDs,) async {final personIdToBiggestCluster = <String, String>{};final biggestClusterToPersonID = <String, String>{};@@ -1141,13 +1159,17 @@ class ClusterFeedbackService<T> {return [];}final allPersonClusters = biggestClusterToPersonID.keys.toSet();- final allOtherPersonClustersToIgnore = <String>{};+ final allOtherPersonClustersToIgnore = <String>{+ ...excludedSuggestionClusterIDs,+ };for (final ignoredClusters in personIdToOtherPersonClusterIDs.values) {allOtherPersonClustersToIgnore.addAll(ignoredClusters);}+ final ignoredClustersForAverages = allOtherPersonClustersToIgnore+ .difference(allPersonClusters);final clusterAvg = await _getUpdateClusterAvg(allClusterIdsToCountMap,- allOtherPersonClustersToIgnore,+ ignoredClustersForAverages,minClusterSize: kMinimumClusterSizeSearchResult,);@@ -1526,10 +1548,14 @@ class ClusterFeedbackService<T> {// Get all the cluster datafinal allClusterIdsToCountMap = await mlDataDB.clusterIdToFaceCount();final ignoredClusters = await mlDataDB.getPersonIgnoredClusters(p.remoteID);+ ignoredClusters.addAll(await _getExcludedSuggestionClusterIDs());final personClusters = await mlDataDB.getPersonClusterIDs(p.remoteID);if (personClusters.isEmpty) {return [];}+ final ignoredClustersForAverages = ignoredClusters.difference(+ personClusters,+ );final personFaceIDs = await mlDataDB.getFaceIDsForPerson(p.remoteID);final personFileIDs = personFaceIDs.map(getFileIdFromFaceId<int>).toSet();w?.log(@@ -1551,7 +1577,7 @@ class ClusterFeedbackService<T> {min(minimumSize, kMinimumClusterSizeSearchResult)) {clusterAvgBigClusters = await _getUpdateClusterAvg(allClusterIdsToCountMap,- ignoredClusters,+ ignoredClustersForAverages,minClusterSize: minimumSize,);w?.log(