add logs and refactor code

This commit is contained in:
Abhinav 2022-08-06 15:19:16 +05:30
parent 4fb7655081
commit b506d8586e
2 changed files with 68 additions and 56 deletions

View file

@ -34,6 +34,7 @@ import PeopleService from './peopleService';
import ObjectService from './objectService';
// import TextService from './textService';
import ReaderService from './readerService';
import { logError } from 'utils/sentry';
class MachineLearningService {
private initialized = false;
// private faceDetectionService: FaceDetectionService;
@ -361,6 +362,7 @@ class MachineLearningService {
syncContext.nSyncedFiles += 1;
return mlFileData;
} catch (e) {
logError(e, 'ML syncFile failed');
let error = e;
console.error('Error in ml sync, fileId: ', enteFile.id, error);
if ('status' in error) {
@ -409,30 +411,10 @@ class MachineLearningService {
newMlFile.mlVersion = fileContext.oldMlFile.mlVersion;
}
await ReaderService.getImageBitmap(syncContext, fileContext);
const faceDetection = async () => {
console.time(`face detection time taken ${enteFile.id}`);
await FaceService.syncFileFaceDetections(syncContext, fileContext);
if (newMlFile.faces && newMlFile.faces.length > 0) {
await FaceService.syncFileFaceCrops(syncContext, fileContext);
await FaceService.syncFileFaceAlignments(
syncContext,
fileContext
);
await FaceService.syncFileFaceEmbeddings(
syncContext,
fileContext
);
}
console.timeEnd(`face detection time taken ${enteFile.id}`);
};
try {
await ReaderService.getImageBitmap(syncContext, fileContext);
await Promise.all([
faceDetection(),
this.syncFaceDetections(syncContext, fileContext),
ObjectService.syncFileObjectDetections(
syncContext,
fileContext
@ -447,6 +429,7 @@ class MachineLearningService {
newMlFile.lastErrorMessage = undefined;
await this.persistMLFileData(syncContext, newMlFile);
} catch (e) {
logError(e, 'ml detection failed');
newMlFile.mlVersion = oldMlFile.mlVersion;
throw e;
} finally {
@ -561,6 +544,25 @@ class MachineLearningService {
syncContext.tsne = toTSNE(input, syncContext.config.tsne);
console.log('tsne: ', syncContext.tsne);
}
private async syncFaceDetections(
syncContext: MLSyncContext,
fileContext: MLSyncFileContext
) {
console.time(`object detection time taken ${fileContext.enteFile.id}`);
const { newMlFile } = fileContext;
console.time(`face detection time taken ${fileContext.enteFile.id}`);
await FaceService.syncFileFaceDetections(syncContext, fileContext);
if (newMlFile.faces && newMlFile.faces.length > 0) {
await FaceService.syncFileFaceCrops(syncContext, fileContext);
await FaceService.syncFileFaceAlignments(syncContext, fileContext);
await FaceService.syncFileFaceEmbeddings(syncContext, fileContext);
}
console.timeEnd(`face detection time taken ${fileContext.enteFile.id}`);
}
}
export default new MachineLearningService();

View file

@ -5,48 +5,58 @@ import {
getOriginalImageBitmap,
getThumbnailImageBitmap,
} from 'utils/machineLearning';
import { logError } from 'utils/sentry';
class ReaderService {
async getImageBitmap(
syncContext: MLSyncContext,
fileContext: MLSyncFileContext
) {
if (fileContext.imageBitmap) {
return fileContext.imageBitmap;
}
// console.log('1 TF Memory stats: ', tf.memory());
if (fileContext.localFile) {
if (fileContext.enteFile.metadata.fileType !== FILE_TYPE.IMAGE) {
throw new Error('Local file of only image type is supported');
try {
if (fileContext.imageBitmap) {
return fileContext.imageBitmap;
}
fileContext.imageBitmap = await getLocalFileImageBitmap(
fileContext.enteFile,
fileContext.localFile
);
} else if (
syncContext.config.imageSource === 'Original' &&
[FILE_TYPE.IMAGE, FILE_TYPE.LIVE_PHOTO].includes(
fileContext.enteFile.metadata.fileType
)
) {
fileContext.imageBitmap = await getOriginalImageBitmap(
fileContext.enteFile,
syncContext.token,
await syncContext.getEnteWorker(fileContext.enteFile.id)
);
} else {
fileContext.imageBitmap = await getThumbnailImageBitmap(
fileContext.enteFile,
syncContext.token
);
// console.log('1 TF Memory stats: ', tf.memory());
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 (
syncContext.config.imageSource === 'Original' &&
[FILE_TYPE.IMAGE, FILE_TYPE.LIVE_PHOTO].includes(
fileContext.enteFile.metadata.fileType
)
) {
fileContext.imageBitmap = await getOriginalImageBitmap(
fileContext.enteFile,
syncContext.token,
await syncContext.getEnteWorker(fileContext.enteFile.id)
);
} else {
fileContext.imageBitmap = await getThumbnailImageBitmap(
fileContext.enteFile,
syncContext.token
);
}
fileContext.newMlFile.imageSource = syncContext.config.imageSource;
const { width, height } = fileContext.imageBitmap;
fileContext.newMlFile.imageDimensions = { width, height };
// console.log('2 TF Memory stats: ', tf.memory());
return fileContext.imageBitmap;
} catch (e) {
logError(e, 'failed to create image bitmap');
throw e;
}
fileContext.newMlFile.imageSource = syncContext.config.imageSource;
const { width, height } = fileContext.imageBitmap;
fileContext.newMlFile.imageDimensions = { width, height };
// console.log('2 TF Memory stats: ', tf.memory());
return fileContext.imageBitmap;
}
}
export default new ReaderService();