From b3b79c2b90f1adf29d93b9c7b69567a9ee2535c7 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Sat, 18 May 2024 10:48:09 +0530 Subject: [PATCH] Rearrange --- web/apps/photos/src/services/face/f-index.ts | 102 +++++++++++++++---- web/apps/photos/src/services/face/image.ts | 65 ------------ 2 files changed, 84 insertions(+), 83 deletions(-) diff --git a/web/apps/photos/src/services/face/f-index.ts b/web/apps/photos/src/services/face/f-index.ts index 0e0950261..60e0bafb7 100644 --- a/web/apps/photos/src/services/face/f-index.ts +++ b/web/apps/photos/src/services/face/f-index.ts @@ -481,6 +481,28 @@ function normalizeLandmarks( ) as Array<[number, number]>; } +async function extractFaceImagesToFloat32( + faceAlignments: Array, + faceSize: number, + image: ImageBitmap, +): Promise { + const faceData = new Float32Array( + faceAlignments.length * faceSize * faceSize * 3, + ); + for (let i = 0; i < faceAlignments.length; i++) { + const alignedFace = faceAlignments[i]; + const faceDataOffset = i * faceSize * faceSize * 3; + warpAffineFloat32List( + image, + alignedFace, + faceSize, + faceData, + faceDataOffset, + ); + } + return faceData; +} + const makeFaceID = (detectedFace: DetectedFace, imageDims: Dimensions) => { const part = (v: number) => clamp(v, 0.0, 0.999999).toFixed(5).substring(2); const xMin = part(detectedFace.detection.box.x / imageDims.width); @@ -777,24 +799,68 @@ const getFaceCrop = ( }; }; -async function extractFaceImagesToFloat32( - faceAlignments: Array, - faceSize: number, - image: ImageBitmap, -): Promise { - const faceData = new Float32Array( - faceAlignments.length * faceSize * faceSize * 3, - ); - for (let i = 0; i < faceAlignments.length; i++) { - const alignedFace = faceAlignments[i]; - const faceDataOffset = i * faceSize * faceSize * 3; - warpAffineFloat32List( - image, - alignedFace, - faceSize, - faceData, - faceDataOffset, +export function cropWithRotation( + imageBitmap: ImageBitmap, + cropBox: Box, + rotation?: number, + maxSize?: Dimensions, + minSize?: Dimensions, +) { + const box = cropBox.round(); + + const outputSize = { width: box.width, height: box.height }; + if (maxSize) { + const minScale = Math.min( + maxSize.width / box.width, + maxSize.height / box.height, ); + if (minScale < 1) { + outputSize.width = Math.round(minScale * box.width); + outputSize.height = Math.round(minScale * box.height); + } } - return faceData; + + if (minSize) { + const maxScale = Math.max( + minSize.width / box.width, + minSize.height / box.height, + ); + if (maxScale > 1) { + outputSize.width = Math.round(maxScale * box.width); + outputSize.height = Math.round(maxScale * box.height); + } + } + + // log.info({ imageBitmap, box, outputSize }); + + const offscreen = new OffscreenCanvas(outputSize.width, outputSize.height); + const offscreenCtx = offscreen.getContext("2d"); + offscreenCtx.imageSmoothingQuality = "high"; + + offscreenCtx.translate(outputSize.width / 2, outputSize.height / 2); + rotation && offscreenCtx.rotate(rotation); + + const outputBox = new Box({ + x: -outputSize.width / 2, + y: -outputSize.height / 2, + width: outputSize.width, + height: outputSize.height, + }); + + const enlargedBox = enlargeBox(box, 1.5); + const enlargedOutputBox = enlargeBox(outputBox, 1.5); + + offscreenCtx.drawImage( + imageBitmap, + enlargedBox.x, + enlargedBox.y, + enlargedBox.width, + enlargedBox.height, + enlargedOutputBox.x, + enlargedOutputBox.y, + enlargedOutputBox.width, + enlargedOutputBox.height, + ); + + return offscreen.transferToImageBitmap(); } diff --git a/web/apps/photos/src/services/face/image.ts b/web/apps/photos/src/services/face/image.ts index 538e0b413..7ed8a2cb5 100644 --- a/web/apps/photos/src/services/face/image.ts +++ b/web/apps/photos/src/services/face/image.ts @@ -362,68 +362,3 @@ export function createGrayscaleIntMatrixFromNormalized2List( ); } -export function cropWithRotation( - imageBitmap: ImageBitmap, - cropBox: Box, - rotation?: number, - maxSize?: Dimensions, - minSize?: Dimensions, -) { - const box = cropBox.round(); - - const outputSize = { width: box.width, height: box.height }; - if (maxSize) { - const minScale = Math.min( - maxSize.width / box.width, - maxSize.height / box.height, - ); - if (minScale < 1) { - outputSize.width = Math.round(minScale * box.width); - outputSize.height = Math.round(minScale * box.height); - } - } - - if (minSize) { - const maxScale = Math.max( - minSize.width / box.width, - minSize.height / box.height, - ); - if (maxScale > 1) { - outputSize.width = Math.round(maxScale * box.width); - outputSize.height = Math.round(maxScale * box.height); - } - } - - // log.info({ imageBitmap, box, outputSize }); - - const offscreen = new OffscreenCanvas(outputSize.width, outputSize.height); - const offscreenCtx = offscreen.getContext("2d"); - offscreenCtx.imageSmoothingQuality = "high"; - - offscreenCtx.translate(outputSize.width / 2, outputSize.height / 2); - rotation && offscreenCtx.rotate(rotation); - - const outputBox = new Box({ - x: -outputSize.width / 2, - y: -outputSize.height / 2, - width: outputSize.width, - height: outputSize.height, - }); - - const enlargedBox = enlargeBox(box, 1.5); - const enlargedOutputBox = enlargeBox(outputBox, 1.5); - - offscreenCtx.drawImage( - imageBitmap, - enlargedBox.x, - enlargedBox.y, - enlargedBox.width, - enlargedBox.height, - enlargedOutputBox.x, - enlargedOutputBox.y, - enlargedOutputBox.width, - enlargedOutputBox.height, - ); - - return offscreen.transferToImageBitmap(); -}