[mob][photos] Hook faces into MachineLearningController

This commit is contained in:
laurenspriem 2024-05-14 18:09:38 +05:30
parent 17696c6665
commit df1ca5d583
2 changed files with 28 additions and 7 deletions

View file

@ -1,6 +1,6 @@
import "dart:async"; import "dart:async";
import "dart:developer" as dev show log; import "dart:developer" as dev show log;
import "dart:io" show File; import "dart:io" show File, Platform;
import "dart:isolate"; import "dart:isolate";
import "dart:math" show min; import "dart:math" show min;
import "dart:typed_data" show Uint8List, Float32List, ByteData; import "dart:typed_data" show Uint8List, Float32List, ByteData;
@ -16,6 +16,7 @@ import "package:photos/core/configuration.dart";
import "package:photos/core/event_bus.dart"; import "package:photos/core/event_bus.dart";
import "package:photos/db/files_db.dart"; import "package:photos/db/files_db.dart";
import "package:photos/events/diff_sync_complete_event.dart"; import "package:photos/events/diff_sync_complete_event.dart";
import "package:photos/events/machine_learning_control_event.dart";
import "package:photos/extensions/list.dart"; import "package:photos/extensions/list.dart";
import "package:photos/extensions/stop_watch.dart"; import "package:photos/extensions/stop_watch.dart";
import "package:photos/face/db.dart"; import "package:photos/face/db.dart";
@ -40,6 +41,7 @@ import 'package:photos/services/machine_learning/face_ml/face_ml_exceptions.dart
import 'package:photos/services/machine_learning/face_ml/face_ml_result.dart'; import 'package:photos/services/machine_learning/face_ml/face_ml_result.dart';
import 'package:photos/services/machine_learning/file_ml/file_ml.dart'; import 'package:photos/services/machine_learning/file_ml/file_ml.dart';
import 'package:photos/services/machine_learning/file_ml/remote_fileml_service.dart'; import 'package:photos/services/machine_learning/file_ml/remote_fileml_service.dart';
import "package:photos/services/machine_learning/machine_learning_controller.dart";
import "package:photos/services/search_service.dart"; import "package:photos/services/search_service.dart";
import "package:photos/utils/file_util.dart"; import "package:photos/utils/file_util.dart";
import 'package:photos/utils/image_ml_isolate.dart'; import 'package:photos/utils/image_ml_isolate.dart';
@ -89,6 +91,9 @@ class FaceMlService {
final int _remoteFetchLimit = 100; final int _remoteFetchLimit = 100;
Future<void> init({bool initializeImageMlIsolate = false}) async { Future<void> init({bool initializeImageMlIsolate = false}) async {
if (LocalSettings.instance.isFaceIndexingEnabled == false) {
return;
}
return _initLock.synchronized(() async { return _initLock.synchronized(() async {
if (isInitialized) { if (isInitialized) {
return; return;
@ -114,6 +119,19 @@ class FaceMlService {
} }
isInitialized = true; isInitialized = true;
/// hooking FaceML into [MachineLearningController]
if (Platform.isAndroid) {
Bus.instance.on<MachineLearningControlEvent>().listen((event) {
if (event.shouldRun) {
unawaited(indexAllImages());
} else {
pauseIndexing();
}
});
} else {
unawaited(indexAllImages());
}
}); });
} }
@ -596,9 +614,10 @@ class FaceMlService {
return; return;
} else { } else {
_logger.severe( _logger.severe(
"Failed to fetch embeddings for files after multiple retries", "Failed to fetch embeddings for files after multiple retries",
e, e,
s,); s,
);
rethrow; rethrow;
} }
} }
@ -630,7 +649,7 @@ class FaceMlService {
stopwatch.stop(); stopwatch.stop();
_logger.info( _logger.info(
"`indexAllImages()` finished. Analyzed $fileAnalyzedCount images, in ${stopwatch.elapsed.inSeconds} seconds (avg of ${stopwatch.elapsed.inSeconds / fileAnalyzedCount} seconds per image, skipped $fileSkippedCount images)", "`indexAllImages()` finished. Analyzed $fileAnalyzedCount images, in ${stopwatch.elapsed.inSeconds} seconds (avg of ${stopwatch.elapsed.inSeconds / fileAnalyzedCount} seconds per image, skipped $fileSkippedCount images. MLController status: ${MachineLearningController.instance.canRunML})",
); );
// Dispose of all the isolates // Dispose of all the isolates
@ -1328,7 +1347,7 @@ class FaceMlService {
} }
bool _skipAnalysisEnteFile(EnteFile enteFile, Map<int, int> indexedFileIds) { bool _skipAnalysisEnteFile(EnteFile enteFile, Map<int, int> indexedFileIds) {
if (isImageIndexRunning == false) { if (isImageIndexRunning == false || MachineLearningController.instance.canRunML == false) {
return true; return true;
} }
// Skip if the file is not uploaded or not owned by the user // Skip if the file is not uploaded or not owned by the user

View file

@ -25,6 +25,8 @@ class MachineLearningController {
bool _isRunningML = false; bool _isRunningML = false;
late Timer _userInteractionTimer; late Timer _userInteractionTimer;
get canRunML => _isDeviceHealthy && !_isUserInteracting;
void init() { void init() {
if (Platform.isAndroid) { if (Platform.isAndroid) {
_startInteractionTimer(); _startInteractionTimer();
@ -52,7 +54,7 @@ class MachineLearningController {
} }
void _fireControlEvent() { void _fireControlEvent() {
final shouldRunML = _isDeviceHealthy && !_isUserInteracting; final shouldRunML = canRunML;
if (shouldRunML != _isRunningML) { if (shouldRunML != _isRunningML) {
_isRunningML = shouldRunML; _isRunningML = shouldRunML;
_logger.info( _logger.info(