Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cvat-core api fixes & refactoring #8016

Merged
merged 16 commits into from
Jun 20, 2024
29 changes: 13 additions & 16 deletions cvat-core/src/annotations-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ import {
import AnnotationHistory from './annotations-history';
import { Job } from './session';

interface ImportedCollection {
tags: Tag[],
shapes: Shape[],
tracks: Track[],
}

const validateAttributesList = (
attributes: { spec_id: number, value: string }[],
): { spec_id: number, value: string }[] => {
Expand Down Expand Up @@ -116,7 +110,7 @@ export default class Collection {
};
}

public import(data: Omit<SerializedCollection, 'version'>): ImportedCollection {
public import(data: Omit<SerializedCollection, 'version'>): void {
bsekachev marked this conversation as resolved.
Show resolved Hide resolved
const result = {
tags: [],
shapes: [],
Expand Down Expand Up @@ -155,8 +149,6 @@ export default class Collection {
this.objects[clientID] = trackModel;
}
}

return result;
}

public export(): Omit<SerializedCollection, 'version'> {
Expand Down Expand Up @@ -774,19 +766,24 @@ export default class Collection {
);
}

public clear(startframe: number, endframe: number, delTrackKeyframesOnly: boolean): void {
if (startframe !== undefined && endframe !== undefined) {
public clear(flags?: {
bsekachev marked this conversation as resolved.
Show resolved Hide resolved
startFrame?: number;
stopFrame?: number;
delTrackKeyframesOnly?: boolean;
}): void {
const { startFrame, stopFrame, delTrackKeyframesOnly } = flags ?? {};
if (startFrame !== undefined && stopFrame !== undefined) {
// If only a range of annotations need to be cleared
for (let frame = startframe; frame <= endframe; frame++) {
for (let frame = startFrame; frame <= stopFrame; frame++) {
this.shapes[frame] = [];
this.tags[frame] = [];
}
const { tracks } = this;
tracks.forEach((track) => {
if (track.frame <= endframe) {
if (track.frame <= stopFrame) {
if (delTrackKeyframesOnly) {
for (const keyframe of Object.keys(track.shapes)) {
if (+keyframe >= startframe && +keyframe <= endframe) {
if (+keyframe >= startFrame && +keyframe <= stopFrame) {
delete track.shapes[keyframe];
((track as unknown as SkeletonTrack).elements || []).forEach((element) => {
if (keyframe in element.shapes) {
Expand All @@ -797,13 +794,13 @@ export default class Collection {
track.updated = Date.now();
}
}
} else if (track.frame >= startframe) {
} else if (track.frame >= startFrame) {
const index = tracks.indexOf(track);
if (index > -1) { tracks.splice(index, 1); }
}
}
});
} else if (startframe === undefined && endframe === undefined) {
} else if (startFrame === undefined && stopFrame === undefined) {
// If all annotations need to be cleared
this.shapes = {};
this.tags = {};
Expand Down
14 changes: 9 additions & 5 deletions cvat-core/src/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function getSession(session): WeakMapItem {
}

throw new InstanceNotInitializedError(
'Session has not been initialized yet. Call annotations.get() or annotations.clear(true) before',
'Session has not been initialized yet. Call annotations.get() or annotations.clear({ reload: true }) before',
);
}

Expand Down Expand Up @@ -113,17 +113,21 @@ export async function getAnnotations(session, frame, allTracks, filters): Promis
}
}

export async function clearAnnotations(session, reload, startframe, endframe, delTrackKeyframesOnly): Promise<void> {
checkObjectType('reload', reload, 'boolean', null);
export async function clearAnnotations(
session: Task | Job,
flags: Parameters<typeof Job.prototype.annotations.clear>[0],
): Promise<void> {
const sessionType = session instanceof Task ? 'task' : 'job';
const cache = getCache(sessionType);

if (reload) {
if ('reload' in flags ?? {}) {
const { reload } = flags;
checkObjectType('reload', reload, 'boolean', null);
cache.delete(session);
return getAnnotationsFromServer(session);
}

return getCollection(session).clear(startframe, endframe, delTrackKeyframesOnly);
return getCollection(session).clear(flags);
}

export async function exportDataset(
Expand Down
Loading
Loading