From 480d14729d2fa0c4f398aabdacc8cc1fd8f74815 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Fri, 10 Feb 2023 17:44:03 +0530 Subject: [PATCH] rename thingClass to thing and thingClasses to things and Object to RealWorldObject --- src/components/MachineLearning/ObjectList.tsx | 4 +- .../Search/SearchBar/searchInput/index.tsx | 4 +- src/components/SearchBar-old.tsx | 4 +- .../machineLearning/machineLearningService.ts | 2 +- src/services/machineLearning/objectService.ts | 57 ++++++++----------- src/services/searchService.ts | 12 ++-- src/types/machineLearning/index.ts | 10 ++-- src/types/search/index.ts | 13 ++++- src/utils/machineLearning/index.ts | 6 +- src/utils/storage/mlIDbStorage.ts | 32 +++++------ 10 files changed, 73 insertions(+), 71 deletions(-) diff --git a/src/components/MachineLearning/ObjectList.tsx b/src/components/MachineLearning/ObjectList.tsx index ded8e87fb..e5a3a5340 100644 --- a/src/components/MachineLearning/ObjectList.tsx +++ b/src/components/MachineLearning/ObjectList.tsx @@ -14,10 +14,10 @@ export function ObjectLabelList(props: { useEffect(() => { let didCancel = false; const main = async () => { - const things = await mlIDbStorage.getAllThingsMap(); + const objects = await mlIDbStorage.getAllObjectsMap(); const uniqueObjectNames = [ ...new Set( - (things.get(props.file.id) ?? []).map( + (objects.get(props.file.id) ?? []).map( (object) => object.detection.class ) ), diff --git a/src/components/Search/SearchBar/searchInput/index.tsx b/src/components/Search/SearchBar/searchInput/index.tsx index 198b87f10..6a7a6b136 100644 --- a/src/components/Search/SearchBar/searchInput/index.tsx +++ b/src/components/Search/SearchBar/searchInput/index.tsx @@ -24,7 +24,7 @@ import { Collection } from 'types/collection'; import { OptionWithInfo } from './optionWithInfo'; import { SearchInputWrapper } from '../styledComponents'; import MenuWithPeople from './MenuWithPeople'; -import { Person, ThingClass, WordGroup } from 'types/machineLearning'; +import { Person, Thing, WordGroup } from 'types/machineLearning'; interface Iprops { isOpen: boolean; @@ -103,7 +103,7 @@ export default function SearchInput(props: Iprops) { search = { person: selectedOption.value as Person }; break; case SuggestionType.THING: - search = { thing: selectedOption.value as ThingClass }; + search = { thing: selectedOption.value as Thing }; break; case SuggestionType.TEXT: search = { text: selectedOption.value as WordGroup }; diff --git a/src/components/SearchBar-old.tsx b/src/components/SearchBar-old.tsx index 440d140b8..6bc6ce399 100644 --- a/src/components/SearchBar-old.tsx +++ b/src/components/SearchBar-old.tsx @@ -35,7 +35,7 @@ export {}; // import { GalleryContext } from 'pages/gallery'; // import { AppContext } from 'pages/_app'; // import { Col } from 'react-bootstrap'; -// import { Person, ThingClass, WordGroup } from 'types/machineLearning'; +// import { Person, Thing, WordGroup } from 'types/machineLearning'; // import { IndexStatus } from 'types/machineLearning/ui'; // import { PeopleList } from './MachineLearning/PeopleList'; // import ObjectIcon from './icons/ObjectIcon'; @@ -276,7 +276,7 @@ export {}; // props.setOpen(true); // break; // case SuggestionType.THING: -// props.setSearch({ thing: selectedOption.value as ThingClass }); +// props.setSearch({ thing: selectedOption.value as Thing }); // props.setOpen(true); // break; // case SuggestionType.TEXT: diff --git a/src/services/machineLearning/machineLearningService.ts b/src/services/machineLearning/machineLearningService.ts index b38fd0546..0c3aee7d8 100644 --- a/src/services/machineLearning/machineLearningService.ts +++ b/src/services/machineLearning/machineLearningService.ts @@ -548,7 +548,7 @@ class MachineLearningService { // await this.init(); await PeopleService.syncPeopleIndex(syncContext); - await ObjectService.syncThingClassesIndex(syncContext); + await ObjectService.syncThingsIndex(syncContext); await this.persistMLLibraryData(syncContext); } diff --git a/src/services/machineLearning/objectService.ts b/src/services/machineLearning/objectService.ts index 95a9ff5ce..b174b21b1 100644 --- a/src/services/machineLearning/objectService.ts +++ b/src/services/machineLearning/objectService.ts @@ -2,13 +2,13 @@ import { MLSyncContext, MLSyncFileContext, DetectedObject, - ThingClass, + Thing, } from 'types/machineLearning'; import { addLogLine } from 'utils/logging'; import { isDifferentOrOld, getObjectId, - getAllThingsFromMap, + getAllObjectsFromMap, } from 'utils/machineLearning'; import mlIDbStorage from 'utils/storage/mlIDbStorage'; import ReaderService from './readerService'; @@ -31,7 +31,7 @@ class ObjectService { ) && oldMlFile?.imageSource === syncContext.config.imageSource ) { - newMlFile.things = oldMlFile?.things; + newMlFile.objects = oldMlFile?.objects; newMlFile.imageSource = oldMlFile.imageSource; newMlFile.imageDimensions = oldMlFile.imageDimensions; newMlFile.objectDetectionMethod = oldMlFile.objectDetectionMethod; @@ -69,7 +69,7 @@ class ObjectService { detection, } as DetectedObject; }); - newMlFile.things = detectedObjects?.map((detectedObject) => ({ + newMlFile.objects = detectedObjects?.map((detectedObject) => ({ ...detectedObject, id: getObjectId(detectedObject, newMlFile.imageDimensions), className: detectedObject.detection.class, @@ -83,23 +83,21 @@ class ObjectService { 'ms' ); - addLogLine('[MLService] Detected Objects: ', newMlFile.things?.length); + addLogLine('[MLService] Detected Objects: ', newMlFile.objects?.length); } - async getAllSyncedThingsMap(syncContext: MLSyncContext) { - if (syncContext.allSyncedThingsMap) { - return syncContext.allSyncedThingsMap; + async getAllSyncedObjectsMap(syncContext: MLSyncContext) { + if (syncContext.allSyncedObjectsMap) { + return syncContext.allSyncedObjectsMap; } - syncContext.allSyncedThingsMap = await mlIDbStorage.getAllThingsMap(); - return syncContext.allSyncedThingsMap; + syncContext.allSyncedObjectsMap = await mlIDbStorage.getAllObjectsMap(); + return syncContext.allSyncedObjectsMap; } - public async clusterThingClasses( - syncContext: MLSyncContext - ): Promise { - const allObjectsMap = await this.getAllSyncedThingsMap(syncContext); - const allObjects = getAllThingsFromMap(allObjectsMap); + public async clusterThings(syncContext: MLSyncContext): Promise { + const allObjectsMap = await this.getAllSyncedObjectsMap(syncContext); + const allObjects = getAllObjectsFromMap(allObjectsMap); const objectClusters = new Map(); allObjects.map((object) => { if (!objectClusters.has(object.detection.class)) { @@ -110,43 +108,38 @@ class ObjectService { }); return [...objectClusters.entries()].map(([className, files], id) => ({ id, - className, + name: className, files, })); } - async syncThingClassesIndex(syncContext: MLSyncContext) { + async syncThingsIndex(syncContext: MLSyncContext) { const filesVersion = await mlIDbStorage.getIndexVersion('files'); - addLogLine( - 'thingClasses', - await mlIDbStorage.getIndexVersion('thingClasses') - ); - if ( - filesVersion <= (await mlIDbStorage.getIndexVersion('thingClasses')) - ) { + addLogLine('things', await mlIDbStorage.getIndexVersion('things')); + if (filesVersion <= (await mlIDbStorage.getIndexVersion('things'))) { addLogLine( '[MLService] Skipping people index as already synced to latest version' ); return; } - const thingClasses = await this.clusterThingClasses(syncContext); + const things = await this.clusterThings(syncContext); - if (!thingClasses || thingClasses.length < 1) { + if (!things || things.length < 1) { return; } - await mlIDbStorage.clearAllThingClasses(); + await mlIDbStorage.clearAllThings(); - for (const thingClass of thingClasses) { - await mlIDbStorage.putThingClass(thingClass); + for (const thing of things) { + await mlIDbStorage.putThing(thing); } - await mlIDbStorage.setIndexVersion('thingClasses', filesVersion); + await mlIDbStorage.setIndexVersion('things', filesVersion); } - async getAllThingClasses() { - return await mlIDbStorage.getAllThingClasses(); + async getAllThings() { + return await mlIDbStorage.getAllThings(); } } diff --git a/src/services/searchService.ts b/src/services/searchService.ts index 4aaac6530..f71350ac3 100644 --- a/src/services/searchService.ts +++ b/src/services/searchService.ts @@ -22,7 +22,7 @@ import { import ObjectService from './machineLearning/objectService'; import textService from './machineLearning/textService'; import { getFormattedDate, isInsideBox, isSameDayAnyYear } from 'utils/search'; -import { Person, ThingClass } from 'types/machineLearning'; +import { Person, Thing } from 'types/machineLearning'; import { getUniqueFiles } from 'utils/file'; import { User } from 'types/user'; import { getData, LS_KEYS } from 'utils/storage/localStorage'; @@ -231,7 +231,7 @@ async function getThingSuggestion(searchPhrase: string) { ({ type: SuggestionType.THING, value: searchResult, - label: searchResult.className, + label: searchResult.name, } as Suggestion) ); } @@ -315,9 +315,9 @@ async function searchLocation( } async function searchThing(searchPhrase: string) { - const thingClasses = await ObjectService.getAllThingClasses(); - return thingClasses.filter((thingClass) => - thingClass.className.toLocaleLowerCase().includes(searchPhrase) + const things = await ObjectService.getAllThings(); + return things.filter((thing) => + thing.name.toLocaleLowerCase().includes(searchPhrase) ); } @@ -389,6 +389,6 @@ function convertSuggestionToSearchQuery(option: Suggestion): Search { return { person: option.value as Person }; case SuggestionType.THING: - return { thing: option.value as ThingClass }; + return { thing: option.value as Thing }; } } diff --git a/src/types/machineLearning/index.ts b/src/types/machineLearning/index.ts index a089f4ab6..26f19c5a3 100644 --- a/src/types/machineLearning/index.ts +++ b/src/types/machineLearning/index.ts @@ -194,14 +194,14 @@ export interface DetectedObject { detection: ObjectDetection; } -export interface Thing extends DetectedObject { +export interface RealWorldObject extends DetectedObject { id: string; className: string; } -export interface ThingClass { +export interface Thing { id: number; - className: string; + name: string; files: Array; } @@ -224,7 +224,7 @@ export interface DetectedText { export interface MlFileData { fileId: number; faces?: Face[]; - things?: Thing[]; + objects?: RealWorldObject[]; text?: DetectedText[]; imageSource?: ImageType; imageDimensions?: Dimensions; @@ -334,7 +334,7 @@ export interface MLSyncContext { nSyncedFiles: number; nSyncedFaces: number; allSyncedFacesMap?: Map>; - allSyncedThingsMap?: Map>; + allSyncedObjectsMap?: Map>; allSyncedTextMap?: Map>; tsne?: any; diff --git a/src/types/search/index.ts b/src/types/search/index.ts index 980adb94f..c16f80a4a 100644 --- a/src/types/search/index.ts +++ b/src/types/search/index.ts @@ -1,4 +1,4 @@ -import { Person, Thing, ThingClass, WordGroup } from 'types/machineLearning'; +import { Person, Thing, WordGroup } from 'types/machineLearning'; import { IndexStatus } from 'types/machineLearning/ui'; import { EnteFile } from 'types/file'; @@ -29,7 +29,14 @@ export interface DateValue { export interface Suggestion { type: SuggestionType; label: string; - value: Bbox | DateValue | number[] | Person | IndexStatus | Thing; + value: + | Bbox + | DateValue + | number[] + | Person + | IndexStatus + | Thing + | WordGroup; hide?: boolean; } @@ -39,7 +46,7 @@ export type Search = { collection?: number; files?: number[]; person?: Person; - thing?: ThingClass; + thing?: Thing; text?: WordGroup; }; diff --git a/src/utils/machineLearning/index.ts b/src/utils/machineLearning/index.ts index 3e286964e..e8e1b9cc1 100644 --- a/src/utils/machineLearning/index.ts +++ b/src/utils/machineLearning/index.ts @@ -11,7 +11,7 @@ import { getLocalFiles } from 'services/fileService'; import { EnteFile } from 'types/file'; import { Dimensions } from 'types/image'; import { - Thing, + RealWorldObject, AlignedFace, DetectedFace, DetectedObject, @@ -209,7 +209,9 @@ export function getAllFacesFromMap(allFacesMap: Map>) { return allFaces; } -export function getAllThingsFromMap(allObjectsMap: Map>) { +export function getAllObjectsFromMap( + allObjectsMap: Map> +) { return [...allObjectsMap.values()].flat(); } diff --git a/src/utils/storage/mlIDbStorage.ts b/src/utils/storage/mlIDbStorage.ts index d615ebfea..5e120850b 100644 --- a/src/utils/storage/mlIDbStorage.ts +++ b/src/utils/storage/mlIDbStorage.ts @@ -19,8 +19,8 @@ import { MlFileData, MLLibraryData, Person, + RealWorldObject, Thing, - ThingClass, } from 'types/machineLearning'; import { IndexStatus } from 'types/machineLearning/ui'; import { runningInBrowser } from 'utils/common'; @@ -42,9 +42,9 @@ interface MLDb extends DBSchema { key: number; value: Person; }; - thingClasses: { + things: { key: number; - value: ThingClass; + value: Thing; }; versions: { key: string; @@ -104,7 +104,7 @@ class MLIDbStorage { keyPath: 'id', }); - db.createObjectStore('thingClasses', { + db.createObjectStore('things', { keyPath: 'id', }); @@ -296,19 +296,19 @@ class MLIDbStorage { addLogLine('updateFaces', Date.now() - startTime, 'ms'); } - public async getAllThingsMap() { + public async getAllObjectsMap() { const startTime = Date.now(); const db = await this.db; const allFiles = await db.getAll('files'); - const allThingsMap = new Map>(); + const allObjectsMap = new Map>(); allFiles.forEach( (mlFileData) => - mlFileData.things && - allThingsMap.set(mlFileData.fileId, mlFileData.things) + mlFileData.objects && + allObjectsMap.set(mlFileData.fileId, mlFileData.objects) ); - addLogLine('getAllThingsMap', Date.now() - startTime, 'ms'); + addLogLine('allObjectsMap', Date.now() - startTime, 'ms'); - return allThingsMap; + return allObjectsMap; } public async getAllTextMap() { @@ -346,18 +346,18 @@ class MLIDbStorage { return db.clear('people'); } - public async getAllThingClasses() { + public async getAllThings() { const db = await this.db; - return db.getAll('thingClasses'); + return db.getAll('things'); } - public async putThingClass(thingClass: ThingClass) { + public async putThing(thing: Thing) { const db = await this.db; - return db.put('thingClasses', thingClass); + return db.put('things', thing); } - public async clearAllThingClasses() { + public async clearAllThings() { const db = await this.db; - return db.clear('thingClasses'); + return db.clear('things'); } public async getIndexVersion(index: string) {