image_ml_isolate.dart 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. import 'dart:async';
  2. import "dart:io" show File;
  3. import 'dart:isolate';
  4. import 'dart:typed_data' show Float32List, Uint8List;
  5. import 'dart:ui';
  6. import "package:flutter/rendering.dart";
  7. import 'package:flutter_isolate/flutter_isolate.dart';
  8. import "package:logging/logging.dart";
  9. import "package:photos/face/model/box.dart";
  10. import 'package:photos/models/ml/ml_typedefs.dart';
  11. import "package:photos/services/face_ml/face_alignment/alignment_result.dart";
  12. import "package:photos/services/face_ml/face_detection/detection.dart";
  13. import "package:photos/utils/image_ml_util.dart";
  14. import "package:synchronized/synchronized.dart";
  15. enum ImageOperation {
  16. @Deprecated("No longer using BlazeFace`")
  17. preprocessBlazeFace,
  18. preprocessYoloOnnx,
  19. preprocessFaceAlign,
  20. preprocessMobileFaceNet,
  21. preprocessMobileFaceNetOnnx,
  22. generateFaceThumbnails,
  23. cropAndPadFace,
  24. }
  25. /// The isolate below uses functions from ["package:photos/utils/image_ml_util.dart"] to preprocess images for ML models.
  26. /// This class is responsible for all image operations needed for ML models. It runs in a separate isolate to avoid jank.
  27. ///
  28. /// It can be accessed through the singleton `ImageConversionIsolate.instance`. e.g. `ImageConversionIsolate.instance.convert(imageData)`
  29. ///
  30. /// IMPORTANT: Make sure to dispose of the isolate when you're done with it with `dispose()`, e.g. `ImageConversionIsolate.instance.dispose();`
  31. class ImageMlIsolate {
  32. // static const String debugName = 'ImageMlIsolate';
  33. final _logger = Logger('ImageMlIsolate');
  34. Timer? _inactivityTimer;
  35. final Duration _inactivityDuration = const Duration(seconds: 60);
  36. int _activeTasks = 0;
  37. final _initLock = Lock();
  38. final _functionLock = Lock();
  39. late FlutterIsolate _isolate;
  40. late ReceivePort _receivePort = ReceivePort();
  41. late SendPort _mainSendPort;
  42. bool isSpawned = false;
  43. // singleton pattern
  44. ImageMlIsolate._privateConstructor();
  45. /// Use this instance to access the ImageConversionIsolate service. Make sure to call `init()` before using it.
  46. /// e.g. `await ImageConversionIsolate.instance.init();`
  47. /// And kill the isolate when you're done with it with `dispose()`, e.g. `ImageConversionIsolate.instance.dispose();`
  48. ///
  49. /// Then you can use `convert()` to get the image, so `ImageConversionIsolate.instance.convert(imageData, imagePath: imagePath)`
  50. static final ImageMlIsolate instance = ImageMlIsolate._privateConstructor();
  51. factory ImageMlIsolate() => instance;
  52. Future<void> init() async {
  53. return _initLock.synchronized(() async {
  54. if (isSpawned) return;
  55. _receivePort = ReceivePort();
  56. try {
  57. _isolate = await FlutterIsolate.spawn(
  58. _isolateMain,
  59. _receivePort.sendPort,
  60. );
  61. _mainSendPort = await _receivePort.first as SendPort;
  62. isSpawned = true;
  63. _resetInactivityTimer();
  64. } catch (e) {
  65. _logger.severe('Could not spawn isolate', e);
  66. isSpawned = false;
  67. }
  68. });
  69. }
  70. Future<void> ensureSpawned() async {
  71. if (!isSpawned) {
  72. await init();
  73. }
  74. }
  75. @pragma('vm:entry-point')
  76. static void _isolateMain(SendPort mainSendPort) async {
  77. final receivePort = ReceivePort();
  78. mainSendPort.send(receivePort.sendPort);
  79. receivePort.listen((message) async {
  80. final functionIndex = message[0] as int;
  81. final function = ImageOperation.values[functionIndex];
  82. final args = message[1] as Map<String, dynamic>;
  83. final sendPort = message[2] as SendPort;
  84. try {
  85. switch (function) {
  86. case ImageOperation.preprocessBlazeFace:
  87. final imageData = args['imageData'] as Uint8List;
  88. final normalize = args['normalize'] as bool;
  89. final int normalization = normalize ? 2 : -1;
  90. final requiredWidth = args['requiredWidth'] as int;
  91. final requiredHeight = args['requiredHeight'] as int;
  92. final qualityIndex = args['quality'] as int;
  93. final maintainAspectRatio = args['maintainAspectRatio'] as bool;
  94. final quality = FilterQuality.values[qualityIndex];
  95. final (result, originalSize, newSize) =
  96. await preprocessImageToMatrix(
  97. imageData,
  98. normalization: normalization,
  99. requiredWidth: requiredWidth,
  100. requiredHeight: requiredHeight,
  101. quality: quality,
  102. maintainAspectRatio: maintainAspectRatio,
  103. );
  104. sendPort.send({
  105. 'inputs': result,
  106. 'originalWidth': originalSize.width,
  107. 'originalHeight': originalSize.height,
  108. 'newWidth': newSize.width,
  109. 'newHeight': newSize.height,
  110. });
  111. case ImageOperation.preprocessYoloOnnx:
  112. final imageData = args['imageData'] as Uint8List;
  113. final normalize = args['normalize'] as bool;
  114. final int normalization = normalize ? 1 : -1;
  115. final requiredWidth = args['requiredWidth'] as int;
  116. final requiredHeight = args['requiredHeight'] as int;
  117. final maintainAspectRatio = args['maintainAspectRatio'] as bool;
  118. final (result, originalSize, newSize) =
  119. await preprocessImageToFloat32ChannelsFirst(
  120. imageData,
  121. normalization: normalization,
  122. requiredWidth: requiredWidth,
  123. requiredHeight: requiredHeight,
  124. maintainAspectRatio: maintainAspectRatio,
  125. );
  126. sendPort.send({
  127. 'inputs': result,
  128. 'originalWidth': originalSize.width,
  129. 'originalHeight': originalSize.height,
  130. 'newWidth': newSize.width,
  131. 'newHeight': newSize.height,
  132. });
  133. case ImageOperation.preprocessFaceAlign:
  134. final imageData = args['imageData'] as Uint8List;
  135. final faceLandmarks =
  136. args['faceLandmarks'] as List<List<List<double>>>;
  137. final List<Uint8List> result = await preprocessFaceAlignToUint8List(
  138. imageData,
  139. faceLandmarks,
  140. );
  141. sendPort.send(List.from(result));
  142. case ImageOperation.preprocessMobileFaceNet:
  143. final imageData = args['imageData'] as Uint8List;
  144. final facesJson = args['facesJson'] as List<Map<String, dynamic>>;
  145. final (
  146. inputs,
  147. alignmentResults,
  148. isBlurs,
  149. blurValues,
  150. originalSize
  151. ) = await preprocessToMobileFaceNetInput(
  152. imageData,
  153. facesJson,
  154. );
  155. final List<Map<String, dynamic>> alignmentResultsJson =
  156. alignmentResults.map((result) => result.toJson()).toList();
  157. sendPort.send({
  158. 'inputs': inputs,
  159. 'alignmentResultsJson': alignmentResultsJson,
  160. 'isBlurs': isBlurs,
  161. 'blurValues': blurValues,
  162. 'originalWidth': originalSize.width,
  163. 'originalHeight': originalSize.height,
  164. });
  165. case ImageOperation.preprocessMobileFaceNetOnnx:
  166. final imagePath = args['imagePath'] as String;
  167. final facesJson = args['facesJson'] as List<Map<String, dynamic>>;
  168. final List<FaceDetectionRelative> relativeFaces = facesJson
  169. .map((face) => FaceDetectionRelative.fromJson(face))
  170. .toList();
  171. final (
  172. inputs,
  173. alignmentResults,
  174. isBlurs,
  175. blurValues,
  176. originalSize
  177. ) = await preprocessToMobileFaceNetFloat32List(
  178. imagePath,
  179. relativeFaces,
  180. );
  181. final List<Map<String, dynamic>> alignmentResultsJson =
  182. alignmentResults.map((result) => result.toJson()).toList();
  183. sendPort.send({
  184. 'inputs': inputs,
  185. 'alignmentResultsJson': alignmentResultsJson,
  186. 'isBlurs': isBlurs,
  187. 'blurValues': blurValues,
  188. 'originalWidth': originalSize.width,
  189. 'originalHeight': originalSize.height,
  190. });
  191. case ImageOperation.generateFaceThumbnails:
  192. final imagePath = args['imagePath'] as String;
  193. final Uint8List imageData = await File(imagePath).readAsBytes();
  194. final faceBoxesJson =
  195. args['faceBoxesList'] as List<Map<String, dynamic>>;
  196. final List<FaceBox> faceBoxes =
  197. faceBoxesJson.map((json) => FaceBox.fromJson(json)).toList();
  198. final List<Uint8List> results = await generateFaceThumbnails(
  199. imageData,
  200. faceBoxes,
  201. );
  202. sendPort.send(List.from(results));
  203. case ImageOperation.cropAndPadFace:
  204. final imageData = args['imageData'] as Uint8List;
  205. final faceBox = args['faceBox'] as List<double>;
  206. final Uint8List result =
  207. await cropAndPadFaceData(imageData, faceBox);
  208. sendPort.send(<dynamic>[result]);
  209. }
  210. } catch (e, stackTrace) {
  211. sendPort
  212. .send({'error': e.toString(), 'stackTrace': stackTrace.toString()});
  213. }
  214. });
  215. }
  216. /// The common method to run any operation in the isolate. It sends the [message] to [_isolateMain] and waits for the result.
  217. Future<dynamic> _runInIsolate(
  218. (ImageOperation, Map<String, dynamic>) message,
  219. ) async {
  220. await ensureSpawned();
  221. return _functionLock.synchronized(() async {
  222. _resetInactivityTimer();
  223. final completer = Completer<dynamic>();
  224. final answerPort = ReceivePort();
  225. _activeTasks++;
  226. _mainSendPort.send([message.$1.index, message.$2, answerPort.sendPort]);
  227. answerPort.listen((receivedMessage) {
  228. if (receivedMessage is Map && receivedMessage.containsKey('error')) {
  229. // Handle the error
  230. final errorMessage = receivedMessage['error'];
  231. final errorStackTrace = receivedMessage['stackTrace'];
  232. final exception = Exception(errorMessage);
  233. final stackTrace = StackTrace.fromString(errorStackTrace);
  234. completer.completeError(exception, stackTrace);
  235. } else {
  236. completer.complete(receivedMessage);
  237. }
  238. });
  239. _activeTasks--;
  240. return completer.future;
  241. });
  242. }
  243. /// Resets a timer that kills the isolate after a certain amount of inactivity.
  244. ///
  245. /// Should be called after initialization (e.g. inside `init()`) and after every call to isolate (e.g. inside `_runInIsolate()`)
  246. void _resetInactivityTimer() {
  247. _inactivityTimer?.cancel();
  248. _inactivityTimer = Timer(_inactivityDuration, () {
  249. if (_activeTasks > 0) {
  250. _logger.info('Tasks are still running. Delaying isolate disposal.');
  251. // Optionally, reschedule the timer to check again later.
  252. _resetInactivityTimer();
  253. } else {
  254. _logger.info(
  255. 'Clustering Isolate has been inactive for ${_inactivityDuration.inSeconds} seconds with no tasks running. Killing isolate.',
  256. );
  257. dispose();
  258. }
  259. });
  260. }
  261. /// Disposes the isolate worker.
  262. void dispose() {
  263. if (!isSpawned) return;
  264. isSpawned = false;
  265. _isolate.kill();
  266. _receivePort.close();
  267. _inactivityTimer?.cancel();
  268. }
  269. /// Preprocesses [imageData] for standard ML models inside a separate isolate.
  270. ///
  271. /// Returns a [Num3DInputMatrix] image usable for ML inference with BlazeFace.
  272. ///
  273. /// Uses [preprocessImageToMatrix] inside the isolate.
  274. Future<(Num3DInputMatrix, Size, Size)> preprocessImageBlazeFace(
  275. Uint8List imageData, {
  276. required bool normalize,
  277. required int requiredWidth,
  278. required int requiredHeight,
  279. FilterQuality quality = FilterQuality.medium,
  280. bool maintainAspectRatio = true,
  281. }) async {
  282. final Map<String, dynamic> results = await _runInIsolate(
  283. (
  284. ImageOperation.preprocessBlazeFace,
  285. {
  286. 'imageData': imageData,
  287. 'normalize': normalize,
  288. 'requiredWidth': requiredWidth,
  289. 'requiredHeight': requiredHeight,
  290. 'quality': quality.index,
  291. 'maintainAspectRatio': maintainAspectRatio,
  292. },
  293. ),
  294. );
  295. final inputs = results['inputs'] as Num3DInputMatrix;
  296. final originalSize = Size(
  297. results['originalWidth'] as double,
  298. results['originalHeight'] as double,
  299. );
  300. final newSize = Size(
  301. results['newWidth'] as double,
  302. results['newHeight'] as double,
  303. );
  304. return (inputs, originalSize, newSize);
  305. }
  306. /// Uses [preprocessImageToFloat32ChannelsFirst] inside the isolate.
  307. Future<(Float32List, Size, Size)> preprocessImageYoloOnnx(
  308. Uint8List imageData, {
  309. required bool normalize,
  310. required int requiredWidth,
  311. required int requiredHeight,
  312. FilterQuality quality = FilterQuality.medium,
  313. bool maintainAspectRatio = true,
  314. }) async {
  315. final Map<String, dynamic> results = await _runInIsolate(
  316. (
  317. ImageOperation.preprocessYoloOnnx,
  318. {
  319. 'imageData': imageData,
  320. 'normalize': normalize,
  321. 'requiredWidth': requiredWidth,
  322. 'requiredHeight': requiredHeight,
  323. 'quality': quality.index,
  324. 'maintainAspectRatio': maintainAspectRatio,
  325. },
  326. ),
  327. );
  328. final inputs = results['inputs'] as Float32List;
  329. final originalSize = Size(
  330. results['originalWidth'] as double,
  331. results['originalHeight'] as double,
  332. );
  333. final newSize = Size(
  334. results['newWidth'] as double,
  335. results['newHeight'] as double,
  336. );
  337. return (inputs, originalSize, newSize);
  338. }
  339. /// Preprocesses [imageData] for face alignment inside a separate isolate, to display the aligned faces. Mostly used for debugging.
  340. ///
  341. /// Returns a list of [Uint8List] images, one for each face, in png format.
  342. ///
  343. /// Uses [preprocessFaceAlignToUint8List] inside the isolate.
  344. ///
  345. /// WARNING: For preprocessing for MobileFaceNet, use [preprocessMobileFaceNet] instead!
  346. Future<List<Uint8List>> preprocessFaceAlign(
  347. Uint8List imageData,
  348. List<FaceDetectionAbsolute> faces,
  349. ) async {
  350. final faceLandmarks = faces.map((face) => face.allKeypoints).toList();
  351. return await _runInIsolate(
  352. (
  353. ImageOperation.preprocessFaceAlign,
  354. {
  355. 'imageData': imageData,
  356. 'faceLandmarks': faceLandmarks,
  357. },
  358. ),
  359. ).then((value) => value.cast<Uint8List>());
  360. }
  361. /// Preprocesses [imageData] for MobileFaceNet input inside a separate isolate.
  362. ///
  363. /// Returns a list of [Num3DInputMatrix] images, one for each face.
  364. ///
  365. /// Uses [preprocessToMobileFaceNetInput] inside the isolate.
  366. @Deprecated("Old method used in TensorFlow Lite")
  367. Future<
  368. (
  369. List<Num3DInputMatrix>,
  370. List<AlignmentResult>,
  371. List<bool>,
  372. List<double>,
  373. Size,
  374. )> preprocessMobileFaceNet(
  375. Uint8List imageData,
  376. List<FaceDetectionRelative> faces,
  377. ) async {
  378. final List<Map<String, dynamic>> facesJson =
  379. faces.map((face) => face.toJson()).toList();
  380. final Map<String, dynamic> results = await _runInIsolate(
  381. (
  382. ImageOperation.preprocessMobileFaceNet,
  383. {
  384. 'imageData': imageData,
  385. 'facesJson': facesJson,
  386. },
  387. ),
  388. );
  389. final inputs = results['inputs'] as List<Num3DInputMatrix>;
  390. final alignmentResultsJson =
  391. results['alignmentResultsJson'] as List<Map<String, dynamic>>;
  392. final alignmentResults = alignmentResultsJson.map((json) {
  393. return AlignmentResult.fromJson(json);
  394. }).toList();
  395. final isBlurs = results['isBlurs'] as List<bool>;
  396. final blurValues = results['blurValues'] as List<double>;
  397. final originalSize = Size(
  398. results['originalWidth'] as double,
  399. results['originalHeight'] as double,
  400. );
  401. return (inputs, alignmentResults, isBlurs, blurValues, originalSize);
  402. }
  403. /// Uses [preprocessToMobileFaceNetFloat32List] inside the isolate.
  404. Future<(Float32List, List<AlignmentResult>, List<bool>, List<double>, Size)>
  405. preprocessMobileFaceNetOnnx(
  406. String imagePath,
  407. List<FaceDetectionRelative> faces,
  408. ) async {
  409. final List<Map<String, dynamic>> facesJson =
  410. faces.map((face) => face.toJson()).toList();
  411. final Map<String, dynamic> results = await _runInIsolate(
  412. (
  413. ImageOperation.preprocessMobileFaceNetOnnx,
  414. {
  415. 'imagePath': imagePath,
  416. 'facesJson': facesJson,
  417. },
  418. ),
  419. );
  420. final inputs = results['inputs'] as Float32List;
  421. final alignmentResultsJson =
  422. results['alignmentResultsJson'] as List<Map<String, dynamic>>;
  423. final alignmentResults = alignmentResultsJson.map((json) {
  424. return AlignmentResult.fromJson(json);
  425. }).toList();
  426. final isBlurs = results['isBlurs'] as List<bool>;
  427. final blurValues = results['blurValues'] as List<double>;
  428. final originalSize = Size(
  429. results['originalWidth'] as double,
  430. results['originalHeight'] as double,
  431. );
  432. return (inputs, alignmentResults, isBlurs, blurValues, originalSize);
  433. }
  434. /// Generates face thumbnails for all [faceBoxes] in [imageData].
  435. ///
  436. /// Uses [generateFaceThumbnails] inside the isolate.
  437. Future<List<Uint8List>> generateFaceThumbnailsForImage(
  438. String imagePath,
  439. List<FaceBox> faceBoxes,
  440. ) async {
  441. final List<Map<String, dynamic>> faceBoxesJson =
  442. faceBoxes.map((box) => box.toJson()).toList();
  443. return await _runInIsolate(
  444. (
  445. ImageOperation.generateFaceThumbnails,
  446. {
  447. 'imagePath': imagePath,
  448. 'faceBoxesList': faceBoxesJson,
  449. },
  450. ),
  451. ).then((value) => value.cast<Uint8List>());
  452. }
  453. @Deprecated('For second pass of BlazeFace, no longer used')
  454. /// Generates cropped and padded image data from [imageData] and a [faceBox].
  455. ///
  456. /// The steps are:
  457. /// 1. Crop the image to the face bounding box
  458. /// 2. Resize this cropped image to a square that is half the BlazeFace input size
  459. /// 3. Pad the image to the BlazeFace input size
  460. ///
  461. /// Uses [cropAndPadFaceData] inside the isolate.
  462. Future<Uint8List> cropAndPadFace(
  463. Uint8List imageData,
  464. List<double> faceBox,
  465. ) async {
  466. return await _runInIsolate(
  467. (
  468. ImageOperation.cropAndPadFace,
  469. {
  470. 'imageData': imageData,
  471. 'faceBox': List<double>.from(faceBox),
  472. },
  473. ),
  474. ).then((value) => value[0] as Uint8List);
  475. }
  476. }