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 ObjectService from './objectService';
// import TextService from './textService'; // import TextService from './textService';
import ReaderService from './readerService'; import ReaderService from './readerService';
import { logError } from 'utils/sentry';
class MachineLearningService { class MachineLearningService {
private initialized = false; private initialized = false;
// private faceDetectionService: FaceDetectionService; // private faceDetectionService: FaceDetectionService;
@ -361,6 +362,7 @@ class MachineLearningService {
syncContext.nSyncedFiles += 1; syncContext.nSyncedFiles += 1;
return mlFileData; return mlFileData;
} catch (e) { } catch (e) {
logError(e, 'ML syncFile failed');
let error = e; let error = e;
console.error('Error in ml sync, fileId: ', enteFile.id, error); console.error('Error in ml sync, fileId: ', enteFile.id, error);
if ('status' in error) { if ('status' in error) {
@ -409,30 +411,10 @@ class MachineLearningService {
newMlFile.mlVersion = fileContext.oldMlFile.mlVersion; 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 { try {
await ReaderService.getImageBitmap(syncContext, fileContext);
await Promise.all([ await Promise.all([
faceDetection(), this.syncFaceDetections(syncContext, fileContext),
ObjectService.syncFileObjectDetections( ObjectService.syncFileObjectDetections(
syncContext, syncContext,
fileContext fileContext
@ -447,6 +429,7 @@ class MachineLearningService {
newMlFile.lastErrorMessage = undefined; newMlFile.lastErrorMessage = undefined;
await this.persistMLFileData(syncContext, newMlFile); await this.persistMLFileData(syncContext, newMlFile);
} catch (e) { } catch (e) {
logError(e, 'ml detection failed');
newMlFile.mlVersion = oldMlFile.mlVersion; newMlFile.mlVersion = oldMlFile.mlVersion;
throw e; throw e;
} finally { } finally {
@ -561,6 +544,25 @@ class MachineLearningService {
syncContext.tsne = toTSNE(input, syncContext.config.tsne); syncContext.tsne = toTSNE(input, syncContext.config.tsne);
console.log('tsne: ', syncContext.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(); export default new MachineLearningService();

View file

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