[mob] store sideways face boolean in local face table

This commit is contained in:
laurenspriem 2024-04-19 18:13:35 +05:30
parent a0fa90cb50
commit 01aecb9742
3 changed files with 5 additions and 32 deletions

View file

@ -1,5 +1,4 @@
import 'dart:async';
import "dart:convert" show json;
import "dart:io" show Directory;
import "dart:math";
@ -11,7 +10,6 @@ import 'package:path_provider/path_provider.dart';
import "package:photos/extensions/stop_watch.dart";
import 'package:photos/face/db_fields.dart';
import "package:photos/face/db_model_mappers.dart";
import "package:photos/face/model/detection.dart";
import "package:photos/face/model/face.dart";
import "package:photos/models/file/file.dart";
import "package:photos/services/machine_learning/face_ml/face_clustering/face_info_for_clustering.dart";
@ -455,31 +453,6 @@ class FaceMLDataDB {
);
}
// TODO: remove this method and replace by correct logic during indexing
Future<Map<String, bool>> getFaceIdsToIsSidewaysFaceTEMP() async {
final db = await instance.sqliteAsyncDB;
final List<Map<String, dynamic>> maps = await db.getAll(
'SELECT $faceIDColumn, $faceDetectionColumn FROM $facesTable',
);
final time = DateTime.now();
final Map<String, bool> result = {};
for (final map in maps) {
final faceID = map[faceIDColumn] as String;
final detection =
Detection.fromJson(json.decode(map[faceDetectionColumn] as String));
result[faceID] = detection.faceIsSideways();
}
_logger.info(
'decoding detections and calculating face sideways bools took ${DateTime.now().difference(time).inMilliseconds} ms',
);
final double sidewaysRatio =
result.values.where((e) => e).length / result.length;
_logger.info('sideways face ratio: $sidewaysRatio');
return result;
}
Future<Set<FaceInfoForClustering>> getFaceInfoForClustering({
double minScore = kMinimumQualityFaceScore,
int minClarity = kLaplacianHardThreshold,
@ -493,14 +466,11 @@ class FaceMLDataDB {
);
final db = await instance.sqliteAsyncDB;
final Map<String, bool> faceIdsToIsSideways =
await getFaceIdsToIsSidewaysFaceTEMP();
final Set<FaceInfoForClustering> result = {};
while (true) {
// Query a batch of rows
final List<Map<String, dynamic>> maps = await db.getAll(
'SELECT $faceIDColumn, $faceEmbeddingBlob, $faceScore, $faceBlur FROM $facesTable'
'SELECT $faceIDColumn, $faceEmbeddingBlob, $faceScore, $faceBlur, $isSideways FROM $facesTable'
' WHERE $faceScore > $minScore AND $faceBlur > $minClarity'
' ORDER BY $faceIDColumn'
' DESC LIMIT $batchSize OFFSET $offset',
@ -522,7 +492,7 @@ class FaceMLDataDB {
embeddingBytes: map[faceEmbeddingBlob] as Uint8List,
faceScore: map[faceScore] as double,
blurValue: map[faceBlur] as double,
isSideways: faceIdsToIsSideways[faceID] ?? false,
isSideways: (map[isSideways] as int) == 1,
);
result.add(faceInfo);
}

View file

@ -8,6 +8,7 @@ const faceDetectionColumn = 'detection';
const faceEmbeddingBlob = 'eBlob';
const faceScore = 'score';
const faceBlur = 'blur';
const isSideways = 'is_sideways';
const imageWidth = 'width';
const imageHeight = 'height';
const faceClusterId = 'cluster_id';
@ -20,6 +21,7 @@ const createFacesTable = '''CREATE TABLE IF NOT EXISTS $facesTable (
$faceEmbeddingBlob BLOB NOT NULL,
$faceScore REAL NOT NULL,
$faceBlur REAL NOT NULL DEFAULT $kLapacianDefault,
$isSideways INTEGER NOT NULL DEFAULT 0,
$imageHeight INTEGER NOT NULL DEFAULT 0,
$imageWidth INTEGER NOT NULL DEFAULT 0,
$mlVersionColumn INTEGER NOT NULL DEFAULT -1,

View file

@ -34,6 +34,7 @@ Map<String, dynamic> mapRemoteToFaceDB(Face face) {
).writeToBuffer(),
faceScore: face.score,
faceBlur: face.blur,
isSideways: face.detection.faceIsSideways() ? 1 : 0,
mlVersionColumn: faceMlVersion,
imageWidth: face.fileInfo?.imageWidth ?? 0,
imageHeight: face.fileInfo?.imageHeight ?? 0,