Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Remove pet_indexed_files table and pet_face_embedding column

Amrithesh Kakkoth Ente3938ef8cdfac
diff --git a/mobile/apps/photos/lib/db/ml/base.dart b/mobile/apps/photos/lib/db/ml/base.dart
index 2450f6218f..8c17601bbb 100644
--- a/mobile/apps/photos/lib/db/ml/base.dart
+++ b/mobile/apps/photos/lib/db/ml/base.dart
@@ -128,6 +128,5 @@ abstract class IMLDataDB<T> {
Future<Map<int, int>> petIndexedFileIds({int minimumMlVersion});
Future<int> getPetIndexedFileCount({int minimumMlVersion});
- Future<void> markPetIndexed(int fileID, int mlVersion);
Future<void> deletePetDataForFiles(List<int> fileIDs);
}
diff --git a/mobile/apps/photos/lib/db/ml/db.dart b/mobile/apps/photos/lib/db/ml/db.dart
index 635e06648f..eeacb7ab14 100644
--- a/mobile/apps/photos/lib/db/ml/db.dart
+++ b/mobile/apps/photos/lib/db/ml/db.dart
@@ -106,6 +106,7 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
createPetFaceVectorIdMappingTable,
createPetBodyVectorIdMappingTable,
createPetIndexedFilesTable,
+ dropPetIndexedFilesTable,
];
static const List<String> _offlineMigrationScripts = [
..._defaultMigrationScripts,
@@ -195,9 +196,9 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
const String sql = '''
INSERT INTO $petFacesTable (
- $fileIDColumn, $petFaceIDColumn, $faceDetectionColumn, $faceVectorIdColumn, $speciesColumn, $faceScore, $imageHeight, $imageWidth, $mlVersionColumn, $petFaceEmbeddingColumn
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
- ON CONFLICT($fileIDColumn, $petFaceIDColumn) DO UPDATE SET $faceDetectionColumn = excluded.$faceDetectionColumn, $faceVectorIdColumn = excluded.$faceVectorIdColumn, $speciesColumn = excluded.$speciesColumn, $faceScore = excluded.$faceScore, $imageHeight = excluded.$imageHeight, $imageWidth = excluded.$imageWidth, $mlVersionColumn = excluded.$mlVersionColumn, $petFaceEmbeddingColumn = excluded.$petFaceEmbeddingColumn
+ $fileIDColumn, $petFaceIDColumn, $faceDetectionColumn, $faceVectorIdColumn, $speciesColumn, $faceScore, $imageHeight, $imageWidth, $mlVersionColumn
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
+ ON CONFLICT($fileIDColumn, $petFaceIDColumn) DO UPDATE SET $faceDetectionColumn = excluded.$faceDetectionColumn, $faceVectorIdColumn = excluded.$faceVectorIdColumn, $speciesColumn = excluded.$speciesColumn, $faceScore = excluded.$faceScore, $imageHeight = excluded.$imageHeight, $imageWidth = excluded.$imageWidth, $mlVersionColumn = excluded.$mlVersionColumn
''';
final parameterSets = batch.map((petFace) {
final map = petFace.toMap();
@@ -211,7 +212,6 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
map[imageHeight],
map[imageWidth],
map[mlVersionColumn],
- map[petFaceEmbeddingColumn],
];
}).toList();
@@ -563,7 +563,6 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
await db.execute(deletePetClusterSummaryTable);
await db.execute(deletePetFaceVectorIdMappingTable);
await db.execute(deletePetBodyVectorIdMappingTable);
- await db.execute(deletePetIndexedFilesTable);
for (final vdb in PetVectorDB.allInstances) {
await vdb.deleteIndexFile();
}
@@ -741,7 +740,7 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
final db = await asyncDB;
const String query = '''
SELECT * FROM $petFacesTable
- WHERE $fileIDColumn = ?
+ WHERE $fileIDColumn = ? AND $speciesColumn != -1
''';
final List<Map<String, dynamic>> maps = await db.getAll(
query,
@@ -2349,7 +2348,7 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
final db = await asyncDB;
final String query = '''
SELECT $fileIDColumn, $mlVersionColumn
- FROM $petIndexedFilesTable
+ FROM $petFacesTable
WHERE $mlVersionColumn >= $minimumMlVersion
''';
final List<Map<String, dynamic>> maps = await db.getAll(query);
@@ -2366,22 +2365,11 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
}) async {
final db = await asyncDB;
final String query =
- 'SELECT COUNT(*) as count FROM $petIndexedFilesTable WHERE $mlVersionColumn >= $minimumMlVersion';
+ 'SELECT COUNT(DISTINCT $fileIDColumn) as count FROM $petFacesTable WHERE $mlVersionColumn >= $minimumMlVersion';
final List<Map<String, dynamic>> maps = await db.getAll(query);
return maps.first['count'] as int;
}
- @override
- Future<void> markPetIndexed(int fileID, int mlVersion) async {
- final db = await asyncDB;
- await db.execute(
- '''INSERT INTO $petIndexedFilesTable ($fileIDColumn, $mlVersionColumn)
- VALUES (?, ?)
- ON CONFLICT($fileIDColumn) DO UPDATE SET $mlVersionColumn = excluded.$mlVersionColumn''',
- [fileID, mlVersion],
- );
- }
-
@override
Future<void> deletePetDataForFiles(List<int> fileIDs) async {
if (fileIDs.isEmpty) return;
@@ -2393,9 +2381,6 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
await db.execute(
'DELETE FROM $petBodiesTable WHERE $fileIDColumn IN ($ids)',
);
- await db.execute(
- 'DELETE FROM $petIndexedFilesTable WHERE $fileIDColumn IN ($ids)',
- );
}
@override
diff --git a/mobile/apps/photos/lib/db/ml/db_pet_model_mappers.dart b/mobile/apps/photos/lib/db/ml/db_pet_model_mappers.dart
index 977c3a0e0b..fe2d1e04bf 100644
--- a/mobile/apps/photos/lib/db/ml/db_pet_model_mappers.dart
+++ b/mobile/apps/photos/lib/db/ml/db_pet_model_mappers.dart
@@ -1,7 +1,6 @@
-import "dart:typed_data" show Uint8List;
-
import "package:ente_pure_utils/ente_pure_utils.dart";
import "package:photos/db/ml/schema.dart";
+import "package:photos/models/ml/ml_versions.dart";
// ── Pet Face DB Mapper ──
@@ -16,7 +15,6 @@ class DBPetFace {
final int imageHeight;
final int imageWidth;
final int mlVersion;
- final Uint8List? embeddingBlob;
DBPetFace({
required this.fileId,
@@ -28,7 +26,6 @@ class DBPetFace {
required this.imageHeight,
required this.imageWidth,
required this.mlVersion,
- this.embeddingBlob,
});
Map<String, dynamic> toMap() {
@@ -42,10 +39,25 @@ class DBPetFace {
'height': imageHeight,
'width': imageWidth,
mlVersionColumn: mlVersion,
- petFaceEmbeddingColumn: embeddingBlob,
};
}
+ /// Creates a dummy entry to mark a file as pet-indexed when no pets were
+ /// found, matching how [Face.empty] works for human face detection.
+ factory DBPetFace.empty(int fileId, {bool error = false}) {
+ return DBPetFace(
+ fileId: fileId,
+ petFaceId: '${fileId}_pet_0_0_0_0',
+ detection: '{}',
+ faceVectorId: -1,
+ species: -1,
+ faceScore: error ? -1.0 : 0.0,
+ imageHeight: 0,
+ imageWidth: 0,
+ mlVersion: petMlVersion,
+ );
+ }
+
factory DBPetFace.fromMap(Map<String, dynamic> map) {
return DBPetFace(
fileId: map[fileIDColumn] as int,
@@ -57,7 +69,6 @@ class DBPetFace {
imageHeight: map['height'] as int,
imageWidth: map['width'] as int,
mlVersion: map[mlVersionColumn] as int,
- embeddingBlob: map[petFaceEmbeddingColumn] as Uint8List?,
);
}
}
diff --git a/mobile/apps/photos/lib/db/ml/schema.dart b/mobile/apps/photos/lib/db/ml/schema.dart
index a5d3133f10..0197b2ce5c 100644
--- a/mobile/apps/photos/lib/db/ml/schema.dart
+++ b/mobile/apps/photos/lib/db/ml/schema.dart
@@ -207,7 +207,6 @@ const createPetFacesTable = '''CREATE TABLE IF NOT EXISTS $petFacesTable (
$imageHeight INTEGER NOT NULL DEFAULT 0,
$imageWidth INTEGER NOT NULL DEFAULT 0,
$mlVersionColumn INTEGER NOT NULL DEFAULT -1,
- $petFaceEmbeddingColumn BLOB,
PRIMARY KEY($fileIDColumn, $petFaceIDColumn)
);
''';
@@ -265,10 +264,6 @@ CREATE TABLE IF NOT EXISTS $petClusterSummaryTable (
const deletePetClusterSummaryTable = 'DELETE FROM $petClusterSummaryTable';
-// ── Embedding BLOB Columns ──
-
-const petFaceEmbeddingColumn = 'pet_face_embedding';
-
// ── Vector ID Mapping Tables ──
/// Maps pet_face_id (string) β†’ integer for face usearch index
@@ -310,4 +305,4 @@ CREATE TABLE IF NOT EXISTS $petIndexedFilesTable (
);
''';
-const deletePetIndexedFilesTable = 'DELETE FROM $petIndexedFilesTable';
+const dropPetIndexedFilesTable = 'DROP TABLE IF EXISTS $petIndexedFilesTable';
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
index 3f5976f17a..7064e2cd7d 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
@@ -2,7 +2,7 @@ import "dart:async";
import "dart:convert" show jsonEncode;
import "dart:io" show Platform;
import "dart:math" show min;
-import "dart:typed_data" show Float32List, Uint8List;
+import "dart:typed_data" show Uint8List;
import "package:flutter/foundation.dart" show kDebugMode;
import "package:logging/logging.dart";
@@ -631,17 +631,10 @@ class MLService {
}
// Pet results locally
- final rustPets =
- result.petFaces != null || result.petBodies != null;
+ final rustPets = result.petFaces != null || result.petBodies != null;
if (rustPets) {
if (result.petFaces != null && result.petFaces!.isNotEmpty) {
final dbPetFaces = result.petFaces!.map((pf) {
- final floatList = Float32List.fromList(pf.embedding);
- final blob = Uint8List.view(
- floatList.buffer,
- floatList.offsetInBytes,
- floatList.lengthInBytes,
- );
return DBPetFace(
fileId: result.fileId,
petFaceId: pf.petFaceId,
@@ -652,7 +645,6 @@ class MLService {
imageHeight: result.decodedImageSize.height,
imageWidth: result.decodedImageSize.width,
mlVersion: petMlVersion,
- embeddingBlob: blob,
);
}).toList();
await mlDataDB.bulkInsertPetFaces(dbPetFaces);
@@ -694,7 +686,11 @@ class MLService {
}
}
if (instruction.shouldRunPets && rustPets) {
- await mlDataDB.markPetIndexed(result.fileId, petMlVersion);
+ final hasPetFaces =
+ result.petFaces != null && result.petFaces!.isNotEmpty;
+ if (!hasPetFaces) {
+ await mlDataDB.bulkInsertPetFaces([DBPetFace.empty(result.fileId)]);
+ }
}
_logger.info("ML result for fileID ${result.fileId} stored remote+local");
return actuallyRanML;
@@ -725,7 +721,9 @@ class MLService {
);
}
if (instruction.shouldRunPets) {
- await mlDataDB.markPetIndexed(instruction.fileKey, petMlVersion);
+ await mlDataDB.bulkInsertPetFaces(
+ [DBPetFace.empty(instruction.fileKey, error: true)],
+ );
}
return true;
}