diff --git a/src/services/machineLearning/machineLearningService.ts b/src/services/machineLearning/machineLearningService.ts index 156bca13b..1d74ac54a 100644 --- a/src/services/machineLearning/machineLearningService.ts +++ b/src/services/machineLearning/machineLearningService.ts @@ -26,6 +26,7 @@ import { toTSNE } from 'utils/machineLearning/visualization'; import { mlFilesStore, mlPeopleStore } from 'utils/storage/localForage'; import ArcfaceAlignmentService from './arcfaceAlignmentService'; import { + findFirstIfSorted, getAllFacesFromMap, getFaceImage, getThumbnailTFImage, @@ -315,8 +316,14 @@ class MachineLearningService { .map((f) => allFaces[f]) .filter((f) => f); + // TODO: face box to be normalized to 0..1 scale + const personFace = findFirstIfSorted( + faces, + (a, b) => + a.probability * a.box.width - b.probability * b.box.width + ); const faceImageTensor = await getFaceImage( - faces[0], + personFace, syncContext.token ); const faceImage = await faceImageTensor.array(); diff --git a/src/utils/machineLearning/index.ts b/src/utils/machineLearning/index.ts index 51831eadb..f8ef9b344 100644 --- a/src/utils/machineLearning/index.ts +++ b/src/utils/machineLearning/index.ts @@ -180,7 +180,7 @@ export async function getPeopleList(file: File): Promise> { const mlFileData: MlFileData = await mlFilesStore.getItem( file.id.toString() ); - if (!mlFileData.faces || mlFileData.faces.length < 1) { + if (!mlFileData || !mlFileData.faces || mlFileData.faces.length < 1) { return []; } @@ -200,6 +200,25 @@ export async function getPeopleList(file: File): Promise> { return peopleList; } +export function findFirstIfSorted( + elements: Array, + comparator: (a: T, b: T) => number +) { + if (!elements || elements.length < 1) { + return; + } + let first = elements[0]; + + for (let i = 1; i < elements.length; i++) { + const comp = comparator(elements[i], first); + if (comp > 0) { + first = elements[i]; + } + } + + return first; +} + export const DEFAULT_ML_SYNC_CONFIG: MLSyncConfig = { syncIntervalSec: 5, // 300 batchSize: 5, // 200