Skip to content

Commit

Permalink
updated api
Browse files Browse the repository at this point in the history
  • Loading branch information
bsekachev committed Aug 10, 2023
1 parent c0bc1bf commit d4e49e7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 50 deletions.
6 changes: 4 additions & 2 deletions cvat-core/src/frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,8 @@ export async function getFrame(
chunkSize,
decodedBlocksCacheSize,
dimension,
startFrame,
stopFrame,
),
decodedBlocksCacheSize,
activeChunkRequest: null,
Expand Down Expand Up @@ -582,12 +584,12 @@ export async function findFrame(
return lastUndeletedFrame;
}

export function getRanges(jobID): Array<string> {
export function getCachedChunks(jobID): number[] {
if (!(jobID in frameDataCache)) {
return [];
}

return frameDataCache[jobID].provider.cachedFrames;
return frameDataCache[jobID].provider.cachedChunks(true);
}

export function clear(jobID: number): void {
Expand Down
23 changes: 10 additions & 13 deletions cvat-core/src/session-implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
getFrame,
deleteFrame,
restoreFrame,
getRanges,
getCachedChunks,
clear as clearFrames,
findFrame,
getContextImage,
Expand Down Expand Up @@ -163,9 +163,9 @@ export function implementJob(Job) {
return result;
};

Job.prototype.frames.ranges.implementation = async function () {
const rangesData = await getRanges(this.id);
return rangesData;
Job.prototype.frames.cachedChunks.implementation = async function () {
const cachedChunks = await getCachedChunks(this.id);
return cachedChunks;
};

Job.prototype.frames.preview.implementation = async function (this: JobClass): Promise<string> {
Expand Down Expand Up @@ -570,21 +570,18 @@ export function implementTask(Task) {
isPlaying,
step,
this.dimension,
(chunkNumber, quality) => job.frames.chunk(chunkNumber, quality),
);
return result;
};

Task.prototype.frames.ranges.implementation = async function () {
const rangesData = {
decoded: [],
buffered: [],
};
Task.prototype.frames.cachedChunks.implementation = async function () {
let chunks = [];
for (const job of this.jobs) {
const { decoded, buffered } = await getRanges(job.id);
rangesData.decoded.push(decoded);
rangesData.buffered.push(buffered);
const cachedChunks = await getCachedChunks(job.id);
chunks = chunks.concat(cachedChunks);
}
return rangesData;
return Array.from(new Set(chunks));
};

Task.prototype.frames.preview.implementation = async function (this: TaskClass): Promise<string> {
Expand Down
12 changes: 6 additions & 6 deletions cvat-core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ function buildDuplicatedAPI(prototype) {
prototype.frames.save,
);
},
async ranges() {
const result = await PluginRegistry.apiWrapper.call(this, prototype.frames.ranges);
async cachedChunks() {
const result = await PluginRegistry.apiWrapper.call(this, prototype.frames.cachedChunks);
return result;
},
async preview() {
Expand Down Expand Up @@ -370,7 +370,7 @@ export class Job extends Session {
delete: CallableFunction;
restore: CallableFunction;
save: CallableFunction;
ranges: CallableFunction;
cachedChunks: CallableFunction;
preview: CallableFunction;
contextImage: CallableFunction;
search: CallableFunction;
Expand Down Expand Up @@ -574,7 +574,7 @@ export class Job extends Session {
delete: Object.getPrototypeOf(this).frames.delete.bind(this),
restore: Object.getPrototypeOf(this).frames.restore.bind(this),
save: Object.getPrototypeOf(this).frames.save.bind(this),
ranges: Object.getPrototypeOf(this).frames.ranges.bind(this),
cachedChunks: Object.getPrototypeOf(this).frames.cachedChunks.bind(this),
preview: Object.getPrototypeOf(this).frames.preview.bind(this),
search: Object.getPrototypeOf(this).frames.search.bind(this),
contextImage: Object.getPrototypeOf(this).frames.contextImage.bind(this),
Expand Down Expand Up @@ -685,7 +685,7 @@ export class Task extends Session {
delete: CallableFunction;
restore: CallableFunction;
save: CallableFunction;
ranges: CallableFunction;
cachedChunks: CallableFunction;
preview: CallableFunction;
contextImage: CallableFunction;
search: CallableFunction;
Expand Down Expand Up @@ -1102,7 +1102,7 @@ export class Task extends Session {
delete: Object.getPrototypeOf(this).frames.delete.bind(this),
restore: Object.getPrototypeOf(this).frames.restore.bind(this),
save: Object.getPrototypeOf(this).frames.save.bind(this),
ranges: Object.getPrototypeOf(this).frames.ranges.bind(this),
cachedChunks: Object.getPrototypeOf(this).frames.cachedChunks.bind(this),
preview: Object.getPrototypeOf(this).frames.preview.bind(this),
contextImage: Object.getPrototypeOf(this).frames.contextImage.bind(this),
search: Object.getPrototypeOf(this).frames.search.bind(this),
Expand Down
39 changes: 11 additions & 28 deletions cvat-data/src/ts/cvat-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ interface BlockToDecode {
export class FrameDecoder {
private blockType: BlockType;
private chunkSize: number;
private startFrame: number;
private stopFrame: number;
/*
ImageBitmap when decode zip or video chunks
Blob when 3D dimension
Expand All @@ -104,6 +106,8 @@ export class FrameDecoder {
chunkSize: number,
cachedBlockCount: number,
dimension: DimensionType = DimensionType.DIMENSION_2D,
startFrame: number,
stopFrame: number,
) {
this.mutex = new Mutex();
this.orderedStack = [];
Expand All @@ -115,6 +119,8 @@ export class FrameDecoder {
this.renderHeight = 1080;
this.chunkSize = chunkSize;
this.blockType = blockType;
this.startFrame = startFrame;
this.stopFrame = stopFrame;

this.decodedChunks = {};
this.requestedChunkToDecode = null;
Expand Down Expand Up @@ -327,34 +333,11 @@ export class FrameDecoder {
}
}

get cachedChunks(): number[] {
return Object.keys(this.decodedChunks).map((chunkNumber: string) => +chunkNumber).sort((a, b) => a - b);
}

get cachedFrames(): string[] {
const chunkIsBeingDecoded = this.chunkIsBeingDecoded ?
public cachedChunks(includeInProgress = false): number[] {
const chunkIsBeingDecoded = includeInProgress && this.chunkIsBeingDecoded ?
Math.floor(this.chunkIsBeingDecoded.start / this.chunkSize) : null;
const chunks = Object.keys(this.decodedChunks)
.map((chunkNumber: string) => +chunkNumber)
.concat(...(chunkIsBeingDecoded !== null ? [chunkIsBeingDecoded] : []))
.sort((a, b) => a - b);
return chunks.map((chunk) => {
if (chunk === chunkIsBeingDecoded) {
return [this.chunkIsBeingDecoded.start, this.chunkIsBeingDecoded.end];
}
const frames = Object.keys(this.decodedChunks[chunk]).map((frame) => +frame);
const min = Math.min(...frames);
const max = Math.max(...frames);
return [min, max];
}).reduce<Array<[number, number]>>((acc, val) => {
if (acc.length && acc[acc.length - 1][1] + 1 === val[0]) {
const newMax = val[1];
acc[acc.length - 1][1] = newMax;
} else {
acc.push(val as [number, number]);
}

return acc;
}, []).map((val) => `${val[0]}:${val[1]}`);
return Object.keys(this.decodedChunks).map((chunkNumber: string) => +chunkNumber).concat(
...(chunkIsBeingDecoded !== null ? [chunkIsBeingDecoded] : []),
).sort((a, b) => a - b);
}
}
19 changes: 18 additions & 1 deletion cvat-ui/src/actions/annotation-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,24 @@ export function confirmCanvasReadyAsync(): ThunkAction {
try {
const state: CombinedState = getState();
const { instance: job } = state.annotation.job;
const ranges = await job.frames.ranges();
const chunks = await job.frames.cachedChunks() as number[];
const { startFrame, stopFrame, dataChunkSize } = job;

const ranges = chunks.map((chunk) => (
[
Math.max(startFrame, chunk * dataChunkSize),
Math.min(stopFrame, (chunk + 1) * dataChunkSize - 1),
]
)).reduce<Array<[number, number]>>((acc, val) => {
if (acc.length && acc[acc.length - 1][1] + 1 === val[0]) {
const newMax = val[1];
acc[acc.length - 1][1] = newMax;
} else {
acc.push(val as [number, number]);
}
return acc;
}, []).map(([start, end]) => `${start}:${end}`);

dispatch(confirmCanvasReady(ranges));
} catch (error) {
// even if error happens here, do not need to notify the users
Expand Down

0 comments on commit d4e49e7

Please sign in to comment.