Manav Rathi 1 рік тому
батько
коміт
7bf8912dbc
1 змінених файлів з 30 додано та 0 видалено
  1. 30 0
      desktop/src/main/services/ml-face.ts

+ 30 - 0
desktop/src/main/services/ml-face.ts

@@ -47,6 +47,36 @@ const faceDetectionSession = async () => {
     return _faceDetectionSession;
 };
 
+let activeFaceEmbeddingModelDownload: Promise<string> | undefined;
+
+const faceEmbeddingModelPathDownloadingIfNeeded = async () => {
+    try {
+        if (activeFaceEmbeddingModelDownload) {
+            log.info("Waiting for face embedding model download to finish");
+            await activeFaceEmbeddingModelDownload;
+        } else {
+            activeFaceEmbeddingModelDownload = modelPathDownloadingIfNeeded(
+                faceEmbeddingModelName,
+                faceEmbeddingModelByteSize,
+            );
+            return await activeFaceEmbeddingModelDownload;
+        }
+    } finally {
+        activeFaceEmbeddingModelDownload = undefined;
+    }
+};
+
+let _faceEmbeddingSession: Promise<ort.InferenceSession> | undefined;
+
+const faceEmbeddingSession = async () => {
+    if (!_faceEmbeddingSession) {
+        _faceEmbeddingSession =
+            faceEmbeddingModelPathDownloadingIfNeeded().then((modelPath) =>
+                createInferenceSession(modelPath),
+            );
+    }
+    return _faceEmbeddingSession;
+};
 
 // export const clipImageEmbedding = async (jpegImageData: Uint8Array) => {
 //     const tempFilePath = await generateTempFilePath("");