From caa72ba83022de5b48355ab9870497b2f598a42a Mon Sep 17 00:00:00 2001 From: ashilkn Date: Fri, 26 Apr 2024 15:44:07 +0530 Subject: [PATCH] [mob][photos] add option to pass decoded image to face thumbnail generation methods to avoid unnecessary decoding when possible --- mobile/lib/utils/face/face_util.dart | 57 +++++++++++++--------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/mobile/lib/utils/face/face_util.dart b/mobile/lib/utils/face/face_util.dart index 99ec045121..3d7853f08d 100644 --- a/mobile/lib/utils/face/face_util.dart +++ b/mobile/lib/utils/face/face_util.dart @@ -12,28 +12,14 @@ final _computer = Computer.shared(); ///Convert img.Image to ui.Image and use RawImage to display. Future> generateImgFaceThumbnails( String imagePath, - List faceBoxes, -) async { + List faceBoxes, { + ///Pass decodedImage decoded by [decodeToImgImage] to avoid decoding image + ///multiple times if all faces are from the same image (eg: File info). + img.Image? decodedImage, +}) async { final faceThumbnails = []; - img.Image? image = - await _computer.compute(_decodeImageFile, param: {"filePath": imagePath}); - - if (image == null) { - _logger.info( - "Failed to decode image. Compressing to jpg and decoding", - ); - final compressedJPGImage = - await FlutterImageCompress.compressWithFile(imagePath); - image = await _computer.compute( - _decodeJpg, - param: {"image": compressedJPGImage}, - ); - - if (image == null) { - throw Exception("Failed to decode image"); - } - } + final image = decodedImage ?? await decodeToImgImage(imagePath); for (FaceBox faceBox in faceBoxes) { final croppedImage = cropFaceBoxFromImage(image, faceBox); @@ -45,8 +31,23 @@ Future> generateImgFaceThumbnails( Future> generateJpgFaceThumbnails( String imagePath, - List faceBoxes, -) async { + List faceBoxes, { + ///Pass decodedImage decoded by [decodeToImgImage] to avoid decoding image + ///multiple times if all faces are from the same image (eg: File info). + img.Image? decodedImage, +}) async { + final image = decodedImage ?? await decodeToImgImage(imagePath); + final croppedImages = []; + for (FaceBox faceBox in faceBoxes) { + final croppedImage = cropFaceBoxFromImage(image, faceBox); + croppedImages.add(croppedImage); + } + + return await _computer + .compute(_encodeImagesToJpg, param: {"images": croppedImages}); +} + +Future decodeToImgImage(String imagePath) async { img.Image? image = await _computer.compute(_decodeImageFile, param: {"filePath": imagePath}); @@ -63,16 +64,12 @@ Future> generateJpgFaceThumbnails( if (image == null) { throw Exception("Failed to decode image"); + } else { + return image; } + } else { + return image; } - final croppedImages = []; - for (FaceBox faceBox in faceBoxes) { - final croppedImage = cropFaceBoxFromImage(image, faceBox); - croppedImages.add(croppedImage); - } - - return await _computer - .compute(_encodeImagesToJpg, param: {"images": croppedImages}); } /// Returns an Image from 'package:image/image.dart'