rename thingClass to thing and thingClasses to things and Object to RealWorldObject

This commit is contained in:
Abhinav 2023-02-10 17:44:03 +05:30
parent 701a66d6ce
commit 480d14729d
10 changed files with 73 additions and 71 deletions

View file

@ -14,10 +14,10 @@ export function ObjectLabelList(props: {
useEffect(() => { useEffect(() => {
let didCancel = false; let didCancel = false;
const main = async () => { const main = async () => {
const things = await mlIDbStorage.getAllThingsMap(); const objects = await mlIDbStorage.getAllObjectsMap();
const uniqueObjectNames = [ const uniqueObjectNames = [
...new Set( ...new Set(
(things.get(props.file.id) ?? []).map( (objects.get(props.file.id) ?? []).map(
(object) => object.detection.class (object) => object.detection.class
) )
), ),

View file

@ -24,7 +24,7 @@ import { Collection } from 'types/collection';
import { OptionWithInfo } from './optionWithInfo'; import { OptionWithInfo } from './optionWithInfo';
import { SearchInputWrapper } from '../styledComponents'; import { SearchInputWrapper } from '../styledComponents';
import MenuWithPeople from './MenuWithPeople'; import MenuWithPeople from './MenuWithPeople';
import { Person, ThingClass, WordGroup } from 'types/machineLearning'; import { Person, Thing, WordGroup } from 'types/machineLearning';
interface Iprops { interface Iprops {
isOpen: boolean; isOpen: boolean;
@ -103,7 +103,7 @@ export default function SearchInput(props: Iprops) {
search = { person: selectedOption.value as Person }; search = { person: selectedOption.value as Person };
break; break;
case SuggestionType.THING: case SuggestionType.THING:
search = { thing: selectedOption.value as ThingClass }; search = { thing: selectedOption.value as Thing };
break; break;
case SuggestionType.TEXT: case SuggestionType.TEXT:
search = { text: selectedOption.value as WordGroup }; search = { text: selectedOption.value as WordGroup };

View file

@ -35,7 +35,7 @@ export {};
// import { GalleryContext } from 'pages/gallery'; // import { GalleryContext } from 'pages/gallery';
// import { AppContext } from 'pages/_app'; // import { AppContext } from 'pages/_app';
// import { Col } from 'react-bootstrap'; // 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 { IndexStatus } from 'types/machineLearning/ui';
// import { PeopleList } from './MachineLearning/PeopleList'; // import { PeopleList } from './MachineLearning/PeopleList';
// import ObjectIcon from './icons/ObjectIcon'; // import ObjectIcon from './icons/ObjectIcon';
@ -276,7 +276,7 @@ export {};
// props.setOpen(true); // props.setOpen(true);
// break; // break;
// case SuggestionType.THING: // case SuggestionType.THING:
// props.setSearch({ thing: selectedOption.value as ThingClass }); // props.setSearch({ thing: selectedOption.value as Thing });
// props.setOpen(true); // props.setOpen(true);
// break; // break;
// case SuggestionType.TEXT: // case SuggestionType.TEXT:

View file

@ -548,7 +548,7 @@ class MachineLearningService {
// await this.init(); // await this.init();
await PeopleService.syncPeopleIndex(syncContext); await PeopleService.syncPeopleIndex(syncContext);
await ObjectService.syncThingClassesIndex(syncContext); await ObjectService.syncThingsIndex(syncContext);
await this.persistMLLibraryData(syncContext); await this.persistMLLibraryData(syncContext);
} }

View file

@ -2,13 +2,13 @@ import {
MLSyncContext, MLSyncContext,
MLSyncFileContext, MLSyncFileContext,
DetectedObject, DetectedObject,
ThingClass, Thing,
} from 'types/machineLearning'; } from 'types/machineLearning';
import { addLogLine } from 'utils/logging'; import { addLogLine } from 'utils/logging';
import { import {
isDifferentOrOld, isDifferentOrOld,
getObjectId, getObjectId,
getAllThingsFromMap, getAllObjectsFromMap,
} from 'utils/machineLearning'; } from 'utils/machineLearning';
import mlIDbStorage from 'utils/storage/mlIDbStorage'; import mlIDbStorage from 'utils/storage/mlIDbStorage';
import ReaderService from './readerService'; import ReaderService from './readerService';
@ -31,7 +31,7 @@ class ObjectService {
) && ) &&
oldMlFile?.imageSource === syncContext.config.imageSource oldMlFile?.imageSource === syncContext.config.imageSource
) { ) {
newMlFile.things = oldMlFile?.things; newMlFile.objects = oldMlFile?.objects;
newMlFile.imageSource = oldMlFile.imageSource; newMlFile.imageSource = oldMlFile.imageSource;
newMlFile.imageDimensions = oldMlFile.imageDimensions; newMlFile.imageDimensions = oldMlFile.imageDimensions;
newMlFile.objectDetectionMethod = oldMlFile.objectDetectionMethod; newMlFile.objectDetectionMethod = oldMlFile.objectDetectionMethod;
@ -69,7 +69,7 @@ class ObjectService {
detection, detection,
} as DetectedObject; } as DetectedObject;
}); });
newMlFile.things = detectedObjects?.map((detectedObject) => ({ newMlFile.objects = detectedObjects?.map((detectedObject) => ({
...detectedObject, ...detectedObject,
id: getObjectId(detectedObject, newMlFile.imageDimensions), id: getObjectId(detectedObject, newMlFile.imageDimensions),
className: detectedObject.detection.class, className: detectedObject.detection.class,
@ -83,23 +83,21 @@ class ObjectService {
'ms' 'ms'
); );
addLogLine('[MLService] Detected Objects: ', newMlFile.things?.length); addLogLine('[MLService] Detected Objects: ', newMlFile.objects?.length);
} }
async getAllSyncedThingsMap(syncContext: MLSyncContext) { async getAllSyncedObjectsMap(syncContext: MLSyncContext) {
if (syncContext.allSyncedThingsMap) { if (syncContext.allSyncedObjectsMap) {
return syncContext.allSyncedThingsMap; return syncContext.allSyncedObjectsMap;
} }
syncContext.allSyncedThingsMap = await mlIDbStorage.getAllThingsMap(); syncContext.allSyncedObjectsMap = await mlIDbStorage.getAllObjectsMap();
return syncContext.allSyncedThingsMap; return syncContext.allSyncedObjectsMap;
} }
public async clusterThingClasses( public async clusterThings(syncContext: MLSyncContext): Promise<Thing[]> {
syncContext: MLSyncContext const allObjectsMap = await this.getAllSyncedObjectsMap(syncContext);
): Promise<ThingClass[]> { const allObjects = getAllObjectsFromMap(allObjectsMap);
const allObjectsMap = await this.getAllSyncedThingsMap(syncContext);
const allObjects = getAllThingsFromMap(allObjectsMap);
const objectClusters = new Map<string, number[]>(); const objectClusters = new Map<string, number[]>();
allObjects.map((object) => { allObjects.map((object) => {
if (!objectClusters.has(object.detection.class)) { if (!objectClusters.has(object.detection.class)) {
@ -110,43 +108,38 @@ class ObjectService {
}); });
return [...objectClusters.entries()].map(([className, files], id) => ({ return [...objectClusters.entries()].map(([className, files], id) => ({
id, id,
className, name: className,
files, files,
})); }));
} }
async syncThingClassesIndex(syncContext: MLSyncContext) { async syncThingsIndex(syncContext: MLSyncContext) {
const filesVersion = await mlIDbStorage.getIndexVersion('files'); const filesVersion = await mlIDbStorage.getIndexVersion('files');
addLogLine( addLogLine('things', await mlIDbStorage.getIndexVersion('things'));
'thingClasses', if (filesVersion <= (await mlIDbStorage.getIndexVersion('things'))) {
await mlIDbStorage.getIndexVersion('thingClasses')
);
if (
filesVersion <= (await mlIDbStorage.getIndexVersion('thingClasses'))
) {
addLogLine( addLogLine(
'[MLService] Skipping people index as already synced to latest version' '[MLService] Skipping people index as already synced to latest version'
); );
return; return;
} }
const thingClasses = await this.clusterThingClasses(syncContext); const things = await this.clusterThings(syncContext);
if (!thingClasses || thingClasses.length < 1) { if (!things || things.length < 1) {
return; return;
} }
await mlIDbStorage.clearAllThingClasses(); await mlIDbStorage.clearAllThings();
for (const thingClass of thingClasses) { for (const thing of things) {
await mlIDbStorage.putThingClass(thingClass); await mlIDbStorage.putThing(thing);
} }
await mlIDbStorage.setIndexVersion('thingClasses', filesVersion); await mlIDbStorage.setIndexVersion('things', filesVersion);
} }
async getAllThingClasses() { async getAllThings() {
return await mlIDbStorage.getAllThingClasses(); return await mlIDbStorage.getAllThings();
} }
} }

View file

@ -22,7 +22,7 @@ import {
import ObjectService from './machineLearning/objectService'; import ObjectService from './machineLearning/objectService';
import textService from './machineLearning/textService'; import textService from './machineLearning/textService';
import { getFormattedDate, isInsideBox, isSameDayAnyYear } from 'utils/search'; 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 { getUniqueFiles } from 'utils/file';
import { User } from 'types/user'; import { User } from 'types/user';
import { getData, LS_KEYS } from 'utils/storage/localStorage'; import { getData, LS_KEYS } from 'utils/storage/localStorage';
@ -231,7 +231,7 @@ async function getThingSuggestion(searchPhrase: string) {
({ ({
type: SuggestionType.THING, type: SuggestionType.THING,
value: searchResult, value: searchResult,
label: searchResult.className, label: searchResult.name,
} as Suggestion) } as Suggestion)
); );
} }
@ -315,9 +315,9 @@ async function searchLocation(
} }
async function searchThing(searchPhrase: string) { async function searchThing(searchPhrase: string) {
const thingClasses = await ObjectService.getAllThingClasses(); const things = await ObjectService.getAllThings();
return thingClasses.filter((thingClass) => return things.filter((thing) =>
thingClass.className.toLocaleLowerCase().includes(searchPhrase) thing.name.toLocaleLowerCase().includes(searchPhrase)
); );
} }
@ -389,6 +389,6 @@ function convertSuggestionToSearchQuery(option: Suggestion): Search {
return { person: option.value as Person }; return { person: option.value as Person };
case SuggestionType.THING: case SuggestionType.THING:
return { thing: option.value as ThingClass }; return { thing: option.value as Thing };
} }
} }

View file

@ -194,14 +194,14 @@ export interface DetectedObject {
detection: ObjectDetection; detection: ObjectDetection;
} }
export interface Thing extends DetectedObject { export interface RealWorldObject extends DetectedObject {
id: string; id: string;
className: string; className: string;
} }
export interface ThingClass { export interface Thing {
id: number; id: number;
className: string; name: string;
files: Array<number>; files: Array<number>;
} }
@ -224,7 +224,7 @@ export interface DetectedText {
export interface MlFileData { export interface MlFileData {
fileId: number; fileId: number;
faces?: Face[]; faces?: Face[];
things?: Thing[]; objects?: RealWorldObject[];
text?: DetectedText[]; text?: DetectedText[];
imageSource?: ImageType; imageSource?: ImageType;
imageDimensions?: Dimensions; imageDimensions?: Dimensions;
@ -334,7 +334,7 @@ export interface MLSyncContext {
nSyncedFiles: number; nSyncedFiles: number;
nSyncedFaces: number; nSyncedFaces: number;
allSyncedFacesMap?: Map<number, Array<Face>>; allSyncedFacesMap?: Map<number, Array<Face>>;
allSyncedThingsMap?: Map<number, Array<Thing>>; allSyncedObjectsMap?: Map<number, Array<RealWorldObject>>;
allSyncedTextMap?: Map<number, Array<DetectedText>>; allSyncedTextMap?: Map<number, Array<DetectedText>>;
tsne?: any; tsne?: any;

View file

@ -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 { IndexStatus } from 'types/machineLearning/ui';
import { EnteFile } from 'types/file'; import { EnteFile } from 'types/file';
@ -29,7 +29,14 @@ export interface DateValue {
export interface Suggestion { export interface Suggestion {
type: SuggestionType; type: SuggestionType;
label: string; label: string;
value: Bbox | DateValue | number[] | Person | IndexStatus | Thing; value:
| Bbox
| DateValue
| number[]
| Person
| IndexStatus
| Thing
| WordGroup;
hide?: boolean; hide?: boolean;
} }
@ -39,7 +46,7 @@ export type Search = {
collection?: number; collection?: number;
files?: number[]; files?: number[];
person?: Person; person?: Person;
thing?: ThingClass; thing?: Thing;
text?: WordGroup; text?: WordGroup;
}; };

View file

@ -11,7 +11,7 @@ import { getLocalFiles } from 'services/fileService';
import { EnteFile } from 'types/file'; import { EnteFile } from 'types/file';
import { Dimensions } from 'types/image'; import { Dimensions } from 'types/image';
import { import {
Thing, RealWorldObject,
AlignedFace, AlignedFace,
DetectedFace, DetectedFace,
DetectedObject, DetectedObject,
@ -209,7 +209,9 @@ export function getAllFacesFromMap(allFacesMap: Map<number, Array<Face>>) {
return allFaces; return allFaces;
} }
export function getAllThingsFromMap(allObjectsMap: Map<number, Array<Thing>>) { export function getAllObjectsFromMap(
allObjectsMap: Map<number, Array<RealWorldObject>>
) {
return [...allObjectsMap.values()].flat(); return [...allObjectsMap.values()].flat();
} }

View file

@ -19,8 +19,8 @@ import {
MlFileData, MlFileData,
MLLibraryData, MLLibraryData,
Person, Person,
RealWorldObject,
Thing, Thing,
ThingClass,
} from 'types/machineLearning'; } from 'types/machineLearning';
import { IndexStatus } from 'types/machineLearning/ui'; import { IndexStatus } from 'types/machineLearning/ui';
import { runningInBrowser } from 'utils/common'; import { runningInBrowser } from 'utils/common';
@ -42,9 +42,9 @@ interface MLDb extends DBSchema {
key: number; key: number;
value: Person; value: Person;
}; };
thingClasses: { things: {
key: number; key: number;
value: ThingClass; value: Thing;
}; };
versions: { versions: {
key: string; key: string;
@ -104,7 +104,7 @@ class MLIDbStorage {
keyPath: 'id', keyPath: 'id',
}); });
db.createObjectStore('thingClasses', { db.createObjectStore('things', {
keyPath: 'id', keyPath: 'id',
}); });
@ -296,19 +296,19 @@ class MLIDbStorage {
addLogLine('updateFaces', Date.now() - startTime, 'ms'); addLogLine('updateFaces', Date.now() - startTime, 'ms');
} }
public async getAllThingsMap() { public async getAllObjectsMap() {
const startTime = Date.now(); const startTime = Date.now();
const db = await this.db; const db = await this.db;
const allFiles = await db.getAll('files'); const allFiles = await db.getAll('files');
const allThingsMap = new Map<number, Array<Thing>>(); const allObjectsMap = new Map<number, Array<RealWorldObject>>();
allFiles.forEach( allFiles.forEach(
(mlFileData) => (mlFileData) =>
mlFileData.things && mlFileData.objects &&
allThingsMap.set(mlFileData.fileId, mlFileData.things) 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() { public async getAllTextMap() {
@ -346,18 +346,18 @@ class MLIDbStorage {
return db.clear('people'); return db.clear('people');
} }
public async getAllThingClasses() { public async getAllThings() {
const db = await this.db; 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; 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; const db = await this.db;
return db.clear('thingClasses'); return db.clear('things');
} }
public async getIndexVersion(index: string) { public async getIndexVersion(index: string) {