Skip to content

Commit

Permalink
Disable annotation cache
Browse files Browse the repository at this point in the history
  • Loading branch information
timonegk committed Mar 29, 2021
1 parent 5590a2d commit 3f8e229
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ globals = {
mouseClickY: undefined,
mouseDownX: undefined,
mouseDownY: undefined,
currentAnnotations: undefined,
allAnnotations: undefined,
currentAnnotationsOfSelectedType: undefined,
stdColor: '#CC4444',
mutColor: '#CC0000'
};
Expand Down Expand Up @@ -51,7 +50,7 @@ function calculateImageScale() {
var gImageList;
var gMousepos;
let gAnnotationType = -1;
let gAnnotationCache = {};
let gCurrentAnnotations = [];
let gHighlightedAnnotation;

var gShiftDown;
Expand Down Expand Up @@ -144,8 +143,8 @@ function calculateImageScale() {
unsetHighlightColor: function() {}
};
}
if (globals.currentAnnotations) {
tool.drawExistingAnnotations(globals.currentAnnotations);
if (globals.currentAnnotationsOfSelectedType) {
tool.drawExistingAnnotations(globals.currentAnnotationsOfSelectedType);
}
console.log("Using tool " + tool.constructor.name);
}
Expand Down Expand Up @@ -242,7 +241,7 @@ function calculateImageScale() {
if (reload_list === true) {
loadImageList();
}
tool.drawExistingAnnotations(globals.currentAnnotations);
tool.drawExistingAnnotations(globals.currentAnnotationsOfSelectedType);
}
} else {
displayFeedback($('#feedback_annotation_exists'));
Expand All @@ -255,13 +254,12 @@ function calculateImageScale() {
}
}
// update current annotations
globals.allAnnotations = data.annotations;
globals.currentAnnotations = globals.allAnnotations.filter(function(e) {
gCurrentAnnotations = data.annotations;
globals.currentAnnotationsOfSelectedType = gCurrentAnnotations.filter(function(e) {
return e.annotation_type.id === gAnnotationType;
});
gAnnotationCache[gImageId] = globals.allAnnotations;

tool.drawExistingAnnotations(globals.currentAnnotations);
tool.drawExistingAnnotations(globals.currentAnnotationsOfSelectedType);

globals.editedAnnotationsId = undefined;
$('.annotation').removeClass('alert-info');
Expand Down Expand Up @@ -348,14 +346,13 @@ function calculateImageScale() {
headers: gHeaders,
dataType: 'json',
success: function(data) {
globals.allAnnotations = data.annotations;
globals.currentAnnotations = globals.allAnnotations.filter(function(e) {
gCurrentAnnotations = data.annotations;
globals.currentAnnotationsOfSelectedType = gCurrentAnnotations.filter(function(e) {
return e.annotation_type.id === gAnnotationType;
});
gAnnotationCache[gImageId] = globals.allAnnotations;
// redraw the annotations
tool.drawExistingAnnotations(globals.currentAnnotations);
displayExistingAnnotations(globals.allAnnotations);
tool.drawExistingAnnotations(globals.currentAnnotationsOfSelectedType);
displayExistingAnnotations(gCurrentAnnotations);
displayFeedback($('#feedback_annotation_deleted'));
globals.editedAnnotationsId = undefined;
},
Expand Down Expand Up @@ -482,7 +479,7 @@ function calculateImageScale() {
if (e.target.className !== 'annotation_edit_button') {
$('.annotation').removeClass('alert-info');
if (gHighlightedAnnotation) {
tool.unsetHighlightColor(gHighlightedAnnotation, globals.currentAnnotations.filter(function(element) {
tool.unsetHighlightColor(gHighlightedAnnotation, globals.currentAnnotationsOfSelectedType.filter(function(element) {
return element.id === gHighlightedAnnotation;
}));
gHighlightedAnnotation = undefined;
Expand All @@ -491,7 +488,7 @@ function calculateImageScale() {
// when the click was on the annotation edit button corresponding to the
// currently highlighted annotation, do not remove the blue highlight
if (gHighlightedAnnotation && gHighlightedAnnotation !== $(e.target).parent().data('annotationid')) {
tool.unsetHighlightColor(gHighlightedAnnotation, globals.currentAnnotations.filter(function(element) {
tool.unsetHighlightColor(gHighlightedAnnotation, globals.currentAnnotationsOfSelectedType.filter(function(element) {
return element.id === gHighlightedAnnotation;
}));
gHighlightedAnnotation = undefined;
Expand Down Expand Up @@ -537,7 +534,6 @@ function calculateImageScale() {
newImage.attr('id', 'image');
gImageId = imageId;
preloadImages();
preloadAnnotations();

currentImage.replaceWith(newImage);
globals.image = newImage;
Expand Down Expand Up @@ -790,7 +786,7 @@ function calculateImageScale() {
function handleShowAnnotationsToggle(event) {
globals.drawAnnotations = $('#draw_annotations').is(':checked');
if (globals.drawAnnotations) {
tool.drawExistingAnnotations(globals.currentAnnotations);
tool.drawExistingAnnotations(globals.currentAnnotationsOfSelectedType);
} else {
tool.clear();
}
Expand Down Expand Up @@ -845,7 +841,7 @@ function calculateImageScale() {
function handleResize() {
tool.cancelSelection();
calculateImageScale();
tool.drawExistingAnnotations(globals.currentAnnotations);
tool.drawExistingAnnotations(globals.currentAnnotationsOfSelectedType);
}

/**
Expand Down Expand Up @@ -902,13 +898,12 @@ function calculateImageScale() {

let handleNewAnnotations = function() {
// image is in cache.
globals.allAnnotations = gAnnotationCache[imageId];
globals.currentAnnotations = globals.allAnnotations.filter(function(e) {
globals.currentAnnotationsOfSelectedType = gCurrentAnnotations.filter(function(e) {
return e.annotation_type.id === gAnnotationType;
});
loading.addClass('hidden');
displayExistingAnnotations(globals.allAnnotations);
tool.drawExistingAnnotations(globals.currentAnnotations);
displayExistingAnnotations(gCurrentAnnotations);
tool.drawExistingAnnotations(globals.currentAnnotationsOfSelectedType);

if (globals.restoreSelection !== undefined) {
tool.restoreSelection();
Expand All @@ -918,16 +913,7 @@ function calculateImageScale() {
};

// load existing annotations for this image
if (gAnnotationCache[imageId] === undefined) {
// image is not available in cache. Load it.
loadAnnotationsToCache(imageId);
$(document).one("ajaxStop", handleNewAnnotations);
} else if ($.isEmptyObject(gAnnotationCache[imageId])) {
// we are already loading the annotation, wait for ajax
$(document).one("ajaxStop", handleNewAnnotations);
} else {
handleNewAnnotations();
}
loadAnnotations(imageId, handleNewAnnotations);
}

/**
Expand Down Expand Up @@ -996,8 +982,9 @@ function calculateImageScale() {
* Load the annotations of an image to the cache if they are not in it already.
*
* @param imageId
* @param cb callback for success
*/
function loadAnnotationsToCache(imageId) {
function loadAnnotations(imageId, cb) {
imageId = parseInt(imageId);

if (gImageList.indexOf(imageId) === -1) {
Expand All @@ -1007,13 +994,6 @@ function calculateImageScale() {
return;
}

if (gAnnotationCache[imageId] !== undefined) {
// already cached
return;
}
// prevent multiple ajax requests for the same image
gAnnotationCache[imageId] = {};

var params = {
image_id: imageId
};
Expand All @@ -1022,9 +1002,10 @@ function calculateImageScale() {
headers: gHeaders,
dataType: 'json',
success: function(data) {
// save the current annotations to the cache
gAnnotationCache[imageId] = data.annotations;
// save the current annotations
gCurrentAnnotations = data.annotations;
console.log("Saving annotations for", imageId);
cb();
},
error: function() {
console.log("Unable to load annotations for image" + imageId);
Expand Down Expand Up @@ -1083,35 +1064,6 @@ function calculateImageScale() {
}
}


/**
* Delete all images from cache except for those in Array keep
*
* @param keep Array of the image ids which should be kept in the cache.
*/
function pruneAnnotationCache(keep) {
for (var imageId in gAnnotationCache) {
imageId = parseInt(imageId);
if (gAnnotationCache[imageId] !== undefined && keep.indexOf(imageId) === -1) {
delete gAnnotationCache[imageId];
}
}
}

/**
* Preload next and previous annotations to cache.
*/
function preloadAnnotations() {
var keepAnnotations = [];
for (var imageId = gImageId - PRELOAD_BACKWARD;
imageId <= gImageId + PRELOAD_FORWARD;
imageId++) {
keepAnnotations.push(imageId);
loadAnnotationsToCache(imageId);
}
pruneAnnotationCache(keepAnnotations);
}

/**
* Scroll image list to make current image visible.
*/
Expand All @@ -1134,7 +1086,7 @@ function calculateImageScale() {

function handleAnnotationTypeChange() {
gAnnotationType = parseInt($('#annotation_type_id').val());
globals.currentAnnotations = globals.allAnnotations.filter(function(e) {
globals.currentAnnotationsOfSelectedType = gCurrentAnnotations.filter(function(e) {
return e.annotation_type.id === gAnnotationType;
});
setTool();
Expand Down Expand Up @@ -1218,7 +1170,6 @@ function calculateImageScale() {
gImageList = getImageList();
loadAnnotationTypeList();
preloadImages();
preloadAnnotations();
scrollImageList();

// W3C standards do not define the load event on images, we therefore need to use
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ class BoundingBoxes {
// array with all matching annotations
var matchingAnnotations = [];

for (var a in globals.currentAnnotations) {
var annotation = globals.currentAnnotations[a];
for (var a in globals.currentAnnotationsOfSelectedType) {
var annotation = globals.currentAnnotationsOfSelectedType[a];
if (annotation.annotation_type.id !== annotationType) {
continue;
}
Expand Down

0 comments on commit 3f8e229

Please sign in to comment.