From ff5dc490f86f1e7fb0bf57010b79d037c897beeb Mon Sep 17 00:00:00 2001 From: laurenspriem Date: Thu, 4 Jul 2024 09:14:31 +0530 Subject: [PATCH] [mob][photos] Small changes --- .../services/machine_learning/ml_service.dart | 7 +- mobile/lib/utils/ml_util.dart | 101 +++++++----------- 2 files changed, 44 insertions(+), 64 deletions(-) diff --git a/mobile/lib/services/machine_learning/ml_service.dart b/mobile/lib/services/machine_learning/ml_service.dart index e9bdd74241..4bfc2e5de1 100644 --- a/mobile/lib/services/machine_learning/ml_service.dart +++ b/mobile/lib/services/machine_learning/ml_service.dart @@ -5,7 +5,6 @@ import "dart:isolate"; import "dart:math" show min; import "dart:typed_data" show Uint8List, ByteData; -import "package:computer/computer.dart"; import "package:dart_ui_isolate/dart_ui_isolate.dart"; import "package:flutter/foundation.dart" show debugPrint; import "package:logging/logging.dart"; @@ -68,8 +67,6 @@ class FaceMlService { final _functionLock = Lock(); final _initIsolateLock = Lock(); - final _computer = Computer.shared(); - bool _isInitialized = false; bool _isModelsInitialized = false; bool _isIsolateSpawned = false; @@ -132,6 +129,7 @@ class FaceMlService { Future sync() async { await FaceRecognitionService.instance.sync(); + await SemanticSearchService.instance.sync(); } Future runAllFaceML({bool force = false}) async { @@ -385,7 +383,8 @@ class FaceMlService { } } - Future processImage(FileMLInstruction instruction) async { // TODO: clean this function up + Future processImage(FileMLInstruction instruction) async { + // TODO: clean this function up _logger.info( "`processImage` start processing image with uploadedFileID: ${instruction.enteFile.uploadedFileID}", ); diff --git a/mobile/lib/utils/ml_util.dart b/mobile/lib/utils/ml_util.dart index fdb578d7d4..301ce40f2c 100644 --- a/mobile/lib/utils/ml_util.dart +++ b/mobile/lib/utils/ml_util.dart @@ -52,8 +52,10 @@ Future> getFilesForMlIndexing() async { final List hiddenFilesToIndex = []; for (final EnteFile enteFile in enteFiles) { final skip = _skipAnalysisEnteFile(enteFile); - final shouldRunFaces = _shouldRunIndexing(enteFile, faceIndexedFileIDs); - final shouldRunClip = _shouldRunIndexing(enteFile, clipIndexedFileIDs); + final shouldRunFaces = + _shouldRunIndexing(enteFile, faceIndexedFileIDs, faceMlVersion); + final shouldRunClip = + _shouldRunIndexing(enteFile, clipIndexedFileIDs, clipMlVersion); if (skip && !shouldRunFaces && !shouldRunClip) { continue; } @@ -70,8 +72,10 @@ Future> getFilesForMlIndexing() async { } for (final EnteFile enteFile in hiddenFiles) { final skip = _skipAnalysisEnteFile(enteFile); - final shouldRunFaces = _shouldRunIndexing(enteFile, faceIndexedFileIDs); - final shouldRunClip = _shouldRunIndexing(enteFile, clipIndexedFileIDs); + final shouldRunFaces = + _shouldRunIndexing(enteFile, faceIndexedFileIDs, faceMlVersion); + final shouldRunClip = + _shouldRunIndexing(enteFile, clipIndexedFileIDs, clipMlVersion); if (skip && !shouldRunFaces && !shouldRunClip) { continue; } @@ -100,65 +104,38 @@ Future> getIndexableFileIDs() async { return fileIDs.toSet(); } -Future getImagePathForML( - EnteFile enteFile, { - FileDataForML typeOfData = FileDataForML.fileData, -}) async { +Future getImagePathForML(EnteFile enteFile) async { String? imagePath; - switch (typeOfData) { - case FileDataForML.fileData: - final stopwatch = Stopwatch()..start(); - File? file; - if (enteFile.fileType == FileType.video) { - try { - file = await getThumbnailForUploadedFile(enteFile); - } on PlatformException catch (e, s) { - _logger.severe( - "Could not get thumbnail for $enteFile due to PlatformException", - e, - s, - ); - throw ThumbnailRetrievalException(e.toString(), s); - } - } else { - try { - file = await getFile(enteFile, isOrigin: true); - } catch (e, s) { - _logger.severe( - "Could not get file for $enteFile", - e, - s, - ); - } - } - if (file == null) { - _logger.warning( - "Could not get file for $enteFile of type ${enteFile.fileType.toString()}", - ); - break; - } - imagePath = file.path; - stopwatch.stop(); - _logger.info( - "Getting file data for uploadedFileID ${enteFile.uploadedFileID} took ${stopwatch.elapsedMilliseconds} ms", + final stopwatch = Stopwatch()..start(); + File? file; + if (enteFile.fileType == FileType.video) { + try { + file = await getThumbnailForUploadedFile(enteFile); + } on PlatformException catch (e, s) { + _logger.severe( + "Could not get thumbnail for $enteFile due to PlatformException", + e, + s, ); - break; - - case FileDataForML.thumbnailData: - final stopwatch = Stopwatch()..start(); - final File? thumbnail = await getThumbnailForUploadedFile(enteFile); - if (thumbnail == null) { - _logger.warning("Could not get thumbnail for $enteFile"); - break; - } - imagePath = thumbnail.path; - stopwatch.stop(); - _logger.info( - "Getting thumbnail data for uploadedFileID ${enteFile.uploadedFileID} took ${stopwatch.elapsedMilliseconds} ms", + throw ThumbnailRetrievalException(e.toString(), s); + } + } else { + try { + file = await getFile(enteFile, isOrigin: true); + } catch (e, s) { + _logger.severe( + "Could not get file for $enteFile", + e, + s, ); - break; + } } + imagePath = file?.path; + stopwatch.stop(); + _logger.info( + "Getting file data for uploadedFileID ${enteFile.uploadedFileID} took ${stopwatch.elapsedMilliseconds} ms", + ); if (imagePath == null) { _logger.warning( @@ -182,9 +159,13 @@ bool _skipAnalysisEnteFile(EnteFile enteFile) { return false; } -bool _shouldRunIndexing(EnteFile enteFile, Map indexedFileIds) { +bool _shouldRunIndexing( + EnteFile enteFile, + Map indexedFileIds, + int newestVersion, +) { final id = enteFile.uploadedFileID!; - return !indexedFileIds.containsKey(id) || indexedFileIds[id]! < faceMlVersion; + return !indexedFileIds.containsKey(id) || indexedFileIds[id]! < newestVersion; } void normalizeEmbedding(List embedding) {