Manav Rathi 1 рік тому
батько
коміт
24c33fceb7

+ 5 - 4
web/apps/photos/src/services/machineLearning/faceService.ts

@@ -10,8 +10,9 @@ import {
     type Versioned,
 } from "services/ml/types";
 import { imageBitmapToBlob, warpAffineFloat32List } from "utils/image";
-import ReaderService, {
+import {
     fetchImageBitmap,
+    fetchImageBitmapForContext,
     getFaceId,
     getLocalFile,
 } from "./readerService";
@@ -43,7 +44,7 @@ class FaceService {
 
         newMlFile.faceDetectionMethod = syncContext.faceDetectionService.method;
         fileContext.newDetection = true;
-        const imageBitmap = await ReaderService.getImageBitmap(fileContext);
+        const imageBitmap = await fetchImageBitmapForContext(fileContext);
         const timerId = `faceDetection-${fileContext.enteFile.id}`;
         console.time(timerId);
         const faceDetections =
@@ -89,7 +90,7 @@ class FaceService {
             return;
         }
 
-        const imageBitmap = await ReaderService.getImageBitmap(fileContext);
+        const imageBitmap = await fetchImageBitmapForContext(fileContext);
         newMlFile.faceCropMethod = syncContext.faceCropService.method;
 
         for (const face of newMlFile.faces) {
@@ -121,7 +122,7 @@ class FaceService {
         fileContext.newAlignment = true;
         const imageBitmap =
             fileContext.imageBitmap ||
-            (await ReaderService.getImageBitmap(fileContext));
+            (await fetchImageBitmapForContext(fileContext));
 
         // Execute the face alignment calculations
         for (const face of newMlFile.faces) {

+ 3 - 2
web/apps/photos/src/services/machineLearning/machineLearningService.ts

@@ -45,7 +45,8 @@ import hdbscanClusteringService from "./hdbscanClusteringService";
 import laplacianBlurDetectionService from "./laplacianBlurDetectionService";
 import mobileFaceNetEmbeddingService from "./mobileFaceNetEmbeddingService";
 import PeopleService from "./peopleService";
-import ReaderService from "./readerService";
+
+import { fetchImageBitmapForContext } from "./readerService";
 import yoloFaceDetectionService from "./yoloFaceDetectionService";
 
 /**
@@ -581,7 +582,7 @@ class MachineLearningService {
         }
 
         try {
-            await ReaderService.getImageBitmap(fileContext);
+            await fetchImageBitmapForContext(fileContext);
             await Promise.all([
                 this.syncFileAnalyzeFaces(syncContext, fileContext),
             ]);

+ 32 - 44
web/apps/photos/src/services/machineLearning/readerService.ts

@@ -9,52 +9,40 @@ import { EnteFile } from "types/file";
 import { getRenderableImage } from "utils/file";
 import { clamp } from "utils/image";
 
-class ReaderService {
-    async getImageBitmap(fileContext: MLSyncFileContext) {
-        try {
-            if (fileContext.imageBitmap) {
-                return fileContext.imageBitmap;
-            }
-            if (fileContext.localFile) {
-                if (
-                    fileContext.enteFile.metadata.fileType !== FILE_TYPE.IMAGE
-                ) {
-                    throw new Error(
-                        "Local file of only image type is supported",
-                    );
-                }
-                fileContext.imageBitmap = await getLocalFileImageBitmap(
-                    fileContext.enteFile,
-                    fileContext.localFile,
-                );
-            } else if (
-                [FILE_TYPE.IMAGE, FILE_TYPE.LIVE_PHOTO].includes(
-                    fileContext.enteFile.metadata.fileType,
-                )
-            ) {
-                fileContext.imageBitmap = await fetchImageBitmap(
-                    fileContext.enteFile,
-                );
-            } else {
-                // TODO-ML(MR): We don't do it on videos, when will we ever come
-                // here?
-                fileContext.imageBitmap = await getThumbnailImageBitmap(
-                    fileContext.enteFile,
-                );
-            }
-
-            fileContext.newMlFile.imageSource = "Original";
-            const { width, height } = fileContext.imageBitmap;
-            fileContext.newMlFile.imageDimensions = { width, height };
-
-            return fileContext.imageBitmap;
-        } catch (e) {
-            log.error("failed to create image bitmap", e);
-            throw e;
+export const fetchImageBitmapForContext = async (
+    fileContext: MLSyncFileContext,
+) => {
+    if (fileContext.imageBitmap) {
+        return fileContext.imageBitmap;
+    }
+    if (fileContext.localFile) {
+        if (fileContext.enteFile.metadata.fileType !== FILE_TYPE.IMAGE) {
+            throw new Error("Local file of only image type is supported");
         }
+        fileContext.imageBitmap = await getLocalFileImageBitmap(
+            fileContext.enteFile,
+            fileContext.localFile,
+        );
+    } else if (
+        [FILE_TYPE.IMAGE, FILE_TYPE.LIVE_PHOTO].includes(
+            fileContext.enteFile.metadata.fileType,
+        )
+    ) {
+        fileContext.imageBitmap = await fetchImageBitmap(fileContext.enteFile);
+    } else {
+        // TODO-ML(MR): We don't do it on videos, when will we ever come
+        // here?
+        fileContext.imageBitmap = await getThumbnailImageBitmap(
+            fileContext.enteFile,
+        );
     }
-}
-export default new ReaderService();
+
+    fileContext.newMlFile.imageSource = "Original";
+    const { width, height } = fileContext.imageBitmap;
+    fileContext.newMlFile.imageDimensions = { width, height };
+
+    return fileContext.imageBitmap;
+};
 
 export async function getLocalFile(fileId: number) {
     const localFiles = await getLocalFiles();