Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Rearrange

Manav Rathi569808c2916f
diff --git a/web/apps/photos/src/services/machineLearning/laplacianBlurDetectionService.ts b/web/apps/photos/src/services/machineLearning/laplacianBlurDetectionService.ts
index 71641cdb6c..d4c35c97f1 100644
--- a/web/apps/photos/src/services/machineLearning/laplacianBlurDetectionService.ts
+++ b/web/apps/photos/src/services/machineLearning/laplacianBlurDetectionService.ts
@@ -22,75 +22,121 @@ export const detectBlur = (
i,
);
const laplacian = applyLaplacian(faceImage, direction);
- const variance = calculateVariance(laplacian);
- blurValues.push(variance);
+ blurValues.push(matrixVariance(laplacian));
}
return blurValues;
};
-const calculateVariance = (matrix: number[][]): number => {
- const numRows = matrix.length;
- const numCols = matrix[0].length;
- const totalElements = numRows * numCols;
+type FaceDirection = "left" | "right" | "straight";
- // Calculate the mean
- let mean: number = 0;
- matrix.forEach((row) => {
- row.forEach((value) => {
- mean += value;
- });
- });
- mean /= totalElements;
+const faceDirection = (face: Face): FaceDirection => {
+ const landmarks = face.detection.landmarks;
+ const leftEye = landmarks[0];
+ const rightEye = landmarks[1];
+ const nose = landmarks[2];
+ const leftMouth = landmarks[3];
+ const rightMouth = landmarks[4];
- // Calculate the variance
- let variance: number = 0;
- matrix.forEach((row) => {
- row.forEach((value) => {
- const diff: number = value - mean;
- variance += diff * diff;
- });
- });
- variance /= totalElements;
+ const eyeDistanceX = Math.abs(rightEye.x - leftEye.x);
+ const eyeDistanceY = Math.abs(rightEye.y - leftEye.y);
+ const mouthDistanceY = Math.abs(rightMouth.y - leftMouth.y);
- return variance;
+ const faceIsUpright =
+ Math.max(leftEye.y, rightEye.y) + 0.5 * eyeDistanceY < nose.y &&
+ nose.y + 0.5 * mouthDistanceY < Math.min(leftMouth.y, rightMouth.y);
+
+ const noseStickingOutLeft =
+ nose.x < Math.min(leftEye.x, rightEye.x) &&
+ nose.x < Math.min(leftMouth.x, rightMouth.x);
+
+ const noseStickingOutRight =
+ nose.x > Math.max(leftEye.x, rightEye.x) &&
+ nose.x > Math.max(leftMouth.x, rightMouth.x);
+
+ const noseCloseToLeftEye =
+ Math.abs(nose.x - leftEye.x) < 0.2 * eyeDistanceX;
+ const noseCloseToRightEye =
+ Math.abs(nose.x - rightEye.x) < 0.2 * eyeDistanceX;
+
+ if (noseStickingOutLeft || (faceIsUpright && noseCloseToLeftEye)) {
+ return "left";
+ } else if (noseStickingOutRight || (faceIsUpright && noseCloseToRightEye)) {
+ return "right";
+ }
+
+ return "straight";
};
-const padImage = (
+/**
+ * Return a new image by applying a Laplacian blur kernel to each pixel.
+ */
+const applyLaplacian = (
image: number[][],
- removeSideColumns: number = 56,
- direction: FaceDirection = "straight",
+ direction: FaceDirection,
): number[][] => {
- // Exception is removeSideColumns is not even
- if (removeSideColumns % 2 != 0) {
- throw new Error("removeSideColumns must be even");
+ const paddedImage: number[][] = padImage(image, direction);
+ const numRows = paddedImage.length - 2;
+ const numCols = paddedImage[0].length - 2;
+
+ // Create an output image initialized to 0.
+ const outputImage: number[][] = Array.from({ length: numRows }, () =>
+ new Array(numCols).fill(0),
+ );
+
+ // Define the Laplacian kernel.
+ const kernel: number[][] = [
+ [0, 1, 0],
+ [1, -4, 1],
+ [0, 1, 0],
+ ];
+
+ // Apply the kernel to each pixel
+ for (let i = 0; i < numRows; i++) {
+ for (let j = 0; j < numCols; j++) {
+ let sum = 0;
+ for (let ki = 0; ki < 3; ki++) {
+ for (let kj = 0; kj < 3; kj++) {
+ sum += paddedImage[i + ki][j + kj] * kernel[ki][kj];
+ }
+ }
+ // Adjust the output value if necessary (e.g., clipping).
+ outputImage[i][j] = sum;
+ }
}
+
+ return outputImage;
+};
+
+const padImage = (image: number[][], direction: FaceDirection): number[][] => {
+ const removeSideColumns = 56; /* must be even */
+
const numRows = image.length;
const numCols = image[0].length;
const paddedNumCols = numCols + 2 - removeSideColumns;
const paddedNumRows = numRows + 2;
- // Create a new matrix with extra padding
+ // Create a new matrix with extra padding.
const paddedImage: number[][] = Array.from({ length: paddedNumRows }, () =>
new Array(paddedNumCols).fill(0),
);
- // Copy original image into the center of the padded image
if (direction === "straight") {
+ // Copy original image into the center of the padded image.
for (let i = 0; i < numRows; i++) {
for (let j = 0; j < paddedNumCols - 2; j++) {
paddedImage[i + 1][j + 1] =
image[i][j + Math.round(removeSideColumns / 2)];
}
}
- } // If the face is facing left, we only take the right side of the face image
- else if (direction === "left") {
+ } else if (direction === "left") {
+ // If the face is facing left, we only take the right side of the face image.
for (let i = 0; i < numRows; i++) {
for (let j = 0; j < paddedNumCols - 2; j++) {
paddedImage[i + 1][j + 1] = image[i][j + removeSideColumns];
}
}
- } // If the face is facing right, we only take the left side of the face image
- else if (direction === "right") {
+ } else if (direction === "right") {
+ // If the face is facing right, we only take the left side of the face image.
for (let i = 0; i < numRows; i++) {
for (let j = 0; j < paddedNumCols - 2; j++) {
paddedImage[i + 1][j + 1] = image[i][j];
@@ -113,81 +159,29 @@ const padImage = (
return paddedImage;
};
-const applyLaplacian = (
- image: number[][],
- direction: FaceDirection = "straight",
-): number[][] => {
- const paddedImage: number[][] = padImage(image, undefined, direction);
- const numRows = paddedImage.length - 2;
- const numCols = paddedImage[0].length - 2;
-
- // Create an output image initialized to 0
- const outputImage: number[][] = Array.from({ length: numRows }, () =>
- new Array(numCols).fill(0),
- );
-
- // Define the Laplacian kernel
- const kernel: number[][] = [
- [0, 1, 0],
- [1, -4, 1],
- [0, 1, 0],
- ];
-
- // Apply the kernel to each pixel
- for (let i = 0; i < numRows; i++) {
- for (let j = 0; j < numCols; j++) {
- let sum = 0;
- for (let ki = 0; ki < 3; ki++) {
- for (let kj = 0; kj < 3; kj++) {
- sum += paddedImage[i + ki][j + kj] * kernel[ki][kj];
- }
- }
- // Adjust the output value if necessary (e.g., clipping)
- outputImage[i][j] = sum;
- }
- }
-
- return outputImage;
-};
-
-type FaceDirection = "left" | "right" | "straight";
-
-const faceDirection = (face: Face): FaceDirection => {
- const landmarks = face.detection.landmarks;
- const leftEye = landmarks[0];
- const rightEye = landmarks[1];
- const nose = landmarks[2];
- const leftMouth = landmarks[3];
- const rightMouth = landmarks[4];
-
- const eyeDistanceX = Math.abs(rightEye.x - leftEye.x);
- const eyeDistanceY = Math.abs(rightEye.y - leftEye.y);
- const mouthDistanceY = Math.abs(rightMouth.y - leftMouth.y);
-
- const faceIsUpright =
- Math.max(leftEye.y, rightEye.y) + 0.5 * eyeDistanceY < nose.y &&
- nose.y + 0.5 * mouthDistanceY < Math.min(leftMouth.y, rightMouth.y);
-
- const noseStickingOutLeft =
- nose.x < Math.min(leftEye.x, rightEye.x) &&
- nose.x < Math.min(leftMouth.x, rightMouth.x);
-
- const noseStickingOutRight =
- nose.x > Math.max(leftEye.x, rightEye.x) &&
- nose.x > Math.max(leftMouth.x, rightMouth.x);
+const matrixVariance = (matrix: number[][]): number => {
+ const numRows = matrix.length;
+ const numCols = matrix[0].length;
+ const totalElements = numRows * numCols;
- const noseCloseToLeftEye =
- Math.abs(nose.x - leftEye.x) < 0.2 * eyeDistanceX;
- const noseCloseToRightEye =
- Math.abs(nose.x - rightEye.x) < 0.2 * eyeDistanceX;
+ // Calculate the mean.
+ let mean: number = 0;
+ matrix.forEach((row) => {
+ row.forEach((value) => {
+ mean += value;
+ });
+ });
+ mean /= totalElements;
- // if (faceIsUpright && (noseStickingOutLeft || noseCloseToLeftEye)) {
- if (noseStickingOutLeft || (faceIsUpright && noseCloseToLeftEye)) {
- return "left";
- // } else if (faceIsUpright && (noseStickingOutRight || noseCloseToRightEye)) {
- } else if (noseStickingOutRight || (faceIsUpright && noseCloseToRightEye)) {
- return "right";
- }
+ // Calculate the variance.
+ let variance: number = 0;
+ matrix.forEach((row) => {
+ row.forEach((value) => {
+ const diff: number = value - mean;
+ variance += diff * diff;
+ });
+ });
+ variance /= totalElements;
- return "straight";
+ return variance;
};