π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mob][photos] Simplify SimilarityTransform
laurensprieme2da01dd5b4a
diff --git a/mobile/lib/services/machine_learning/face_ml/face_alignment/similarity_transform.dart b/mobile/lib/services/machine_learning/face_ml/face_alignment/similarity_transform.dartindex 8d995342ba..94aa6c3b3b 100644--- a/mobile/lib/services/machine_learning/face_ml/face_alignment/similarity_transform.dart+++ b/mobile/lib/services/machine_learning/face_ml/face_alignment/similarity_transform.dart@@ -8,54 +8,29 @@ import 'package:photos/services/machine_learning/face_ml/face_alignment/alignmen/// The class estimates the parameters of the similarity transformation via the `estimate` function./// After estimation, the transformation can be applied to an image using the `warpAffine` function.class SimilarityTransform {- Matrix _params = Matrix.fromList([- [1.0, 0.0, 0.0],- [0.0, 1.0, 0.0],- [0, 0, 1],- ]);- List<double> _center = <double>[0, 0]; // [x, y]- double _size = 1; // 1 / scale- double _rotation = 0; // atan2(simRotation[1][0], simRotation[0][0]);-- final mobilefacenetIdeal4Landmarks = [+ static const _mobilefacenetIdeal4Landmarks = [<double>[38.2946, 51.6963],<double>[73.5318, 51.5014],<double>[56.0252, 71.7366],<double>[56.1396, 92.2848],];- final mobilefacenetIdeal5Landmarks = [+ static const _mobilefacenetIdeal5Landmarks = [<double>[38.2946, 51.6963],<double>[73.5318, 51.5014],<double>[56.0252, 71.7366],<double>[41.5493, 92.3655],<double>[70.7299, 92.2041],];- get mobilefacenetIdealNormalized4 => mobilefacenetIdeal4Landmarks+ static get mobilefacenetIdealNormalized4 => _mobilefacenetIdeal4Landmarks.map((list) => list.map((value) => value / 112.0).toList()).toList();- get mobilefacenetIdealNormalized5 => mobilefacenetIdeal5Landmarks+ static get mobilefacenetIdealNormalized5 => _mobilefacenetIdeal5Landmarks.map((list) => list.map((value) => value / 112.0).toList()).toList();- List<List<double>> get paramsList => _params.to2DList();-- // singleton pattern- SimilarityTransform._privateConstructor();- static final instance = SimilarityTransform._privateConstructor();- factory SimilarityTransform() => instance;-- void _cleanParams() {- _params = Matrix.fromList([- [1.0, 0.0, 0.0],- [0.0, 1.0, 0.0],- [0, 0, 1],- ]);- _center = <double>[0, 0];- _size = 1;- _rotation = 0;- }+ SimilarityTransform();- /// Function to estimate the parameters of the affine transformation. These parameters are stored in the class variable params.+ /// Function to estimate the parameters of the affine transformation.////// Returns a tuple of (AlignmentResult, bool). The bool indicates whether the parameters are valid or not.///@@ -66,23 +41,18 @@ class SimilarityTransform {/// returns false if the parameters cannot be estimated. The function/// estimates the parameters by solving a least-squares problem using/// the Umeyama algorithm, via [_umeyama].- (AlignmentResult, bool) estimate(List<List<double>> src) {- _cleanParams();+ static (AlignmentResult, bool) estimate(List<List<double>> src) {final (params, center, size, rotation) =_umeyama(src, mobilefacenetIdealNormalized5, true);- _params = params;- _center = center;- _size = size;- _rotation = rotation;final alignmentResult = AlignmentResult(- affineMatrix: paramsList,- center: _center,- size: _size,- rotation: _rotation,+ affineMatrix: params.to2DList(),+ center: center,+ size: size,+ rotation: rotation,);// We check for NaN in the transformation matrix params.final isNoNanInParam =- !_params.asFlattenedList.any((element) => element.isNaN);+ !params.asFlattenedList.any((element) => element.isNaN);return (alignmentResult, isNoNanInParam);}diff --git a/mobile/lib/utils/image_ml_util.dart b/mobile/lib/utils/image_ml_util.dartindex ccec955fc7..ef1af56383 100644--- a/mobile/lib/utils/image_ml_util.dart+++ b/mobile/lib/utils/image_ml_util.dart@@ -222,7 +222,7 @@ Future<(Float32List, List<AlignmentResult>, List<bool>, List<double>, Size)>int alignedImageIndex = 0;for (final face in absoluteFaces) {final (alignmentResult, correctlyEstimated) =- SimilarityTransform.instance.estimate(face.allKeypoints);+ SimilarityTransform.estimate(face.allKeypoints);if (!correctlyEstimated) {alignedImageIndex += 3 * width * height;alignmentResults.add(AlignmentResult.empty());