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(() => {
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
)
),

View file

@ -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 };

View file

@ -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:

View file

@ -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);
}

View file

@ -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<ThingClass[]> {
const allObjectsMap = await this.getAllSyncedThingsMap(syncContext);
const allObjects = getAllThingsFromMap(allObjectsMap);
public async clusterThings(syncContext: MLSyncContext): Promise<Thing[]> {
const allObjectsMap = await this.getAllSyncedObjectsMap(syncContext);
const allObjects = getAllObjectsFromMap(allObjectsMap);
const objectClusters = new Map<string, number[]>();
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();
}
}

View file

@ -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 };
}
}

View file

@ -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<number>;
}
@ -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<number, Array<Face>>;
allSyncedThingsMap?: Map<number, Array<Thing>>;
allSyncedObjectsMap?: Map<number, Array<RealWorldObject>>;
allSyncedTextMap?: Map<number, Array<DetectedText>>;
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 { 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;
};

View file

@ -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<number, Array<Face>>) {
return allFaces;
}
export function getAllThingsFromMap(allObjectsMap: Map<number, Array<Thing>>) {
export function getAllObjectsFromMap(
allObjectsMap: Map<number, Array<RealWorldObject>>
) {
return [...allObjectsMap.values()].flat();
}

View file

@ -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<number, Array<Thing>>();
const allObjectsMap = new Map<number, Array<RealWorldObject>>();
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) {