π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Add unnamed people to memories
laurenspriem33eaa8c2084a
diff --git a/mobile/apps/photos/lib/l10n/intl_en.arb b/mobile/apps/photos/lib/l10n/intl_en.arbindex d4de43eadb..a062cf9cf8 100644--- a/mobile/apps/photos/lib/l10n/intl_en.arb+++ b/mobile/apps/photos/lib/l10n/intl_en.arb@@ -1962,6 +1962,10 @@"personIsAge": "{name} is {age}!","personTurningAge": "{name} turning {age} soon","lastTimeWithThem": "Last time with {name}",+ "unnamedPeopleSpotlight": "A familiar face",+ "unnamedPeopleYouAndThem": "You two",+ "unnamedPeopleDoingSomethingTogether": "Doing it together",+ "unnamedPeopleLastTimeYouSawThem": "Last time together","tripToLocation": "Trip to {location}","tripInYear": "Trip in {year}","lastYearsTrip": "Last year's trip",diff --git a/mobile/apps/photos/lib/models/memories/memories_cache.dart b/mobile/apps/photos/lib/models/memories/memories_cache.dartindex 9489f2eba7..5774dfbb8e 100644--- a/mobile/apps/photos/lib/models/memories/memories_cache.dart+++ b/mobile/apps/photos/lib/models/memories/memories_cache.dart@@ -81,6 +81,7 @@ class ToShowMemory {final String? personID;final String? personName;+ final bool? isUnnamedCluster;final bool? isBirthday;final PeopleMemoryType? peopleMemoryType;final ClipMemoryType? clipMemoryType;@@ -110,6 +111,7 @@ class ToShowMemory {this.fileLocalIntIDs,this.personID,this.personName,+ this.isUnnamedCluster,this.isBirthday,this.peopleMemoryType,this.clipMemoryType,@@ -133,6 +135,7 @@ class ToShowMemory {}) {String? personID;String? personName;+ bool? isUnnamedCluster;bool? isBirthday;PeopleMemoryType? peopleMemoryType;ClipMemoryType? clipMemoryType;@@ -140,6 +143,7 @@ class ToShowMemory {if (memory is PeopleMemory) {personID = memory.personID;personName = memory.personName;+ isUnnamedCluster = memory.isUnnamedCluster;isBirthday = memory.isBirthday;peopleMemoryType = memory.peopleMemoryType;} else if (memory is TripMemory) {@@ -162,6 +166,7 @@ class ToShowMemory {fileLocalIntIDs: fileLocalIntIDs,personID: personID,personName: personName,+ isUnnamedCluster: isUnnamedCluster,isBirthday: isBirthday,peopleMemoryType: peopleMemoryType,clipMemoryType: clipMemoryType,@@ -184,6 +189,7 @@ class ToShowMemory {personID: json['personID'],isBirthday: json['isBirthday'],personName: json['personName'],+ isUnnamedCluster: json['isUnnamedCluster'],peopleMemoryType: json['peopleMemoryType'] != null? peopleMemoryTypeFromString(json['peopleMemoryType']): null,@@ -212,6 +218,7 @@ class ToShowMemory {'personID': personID,'isBirthday': isBirthday,'personName': personName,+ 'isUnnamedCluster': isUnnamedCluster,'peopleMemoryType': peopleMemoryType?.toString().split('.').last,'clipMemoryType': clipMemoryType?.toString().split('.').last,'location': location != nulldiff --git a/mobile/apps/photos/lib/models/memories/people_memory.dart b/mobile/apps/photos/lib/models/memories/people_memory.dartindex 6b89eada22..a9fcd0c005 100644--- a/mobile/apps/photos/lib/models/memories/people_memory.dart+++ b/mobile/apps/photos/lib/models/memories/people_memory.dart@@ -104,6 +104,7 @@ class PeopleMemory extends SmartMemory {final PeopleMemoryType peopleMemoryType;final PeopleActivity? activity;final String? personName;+ final bool isUnnamedCluster;final bool? isBirthday;final int? newAge;@@ -119,6 +120,7 @@ class PeopleMemory extends SmartMemory {super.firstCreationTime,super.lastCreationTime,this.activity,+ this.isUnnamedCluster = false,this.isBirthday,this.newAge,}) : super(@@ -133,6 +135,7 @@ class PeopleMemory extends SmartMemory {PeopleMemory copyWith({int? firstDateToShow,int? lastDateToShow,+ bool? isUnnamedCluster,bool? isBirthday,int? newAge,}) {@@ -146,6 +149,7 @@ class PeopleMemory extends SmartMemory {firstCreationTime: firstCreationTime,lastCreationTime: lastCreationTime,activity: activity,+ isUnnamedCluster: isUnnamedCluster ?? this.isUnnamedCluster,isBirthday: isBirthday ?? this.isBirthday,newAge: newAge ?? this.newAge,);@@ -153,6 +157,18 @@ class PeopleMemory extends SmartMemory {@overrideString createTitle(AppLocalizations locals, String languageCode) {+ if (isUnnamedCluster) {+ switch (peopleMemoryType) {+ case PeopleMemoryType.youAndThem:+ return locals.unnamedPeopleYouAndThem;+ case PeopleMemoryType.doingSomethingTogether:+ return locals.unnamedPeopleDoingSomethingTogether;+ case PeopleMemoryType.spotlight:+ return locals.unnamedPeopleSpotlight;+ case PeopleMemoryType.lastTimeYouSawThem:+ return locals.unnamedPeopleLastTimeYouSawThem;+ }+ }switch (peopleMemoryType) {case PeopleMemoryType.youAndThem:assert(personName != null);@@ -192,6 +208,7 @@ class PeopleMemoryCandidate {required this.firstDateToShow,required this.lastDateToShow,this.activity,+ this.isUnnamedCluster = false,this.lastCreationTime,this.selectionBuilder,this.requiresSelection = true,@@ -201,6 +218,7 @@ class PeopleMemoryCandidate {final String? personName;final PeopleMemoryType type;final PeopleActivity? activity;+ final bool isUnnamedCluster;final List<Memory> rawMemories;final int firstDateToShow;final int lastDateToShow;@@ -235,6 +253,7 @@ class PeopleMemoryCandidate {personName,lastCreationTime: lastCreationTime,activity: activity,+ isUnnamedCluster: isUnnamedCluster,);return _resolvedMemory;}diff --git a/mobile/apps/photos/lib/services/memories_cache_service.dart b/mobile/apps/photos/lib/services/memories_cache_service.dartindex 1ba4b26a63..bacdd9eada 100644--- a/mobile/apps/photos/lib/services/memories_cache_service.dart+++ b/mobile/apps/photos/lib/services/memories_cache_service.dart@@ -459,6 +459,7 @@ class MemoriesCacheService {memory.peopleMemoryType!,memory.personID!,memory.personName,+ isUnnamedCluster: memory.isUnnamedCluster ?? false,title: memory.title,id: memory.id,);@@ -1038,8 +1039,10 @@ class MemoriesCacheService {continue;}final s = await LanguageService.locals;+ final hasNonEmptyName =+ memory.personName != null && memory.personName!.trim().isNotEmpty;await NotificationService.instance.scheduleNotification(- memory.personName != null+ hasNonEmptyName && !memory.isUnnamedCluster? s.wishThemAHappyBirthday(name: memory.personName!): s.happyBirthday,id: memory.id.hashCode,diff --git a/mobile/apps/photos/lib/services/smart_memories_service.dart b/mobile/apps/photos/lib/services/smart_memories_service.dartindex e9aaad00bc..d030a79060 100644--- a/mobile/apps/photos/lib/services/smart_memories_service.dart+++ b/mobile/apps/photos/lib/services/smart_memories_service.dart@@ -63,6 +63,13 @@ class SmartMemoriesService {static const yearsBefore = 30;static const minimumMemoryLength = 5;+ static const _maximumUnnamedPeopleClusters = 10;+ static const _maximumUnnamedPeopleGroupSize = 6;+ static const _minimumUnnamedPeopleWithMePhotos = minimumMemoryLength;+ static const _minimumUnnamedPeopleNonGroupPhotos = minimumMemoryLength;+ static const _minimumUnnamedPeopleNonConsecutiveDays = 4;+ static const _unnamedClusterPersonIDPrefix = "cluster:";+ static const _debugForceUnnamedClustersOnly = false;SmartMemoriesService();@@ -99,6 +106,12 @@ class SmartMemoriesService {_logger.info('gotten all ${persons.length} persons after filtering $t',);+ final allPersonIDs =+ allPersons.map((person) => person.remoteID).toList(growable: false);+ final assignedClusterIDs = allPersonIDs.isEmpty+ ? <String>{}+ : await MLDataDB.instance.getPersonsClusterIDs(allPersonIDs);+ _logger.info('assignedClusterIDs has ${assignedClusterIDs.length} $t');final currentUserEmail = Configuration.instance.getEmail();_logger.info('currentUserEmail: $currentUserEmail $t');@@ -109,6 +122,16 @@ class SmartMemoriesService {final Map<int, List<FaceWithoutEmbedding>> fileIdToFaces =await MLDataDB.instance.getFileIDsToFacesWithoutEmbedding();_logger.info('fileIdToFaces has ${fileIdToFaces.length} entries $t');+ final clusterIdToFaceCount =+ await MLDataDB.instance.clusterIdToFaceCount();+ _logger.info(+ 'clusterIdToFaceCount has ${clusterIdToFaceCount.length} entries $t',+ );+ final clusterIdToFaceIDs =+ await MLDataDB.instance.getAllClusterIdToFaceIDs();+ _logger.info(+ 'clusterIdToFaceIDs has ${clusterIdToFaceIDs.length} entries $t',+ );final allImageEmbeddings = await MLDataDB.instance.getAllClipVectors();_logger.info(@@ -160,6 +183,9 @@ class SmartMemoriesService {"currentUserEmail": currentUserEmail,"cities": cities,"fileIdToFaces": fileIdToFaces,+ "clusterIdToFaceCount": clusterIdToFaceCount,+ "clusterIdToFaceIDs": clusterIdToFaceIDs,+ "assignedClusterIDs": assignedClusterIDs,"allImageEmbeddings": allImageEmbeddings,"clipPositiveTextVector": clipPositiveTextVector,"clipPeopleActivityVectors": clipPeopleActivityVectors,@@ -336,6 +362,177 @@ class SmartMemoriesService {return filtered;}+ static List<PeopleMemoryCandidate> _buildUnnamedClusterCandidates({+ required Map<String, int> clusterIdToFaceCount,+ required Map<String, Iterable<String>> clusterIdToFaceIDs,+ required Set<String> assignedClusterIDs,+ required Map<int, EnteFile> allFileIdsToFile,+ required Map<int, List<FaceWithoutEmbedding>> fileIdToFaces,+ required Set<int>? meFileIDs,+ required bool isMeAssigned,+ required Map<int, int> seenTimes,+ required int nowInMicroseconds,+ required int windowEnd,+ required PeopleSelectionBuilder selectionBuilder,+ }) {+ if (clusterIdToFaceCount.isEmpty || clusterIdToFaceIDs.isEmpty) {+ return <PeopleMemoryCandidate>[];+ }+ if (isMeAssigned && (meFileIDs == null || meFileIDs.isEmpty)) {+ return <PeopleMemoryCandidate>[];+ }+ final sortedUnassignedClusters = clusterIdToFaceCount.entries+ .where((entry) => !assignedClusterIDs.contains(entry.key))+ .toList(growable: false)+ ..sort((a, b) => b.value.compareTo(a.value));+ final unnamedCandidates = <PeopleMemoryCandidate>[];+ for (final entry in sortedUnassignedClusters) {+ if (unnamedCandidates.length >= _maximumUnnamedPeopleClusters) break;+ final clusterID = entry.key;+ final faceIDs = clusterIdToFaceIDs[clusterID];+ if (faceIDs == null || faceIDs.isEmpty) continue;++ final clusterFileIDs = <int>{};+ for (final faceID in faceIDs) {+ clusterFileIDs.add(getFileIdFromFaceId(faceID));+ }+ if (clusterFileIDs.isEmpty) continue;++ final nonGroupFiles = <EnteFile>[];+ final nonGroupCreationTimes = <int>[];+ int overlapWithMeInNonGroupFiles = 0;+ for (final fileID in clusterFileIDs) {+ final facesInPhoto = fileIdToFaces[fileID]?.length ?? 0;+ if (facesInPhoto == 0 ||+ facesInPhoto > _maximumUnnamedPeopleGroupSize) {+ continue;+ }+ final file = allFileIdsToFile[fileID];+ if (file == null || file.creationTime == null) continue;+ nonGroupFiles.add(file);+ nonGroupCreationTimes.add(file.creationTime!);+ if (isMeAssigned && meFileIDs!.contains(fileID)) {+ overlapWithMeInNonGroupFiles++;+ }+ }+ if (isMeAssigned &&+ overlapWithMeInNonGroupFiles < _minimumUnnamedPeopleWithMePhotos) {+ continue;+ }+ if (nonGroupFiles.length < _minimumUnnamedPeopleNonGroupPhotos) {+ continue;+ }+ final nonConsecutiveDays =+ _countNonConsecutiveDays(nonGroupCreationTimes);+ if (nonConsecutiveDays < _minimumUnnamedPeopleNonConsecutiveDays) {+ continue;+ }++ unnamedCandidates.add(+ PeopleMemoryCandidate(+ personID: "$_unnamedClusterPersonIDPrefix$clusterID",+ personName: null,+ type: PeopleMemoryType.spotlight,+ rawMemories: nonGroupFiles+ .map((file) => Memory.fromFile(file, seenTimes))+ .toList(growable: false),+ firstDateToShow: nowInMicroseconds,+ lastDateToShow: windowEnd,+ selectionBuilder: selectionBuilder,+ isUnnamedCluster: true,+ ),+ );+ }+ return unnamedCandidates;+ }++ static int _countNonConsecutiveDays(Iterable<int> creationTimes) {+ if (creationTimes.isEmpty) return 0;+ final uniqueDays = creationTimes+ .map((timestamp) => timestamp - (timestamp % microSecondsInDay))+ .toSet()+ .toList(growable: false)+ ..sort();+ if (uniqueDays.isEmpty) return 0;+ int count = 1;+ int previousDay = uniqueDays.first;+ for (final day in uniqueDays.skip(1)) {+ if (day - previousDay > microSecondsInDay) {+ count++;+ }+ previousDay = day;+ }+ return count;+ }++ static Map<String, int> _latestShownTimeByPersonID(+ Iterable<PeopleShownLog> shownPeople,+ ) {+ final latestShownTimeByPersonID = <String, int>{};+ for (final shownLog in shownPeople) {+ final oldValue = latestShownTimeByPersonID[shownLog.personID];+ if (oldValue == null || shownLog.lastTimeShown > oldValue) {+ latestShownTimeByPersonID[shownLog.personID] = shownLog.lastTimeShown;+ }+ }+ return latestShownTimeByPersonID;+ }++ static List<PeopleMemoryCandidate> _orderUnnamedCandidatesByRecencyAndRandom({+ required Iterable<PeopleMemoryCandidate> candidates,+ required Iterable<PeopleShownLog> shownPeople,+ required DateTime currentTime,+ required Duration shownPersonTimeout,+ }) {+ final remaining = candidates.toList(growable: true);+ if (remaining.length <= 1) return remaining;+ final random = Random();+ final latestShownTimeByPersonID = _latestShownTimeByPersonID(shownPeople);+ final nowInMicroseconds = currentTime.microsecondsSinceEpoch;+ final unseenWeight = max(1, shownPersonTimeout.inDays * 2);+ final orderedCandidates = <PeopleMemoryCandidate>[];+ while (remaining.isNotEmpty) {+ final weights = <int>[];+ int totalWeight = 0;+ for (final candidate in remaining) {+ final latestShownTime = latestShownTimeByPersonID[candidate.personID];+ final weight = latestShownTime == null+ ? unseenWeight+ : max(+ 1,+ (nowInMicroseconds - latestShownTime) ~/ microSecondsInDay,+ );+ weights.add(weight);+ totalWeight += weight;+ }+ var chosenWeight = random.nextInt(totalWeight);+ int chosenIndex = 0;+ for (; chosenIndex < weights.length; chosenIndex++) {+ chosenWeight -= weights[chosenIndex];+ if (chosenWeight < 0) break;+ }+ orderedCandidates.add(remaining.removeAt(chosenIndex));+ }+ return orderedCandidates;+ }++ static bool _wasPersonShownRecently({+ required String personID,+ required Iterable<PeopleShownLog> shownPeople,+ required DateTime currentTime,+ required Duration shownPersonTimeout,+ }) {+ for (final shownLog in shownPeople) {+ if (shownLog.personID != personID) continue;+ final shownDate =+ DateTime.fromMicrosecondsSinceEpoch(shownLog.lastTimeShown);+ if (currentTime.difference(shownDate) < shownPersonTimeout) {+ return true;+ }+ }+ return false;+ }+Future<(Set<EnteFile>, Map<int, EnteFile>)> _getFilesAndMapForMemories({bool useGeneratedIds = false,bool requireLocalId = false,@@ -430,6 +627,15 @@ class SmartMemoriesService {final List<City> cities = args["cities"];final Map<int, List<FaceWithoutEmbedding>> fileIdToFaces =args["fileIdToFaces"];+ final Map<String, int> clusterIdToFaceCount = Map<String, int>.from(+ args["clusterIdToFaceCount"] as Map,+ );+ final Map<String, Iterable<String>> clusterIdToFaceIDs =+ Map<String, Iterable<String>>.from(+ args["clusterIdToFaceIDs"] as Map,+ );+ final Set<String> assignedClusterIDs =+ (args["assignedClusterIDs"] as Set).cast<String>();final List<EmbeddingVector> allImageEmbeddings =args["allImageEmbeddings"];final Vector clipPositiveTextVector = args["clipPositiveTextVector"];@@ -479,6 +685,9 @@ class SmartMemoriesService {persons: persons,currentUserEmail: currentUserEmail,fileIdToFaces: fileIdToFaces,+ clusterIdToFaceCount: clusterIdToFaceCount,+ clusterIdToFaceIDs: clusterIdToFaceIDs,+ assignedClusterIDs: assignedClusterIDs,fileIDToImageEmbedding: fileIDToImageEmbedding,clipPositiveTextVector: clipPositiveTextVector,clipPeopleActivityVectors: clipPeopleActivityVectors,@@ -621,6 +830,9 @@ class SmartMemoriesService {required List<PersonEntity> persons,String? currentUserEmail,required Map<int, List<FaceWithoutEmbedding>> fileIdToFaces,+ required Map<String, int> clusterIdToFaceCount,+ required Map<String, Iterable<String>> clusterIdToFaceIDs,+ required Set<String> assignedClusterIDs,required Map<int, EmbeddingVector> fileIDToImageEmbedding,required Vector clipPositiveTextVector,required Map<PeopleActivity, Vector> clipPeopleActivityVectors,@@ -634,7 +846,6 @@ class SmartMemoriesService {w?.log('allFiles setup');// Get ordered (random) list of important people- if (persons.isEmpty) return [];final personIdToPerson = <String, PersonEntity>{};final personIdToFaceIDs = <String, Set<String>>{};final personIdToFileIDs = <String, Set<int>>{};@@ -657,6 +868,12 @@ class SmartMemoriesService {.toList();orderedImportantPersonsID.shuffle(Random());final amountOfPersons = orderedImportantPersonsID.length;+ final shownPersonTimeout = Duration(+ days: min(+ kPersonShowTimeout.inDays,+ max(1, amountOfPersons) * kMemoriesUpdateFrequencyDays,+ ),+ );w?.log('orderedImportantPersonsID setup');// Check if the user has assignmed "me"@@ -680,6 +897,38 @@ class SmartMemoriesService {);}+ final unnamedClusterCandidates = _buildUnnamedClusterCandidates(+ clusterIdToFaceCount: clusterIdToFaceCount,+ clusterIdToFaceIDs: clusterIdToFaceIDs,+ assignedClusterIDs: assignedClusterIDs,+ allFileIdsToFile: allFileIdsToFile,+ fileIdToFaces: fileIdToFaces,+ meFileIDs: meFileIDs,+ isMeAssigned: isMeAssigned,+ seenTimes: seenTimes,+ nowInMicroseconds: nowInMicroseconds,+ windowEnd: windowEnd,+ selectionBuilder: selectPeopleMemories,+ );+ final randomizedUnnamedClusterCandidates =+ _orderUnnamedCandidatesByRecencyAndRandom(+ candidates: unnamedClusterCandidates,+ shownPeople: shownPeople,+ currentTime: currentTime,+ shownPersonTimeout: shownPersonTimeout,+ );+ w?.log('unnamed cluster candidates setup');++ if (kDebugMode && _debugForceUnnamedClustersOnly) {+ for (final candidate in randomizedUnnamedClusterCandidates) {+ final memory = await candidate.realize();+ if (memory == null) continue;+ memoryResults.add(memory);+ if (!surfaceAll) break;+ }+ return memoryResults;+ }+final Map<String, Map<PeopleMemoryType, List<PeopleMemoryCandidate>>>personToCandidates = {};for (final personID in orderedImportantPersonsID) {@@ -892,6 +1141,12 @@ class SmartMemoriesService {}}}+ for (final candidate in unnamedClusterCandidates) {+ final memory = await candidate.realize();+ if (memory != null) {+ memoryResults.add(memory);+ }+ }return memoryResults;}@@ -986,14 +1241,9 @@ class SmartMemoriesService {w?.log('relevancy setup');// Loop through the people (and memory types) and add based on rotation- final shownPersonTimeout = Duration(- days: min(- kPersonShowTimeout.inDays,- max(1, amountOfPersons) * kMemoriesUpdateFrequencyDays,- ),- );final shownPersonAndTypeTimeout =Duration(days: shownPersonTimeout.inDays * 2);+ bool addedFromRotation = false;peopleRotationLoop:for (final personID in orderedImportantPersonsID) {for (final memory in memoryResults) {@@ -1045,11 +1295,52 @@ class SmartMemoriesService {final potentialMemory = await potentialCandidate.realize();if (potentialMemory == null) continue;memoryResults.add(potentialMemory);+ addedFromRotation = true;added++;if (added >= 2) break peopleRotationLoop;}if (added > 0) break peopleRotationLoop;}+ if (!addedFromRotation) {+ final eligibleUnnamedCandidates = <PeopleMemoryCandidate>[];+ for (final candidate in randomizedUnnamedClusterCandidates) {+ final candidatePersonID = candidate.personID;+ bool alreadyInResults = false;+ for (final memory in memoryResults) {+ if (memory.personID == candidatePersonID) {+ alreadyInResults = true;+ break;+ }+ }+ if (alreadyInResults) {+ continue;+ }+ if (_wasPersonShownRecently(+ personID: candidatePersonID,+ shownPeople: shownPeople,+ currentTime: currentTime,+ shownPersonTimeout: shownPersonTimeout,+ )) {+ continue;+ }+ eligibleUnnamedCandidates.add(candidate);+ }+ final orderedEligibleUnnamedCandidates =+ _orderUnnamedCandidatesByRecencyAndRandom(+ candidates: eligibleUnnamedCandidates,+ shownPeople: shownPeople,+ currentTime: currentTime,+ shownPersonTimeout: shownPersonTimeout,+ );+ for (final candidate in orderedEligibleUnnamedCandidates) {+ final potentialMemory = await candidate.realize();+ if (potentialMemory == null) {+ continue;+ }+ memoryResults.add(potentialMemory);+ break;+ }+ }w?.log('rotation setup');return memoryResults;