π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mob] Persist remote feedback before running clustering
Neeraj Gupta1996d8683504
diff --git a/mobile/lib/face/db.dart b/mobile/lib/face/db.dartindex 8063acc8b8..24f14e3ac7 100644--- a/mobile/lib/face/db.dart+++ b/mobile/lib/face/db.dart@@ -482,6 +482,24 @@ class FaceMLDataDB {);}+ Future<void> bulkAssignClusterToPersonID(+ Map<int, String> clusterToPersonID,) async {+ final db = await instance.database;+ final batch = db.batch();+ for (final entry in clusterToPersonID.entries) {+ final clusterID = entry.key;+ final personID = entry.value;+ batch.insert(+ clusterPersonTable,+ {+ personIdColumn: personID,+ cluserIDColumn: clusterID,+ },+ );+ }+ await batch.commit(noResult: true);+ }+Future<void> captureNotPersonFeedback({required String personID,required int clusterID,diff --git a/mobile/lib/face/db_fields.dart b/mobile/lib/face/db_fields.dartindex a6f28542a8..c62f33338a 100644--- a/mobile/lib/face/db_fields.dart+++ b/mobile/lib/face/db_fields.dart@@ -36,8 +36,7 @@ const createFaceClustersTable = '''CREATE TABLE IF NOT EXISTS $faceClustersTable ($fcFaceId TEXT NOT NULL,$fcClusterID INTEGER NOT NULL,- PRIMARY KEY($fcFaceId),- FOREIGN KEY($fcFaceId) REFERENCES $facesTable($faceIDColumn)+ PRIMARY KEY($fcFaceId));''';// -- Creating a non-unique index on clusterID for query optimizationdiff --git a/mobile/lib/services/machine_learning/face_ml/person/person_service.dart b/mobile/lib/services/machine_learning/face_ml/person/person_service.dartindex a31f31dd3d..266afa0987 100644--- a/mobile/lib/services/machine_learning/face_ml/person/person_service.dart+++ b/mobile/lib/services/machine_learning/face_ml/person/person_service.dart@@ -84,6 +84,24 @@ class PersonService {return PersonEntity(result.id, data);}+ Future<void> storeRemoteFeedback() async {+ final entities = await entityService.getEntities(EntityType.person);+ entities.sort((a, b) => a.updatedAt.compareTo(b.updatedAt));+ final Map<String, int> faceIdToClusterID = {};+ final Map<int, String> clusterToPersonID = {};+ for (var e in entities) {+ final personData = PersonData.fromJson(json.decode(e.data));+ for (var cluster in personData.assigned!) {+ for (var faceId in cluster.faces) {+ faceIdToClusterID[faceId] = cluster.id;+ }+ clusterToPersonID[cluster.id] = e.id;+ }+ }+ await faceMLDataDB.updateClusterIdToFaceId(faceIdToClusterID);+ await faceMLDataDB.bulkAssignClusterToPersonID(clusterToPersonID);+ }+Future<void> updatePerson(PersonEntity updatePerson) async {await entityService.addOrUpdate(EntityType.person,diff --git a/mobile/lib/services/search_service.dart b/mobile/lib/services/search_service.dartindex 3e18265615..7c7544de28 100644--- a/mobile/lib/services/search_service.dart+++ b/mobile/lib/services/search_service.dart@@ -737,117 +737,125 @@ class SearchService {}Future<List<GenericSearchResult>> getAllFace(int? limit) async {- debugPrint("getting faces");- final Map<int, Set<int>> fileIdToClusterID =- await FaceMLDataDB.instance.getFileIdToClusterIds();- final Map<String, PersonEntity> personIdToPerson =- await PersonService.instance.getPersonsMap();- final clusterIDToPersonID =- await FaceMLDataDB.instance.getClusterIDToPersonID();-- final List<GenericSearchResult> facesResult = [];- final Map<int, List<EnteFile>> clusterIdToFiles = {};- final Map<String, List<EnteFile>> personIdToFiles = {};- final allFiles = await getAllFiles();- for (final f in allFiles) {- if (!fileIdToClusterID.containsKey(f.uploadedFileID ?? -1)) {- continue;- }- final cluserIds = fileIdToClusterID[f.uploadedFileID ?? -1]!;- for (final cluster in cluserIds) {- final PersonEntity? p = personIdToPerson[clusterIDToPersonID[cluster] ?? ""];- if (p != null) {- if (personIdToFiles.containsKey(p.remoteID)) {- personIdToFiles[p.remoteID]!.add(f);- } else {- personIdToFiles[p.remoteID] = [f];- }- } else {- if (clusterIdToFiles.containsKey(cluster)) {- clusterIdToFiles[cluster]!.add(f);+ try {+ debugPrint("getting faces");+ final Map<int, Set<int>> fileIdToClusterID =+ await FaceMLDataDB.instance.getFileIdToClusterIds();+ final Map<String, PersonEntity> personIdToPerson =+ await PersonService.instance.getPersonsMap();+ final clusterIDToPersonID =+ await FaceMLDataDB.instance.getClusterIDToPersonID();++ final List<GenericSearchResult> facesResult = [];+ final Map<int, List<EnteFile>> clusterIdToFiles = {};+ final Map<String, List<EnteFile>> personIdToFiles = {};+ final allFiles = await getAllFiles();+ for (final f in allFiles) {+ if (!fileIdToClusterID.containsKey(f.uploadedFileID ?? -1)) {+ continue;+ }+ final cluserIds = fileIdToClusterID[f.uploadedFileID ?? -1]!;+ for (final cluster in cluserIds) {+ final PersonEntity? p =+ personIdToPerson[clusterIDToPersonID[cluster] ?? ""];+ if (p != null) {+ if (personIdToFiles.containsKey(p.remoteID)) {+ personIdToFiles[p.remoteID]!.add(f);+ } else {+ personIdToFiles[p.remoteID] = [f];+ }} else {- clusterIdToFiles[cluster] = [f];+ if (clusterIdToFiles.containsKey(cluster)) {+ clusterIdToFiles[cluster]!.add(f);+ } else {+ clusterIdToFiles[cluster] = [f];+ }}}}- }- // get sorted personId by files count- final sortedPersonIds = personIdToFiles.keys.toList()- ..sort(- (a, b) => personIdToFiles[b]!.length.compareTo(- personIdToFiles[a]!.length,- ),- );- for (final personID in sortedPersonIds) {- final files = personIdToFiles[personID]!;- if (files.isEmpty) {- continue;- }- final PersonEntity p = personIdToPerson[personID]!;- facesResult.add(- GenericSearchResult(- ResultType.faces,- p.data.name,- files,- params: {- kPersonParamID: personID,- kFileID: files.first.uploadedFileID,- },- onResultTap: (ctx) {- routeToPage(- ctx,- PeoplePage(- tagPrefix: "${ResultType.faces.toString()}_${p.data.name}",- person: p,+ // get sorted personId by files count+ final sortedPersonIds = personIdToFiles.keys.toList()+ ..sort(+ (a, b) => personIdToFiles[b]!.length.compareTo(+ personIdToFiles[a]!.length,),- );- },- ),- );- }- final sortedClusterIds = clusterIdToFiles.keys.toList()- ..sort(- (a, b) =>- clusterIdToFiles[b]!.length.compareTo(clusterIdToFiles[a]!.length),- );+ );+ for (final personID in sortedPersonIds) {+ final files = personIdToFiles[personID]!;+ if (files.isEmpty) {+ continue;+ }+ final PersonEntity p = personIdToPerson[personID]!;+ facesResult.add(+ GenericSearchResult(+ ResultType.faces,+ p.data.name,+ files,+ params: {+ kPersonParamID: personID,+ kFileID: files.first.uploadedFileID,+ },+ onResultTap: (ctx) {+ routeToPage(+ ctx,+ PeoplePage(+ tagPrefix: "${ResultType.faces.toString()}_${p.data.name}",+ person: p,+ ),+ );+ },+ ),+ );+ }+ final sortedClusterIds = clusterIdToFiles.keys.toList()+ ..sort(+ (a, b) => clusterIdToFiles[b]!+ .length+ .compareTo(clusterIdToFiles[a]!.length),+ );- for (final clusterId in sortedClusterIds) {- final files = clusterIdToFiles[clusterId]!;- // final String clusterName = "ID:$clusterId, ${files.length}";- final String clusterName = "${files.length}";+ for (final clusterId in sortedClusterIds) {+ final files = clusterIdToFiles[clusterId]!;+ // final String clusterName = "ID:$clusterId, ${files.length}";+ final String clusterName = "${files.length}";- if (clusterIDToPersonID[clusterId] != null) {- throw Exception("Cluster $clusterId should not have person id ${clusterIDToPersonID[clusterId]}");+ if (clusterIDToPersonID[clusterId] != null) {+ throw Exception(+ "Cluster $clusterId should not have person id ${clusterIDToPersonID[clusterId]}");+ }+ if (files.length < 3) {+ continue;+ }+ facesResult.add(+ GenericSearchResult(+ ResultType.faces,+ clusterName,+ files,+ params: {+ kClusterParamId: clusterId,+ kFileID: files.first.uploadedFileID,+ },+ onResultTap: (ctx) {+ routeToPage(+ ctx,+ ClusterPage(+ files,+ tagPrefix: "${ResultType.faces.toString()}_$clusterName",+ clusterID: clusterId,+ ),+ );+ },+ ),+ );}- if (files.length < 3) {- continue;+ if (limit != null) {+ return facesResult.sublist(0, min(limit, facesResult.length));+ } else {+ return facesResult;}- facesResult.add(- GenericSearchResult(- ResultType.faces,- clusterName,- files,- params: {- kClusterParamId: clusterId,- kFileID: files.first.uploadedFileID,- },- onResultTap: (ctx) {- routeToPage(- ctx,- ClusterPage(- files,- tagPrefix: "${ResultType.faces.toString()}_$clusterName",- clusterID: clusterId,- ),- );- },- ),- );- }- if (limit != null) {- return facesResult.sublist(0, min(limit, facesResult.length));- } else {- return facesResult;+ } catch (e, s) {+ _logger.severe("Error in getAllFace", e, s);+ rethrow;}}diff --git a/mobile/lib/ui/settings/debug/face_debug_section_widget.dart b/mobile/lib/ui/settings/debug/face_debug_section_widget.dartindex 55e09e0306..7d06f91a6f 100644--- a/mobile/lib/ui/settings/debug/face_debug_section_widget.dart+++ b/mobile/lib/ui/settings/debug/face_debug_section_widget.dart@@ -159,6 +159,7 @@ class _FaceDebugSectionWidgetState extends State<FaceDebugSectionWidget> {trailingIcon: Icons.chevron_right_outlined,trailingIconIsMuted: true,onTap: () async {+ await PersonService.instance.storeRemoteFeedback();await FaceMlService.instance.clusterAllImages(clusterInBuckets: true);Bus.instance.fire(PeopleChangedEvent());@@ -225,10 +226,15 @@ class _FaceDebugSectionWidgetState extends State<FaceDebugSectionWidget> {"You will need to again re-index all the faces. You can drop feedback if you want to label again",firstButtonLabel: "Yes, confirm",firstButtonOnTap: () async {- await FaceMLDataDB.instance- .dropClustersAndPersonTable(faces: true);- Bus.instance.fire(PeopleChangedEvent());- showShortToast(context, "Done");+ try {+ await FaceMLDataDB.instance+ .dropClustersAndPersonTable(faces: true);+ Bus.instance.fire(PeopleChangedEvent());+ showShortToast(context, "Done");+ } catch (e, s) {+ _logger.warning('drop feedback failed ', e, s);+ await showGenericErrorDialog(context: context, error: e);+ }},);},