Skip to content

armgit / ente

Public Imported from GitHub
0

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

Fix P1/P2 review issues in pet ML pipeline

Amrithesh Kakkoth Ente04c5cc66c8c8
diff --git a/mobile/apps/photos/lib/db/ml/db.dart b/mobile/apps/photos/lib/db/ml/db.dart
index 8884c6a1c1..ca75c42db4 100644
--- a/mobile/apps/photos/lib/db/ml/db.dart
+++ b/mobile/apps/photos/lib/db/ml/db.dart
@@ -241,9 +241,9 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
const String sql = '''
INSERT INTO $petBodiesTable (
- $fileIDColumn, $petBodyIDColumn, $detectionColumn, $bodyVectorIdColumn, $speciesColumn, $faceScore, $imageHeight, $imageWidth, $mlVersionColumn
+ $fileIDColumn, $petBodyIDColumn, $detectionColumn, $bodyVectorIdColumn, $speciesColumn, $bodyScore, $imageHeight, $imageWidth, $mlVersionColumn
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
- ON CONFLICT($fileIDColumn, $petBodyIDColumn) DO UPDATE SET $detectionColumn = excluded.$detectionColumn, $bodyVectorIdColumn = excluded.$bodyVectorIdColumn, $speciesColumn = excluded.$speciesColumn, $faceScore = excluded.$faceScore, $imageHeight = excluded.$imageHeight, $imageWidth = excluded.$imageWidth, $mlVersionColumn = excluded.$mlVersionColumn
+ ON CONFLICT($fileIDColumn, $petBodyIDColumn) DO UPDATE SET $detectionColumn = excluded.$detectionColumn, $bodyVectorIdColumn = excluded.$bodyVectorIdColumn, $speciesColumn = excluded.$speciesColumn, $bodyScore = excluded.$bodyScore, $imageHeight = excluded.$imageHeight, $imageWidth = excluded.$imageWidth, $mlVersionColumn = excluded.$mlVersionColumn
''';
final parameterSets = batch.map((obj) {
final map = obj.toMap();
@@ -253,7 +253,7 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
map[detectionColumn],
map[bodyVectorIdColumn],
map[speciesColumn],
- map[faceScore],
+ map[bodyScore],
map[imageHeight],
map[imageWidth],
map[mlVersionColumn],
@@ -2398,17 +2398,19 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
Future<void> deletePetDataForFiles(List<int> fileIDs) async {
if (fileIDs.isEmpty) return;
final db = await asyncDB;
- final ids = fileIDs.join(", ");
+ final placeholders = List.filled(fileIDs.length, '?').join(', ');
// Collect vector IDs grouped by species before deleting rows,
// so we can remove them from the usearch indexes and mapping tables.
final faceRows = await db.getAll(
'SELECT $petFaceIDColumn, $faceVectorIdColumn, $speciesColumn '
- 'FROM $petFacesTable WHERE $fileIDColumn IN ($ids)',
+ 'FROM $petFacesTable WHERE $fileIDColumn IN ($placeholders)',
+ fileIDs,
);
final bodyRows = await db.getAll(
'SELECT $petBodyIDColumn, $bodyVectorIdColumn, $speciesColumn '
- 'FROM $petBodiesTable WHERE $fileIDColumn IN ($ids)',
+ 'FROM $petBodiesTable WHERE $fileIDColumn IN ($placeholders)',
+ fileIDs,
);
// Group face vector IDs by species for targeted vector DB deletion.
@@ -2484,10 +2486,12 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
);
}
await tx.execute(
- 'DELETE FROM $petFacesTable WHERE $fileIDColumn IN ($ids)',
+ 'DELETE FROM $petFacesTable WHERE $fileIDColumn IN ($placeholders)',
+ fileIDs,
);
await tx.execute(
- 'DELETE FROM $petBodiesTable WHERE $fileIDColumn IN ($ids)',
+ 'DELETE FROM $petBodiesTable WHERE $fileIDColumn IN ($placeholders)',
+ fileIDs,
);
});
}
diff --git a/mobile/apps/photos/lib/db/ml/schema.dart b/mobile/apps/photos/lib/db/ml/schema.dart
index 6b40e73684..30feb458e7 100644
--- a/mobile/apps/photos/lib/db/ml/schema.dart
+++ b/mobile/apps/photos/lib/db/ml/schema.dart
@@ -216,6 +216,9 @@ const deletePetFacesTable = 'DELETE FROM $petFacesTable';
// ── Pet Bodies Table ──
const petBodiesTable = 'pet_bodies';
+// Alias so pet-body code doesn't reference the face-specific constant name.
+// The underlying SQL column is the same ('score').
+const bodyScore = faceScore;
const createPetBodiesTable = '''CREATE TABLE IF NOT EXISTS $petBodiesTable (
$fileIDColumn INTEGER NOT NULL,
@@ -223,7 +226,7 @@ const createPetBodiesTable = '''CREATE TABLE IF NOT EXISTS $petBodiesTable (
$detectionColumn TEXT NOT NULL,
$bodyVectorIdColumn INTEGER UNIQUE,
$speciesColumn INTEGER NOT NULL,
- $faceScore REAL NOT NULL,
+ $bodyScore REAL NOT NULL,
$imageHeight INTEGER NOT NULL DEFAULT 0,
$imageWidth INTEGER NOT NULL DEFAULT 0,
$mlVersionColumn INTEGER NOT NULL DEFAULT -1,
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 75144c44f3..44e9f43608 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
@@ -605,7 +605,7 @@ class MLService {
);
}
}
- if (!isOffline) {
+ if (!isOffline && (result.facesRan || result.clipRan)) {
// Storing results on remote
await fileDataService.putFileData(
instruction.file,
@@ -654,6 +654,10 @@ class MLService {
dbPetFaces,
result.petFaces!,
);
+ } else if (instruction.shouldRunPets) {
+ // No pet faces detected; insert empty marker so the file is
+ // considered pet-indexed (mirrors Face.empty for human faces).
+ await mlDataDB.bulkInsertPetFaces([DBPetFace.empty(result.fileId)]);
}
if (result.petBodies != null && result.petBodies!.isNotEmpty) {
@@ -687,11 +691,6 @@ class MLService {
);
}
}
- if (instruction.shouldRunPets &&
- result.petFaces != null &&
- result.petFaces!.isEmpty) {
- await mlDataDB.bulkInsertPetFaces([DBPetFace.empty(result.fileId)]);
- }
_logger.info("ML result for fileID ${result.fileId} stored remote+local");
return actuallyRanML;
} catch (e, s) {
diff --git a/mobile/apps/photos/lib/ui/viewer/file_details/file_info_pets_item_widget.dart b/mobile/apps/photos/lib/ui/viewer/file_details/file_info_pets_item_widget.dart
index c62c56dc52..8d037bc4b9 100644
--- a/mobile/apps/photos/lib/ui/viewer/file_details/file_info_pets_item_widget.dart
+++ b/mobile/apps/photos/lib/ui/viewer/file_details/file_info_pets_item_widget.dart
@@ -71,7 +71,8 @@ class _PetsItemWidgetState extends State<PetsItemWidget> {
final faces = <Face>[];
for (final dbPetFace in dbPetFaces) {
final json = jsonDecode(dbPetFace.detection) as Map<String, dynamic>;
- final boxList = (json['box'] as List).cast<double>();
+ final boxList =
+ (json['box'] as List).map((e) => (e as num).toDouble()).toList();
final detection = Detection(
box: FaceBox(
x: boxList[0],