-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageList.js
453 lines (397 loc) · 17.4 KB
/
imageList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import { drawImageFromList, getImageProperties, calculateCurrentImageHeightRatio } from './uploadImages/drawImageOnCanvas.js';
import { updateCrosshairDimensionsAndHideAsync, moveCanvasCrosshairViaLastCanvasPositionAsync } from '../../canvas/mouseInteractions/cursorModes/drawWithCrosshairMode.js';
import { removeAllShapeRefs, retrieveAllShapeRefs } from '../../canvas/objects/allShapes/allShapes.js';
import { retrieveAllLabelRefs, removeAllLabelRefs } from '../../canvas/objects/label/label.js';
import { repopulateLabelAndShapeObjects } from '../../canvas/objects/allShapes/labelAndShapeBuilder.js';
import { resetZoom, zoomOutObjectOnImageSelect } from '../toolkit/buttonClickEvents/facadeWorkers/zoomWorker.js';
import { removeAllLabelListItems } from '../labelList/labelList.js';
import { switchCanvasContainerElements } from '../../canvas/utils/canvasUtils.js';
import labelProperties from '../../canvas/objects/label/properties.js';
import { initialiseImageListML } from './imageListML.js';
import { getCanvasReferences } from '../../canvas/utils/fabricUtils.js';
import purgeCanvasMouseEvents from '../../canvas/mouseInteractions/mouseEvents/resetCanvasUtils/purgeAllMouseHandlers.js';
import assignDefaultEvents from '../../canvas/mouseInteractions/mouseEvents/eventHandlers/defaultEventHandlers.js';
import { updateImageNameElement } from '../imageSwitchPanel/style.js';
import scrollIntoViewIfNeeded from '../utils/tableUtils.js';
import { setDefaultCursorMode } from '../../canvas/mouseInteractions/cursorModes/defaultMode.js';
import { changeExistingImagesMovability } from '../settingsPopup/options/movableObjects.js';
import { removeWatermarkFromCanvasAreaBackground } from '../../canvas/utils/watermark.js';
import { setZoomInButtonToDefault, setCreatePolygonButtonToActive, setRemoveImagesButtonDefault,
setCreateBoundingBoxButtonToDefault, setCreatePolygonButtonToDefault, setEditShapesButtonToActive,
setCreateBoundingBoxButtonToActive, setPolygonEditingButtonsToDefault } from '../toolkit/styling/state.js';
import { getDefaultState, setCurrentImageId, getContinuousDrawingState,
getCrosshairUsedOnCanvasState, getLastDrawingModeState } from '../state.js';
import { getStatementsForCurrentImageToJSON } from '../../canvas/objects/allShapes/allShapes.js';
let currentlyActiveElement = null;
let imageContainerElement = null;
const images = [];
let currentlySelectedImageId = 0;
let canvas = null;
let newImageId = 0;
let hasCurrentImageThumbnailRedBorder = false;
const ANIMATION_DURATION_MILLISECONDS = 300;
function initialiseImageElement() {
return document.createElement('img');
}
function appendAnimationReadyStyling(imageThumbnailElement) {
imageThumbnailElement.style.transition = `${ANIMATION_DURATION_MILLISECONDS / 1000}s`;
imageThumbnailElement.style.maxHeight = '0%';
}
function initiateDivElement() {
return document.createElement('div');
}
function triggerAnimation(imageThumbnailElement) {
setTimeout(() => {
imageThumbnailElement.style.maxHeight = '100%';
setTimeout(() => {
imageThumbnailElement.style.transition = '';
}, ANIMATION_DURATION_MILLISECONDS);
});
}
function GetFileBlobUsingURL(url, convertBlob) {
let xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.responseType = "blob";
xhr.addEventListener('load', function() {
convertBlob(xhr.response);
});
xhr.send();
}
function blobToFile(blob, name) {
blob.lastModifiedDate = new Date();
blob.name = name;
return blob;
}
function GetFileObjectFromURL(filePathOrUrl, convertBlob) {
GetFileBlobUsingURL(filePathOrUrl, function (blob) {
convertBlob(blobToFile(blob, 'testFile.jpg'));
});
}
// imageData is a base64 encoded image is necessary for adding image to image list
// TODO: to find place where image was converted into base64
function addNewImage(imageName, imageData) {
const thumbnailElementRef = addNewItemToImageList(imageData);
const imageObject = {
data: imageData, name: imageName, shapes: {}, labels: {}, thumbnailElementRef,
};
images.push(imageObject);
}
function changeCurrentImageNameElementText(imageName, isfirstFromMany) {
updateImageNameElement(imageName, images, currentlySelectedImageId, isfirstFromMany);
}
function displayUploadedImage(imageMetadata, isfirstFromMany) {
highlightImageThumbnail(images[newImageId].thumbnailElementRef.childNodes[1]);
saveAndRemoveCurrentImageDetails();
changeCurrentImageNameElementText(imageMetadata.name, isfirstFromMany);
images[newImageId].thumbnailElementRef.scrollIntoView();
removeWatermarkFromCanvasAreaBackground();
setToolkitStylingOnNewImage();
if (getCrosshairUsedOnCanvasState()) updateCrosshairDimensionsAndHideAsync(canvas);
}
function setDefaultImageProperties(image, imageMetadata) {
image.imageDimensions = { scaleX: 1, scaleY: 1 };
image.shapes = {};
image.labels = {};
image.size = imageMetadata.size;
image.numberOfMLGeneratedShapes = 0;
image.analysedByML = false;
}
//// unnecessary to consider
function setCurrentlyActiveElementToInvisible() {
if (currentlyActiveElement) {
currentlyActiveElement.style.display = 'none';
setSelectedMLThumbnailColourOverlayBackToDefault(currentlyActiveElement);
}
}
function highlightImageThumbnail(element) {
setCurrentlyActiveElementToInvisible();
setMLThumbnailOverlayToMLSelected(element);
if (hasCurrentImageThumbnailRedBorder) {
element.style.borderColor = '#ff4d4d';
}
element.style.display = 'block';
currentlyActiveElement = element;
}
function addNewItemToImageList(imageData) {
const imageThumbnailElement = initialiseImageElement();
imageThumbnailElement.id = newImageId;
imageThumbnailElement.classList.add('image-list-thumbnail-image');
// console.log("imageData.src", imageData.src);
imageThumbnailElement.src = imageData.src;
imageThumbnailElement.setAttribute('draggable', false);
imageThumbnailElement.setAttribute('ondragstart', 'return false'); // for firefox
appendAnimationReadyStyling(imageThumbnailElement);
const colorOverlayElement = initiateDivElement();
colorOverlayElement.classList.add('image-list-thumbnail-color-overlay');
colorOverlayElement.classList.add('image-list-thumbnail-default');
const tickSVGElement = initialiseImageElement();
tickSVGElement.classList.add('image-list-thumbnail-SVG-tick-icon');
tickSVGElement.src = 'assets/svg/done-tick-highlighted.svg';
const parentThumbnailDivElement = initiateDivElement();
parentThumbnailDivElement.classList.add('image-list-thumbnail');
parentThumbnailDivElement.onclick = window.switchImage.bind(this, newImageId);
parentThumbnailDivElement.appendChild(imageThumbnailElement);
parentThumbnailDivElement.appendChild(colorOverlayElement);
parentThumbnailDivElement.appendChild(tickSVGElement);
imageContainerElement.appendChild(parentThumbnailDivElement);
triggerAnimation(imageThumbnailElement);
return parentThumbnailDivElement;
}
function displayTickSVGOverImageThumbnail(id) {
const imageId = id !== undefined ? id : currentlySelectedImageId;
if (images[imageId]) images[imageId].thumbnailElementRef.childNodes[2].style.display = 'block';
}
function removeTickSVGOverImageThumbnail(id) {
images[id].thumbnailElementRef.childNodes[2].style.display = 'none';
}
function getLastImageIdByName(imageName) {
for (let i = images.length - 1; i >= 0; i -= 1) {
if (imageName === images[i].name) {
return i;
}
}
return null;
}
function findImageContainerElement() {
imageContainerElement = document.getElementById('image-list-image-container');
}
function initialiseImageList() {
findImageContainerElement();
initialiseImageListML(images);
}
function getAllImageData() {
return images;
}
function updateCurrentImageIds(currentId, newId) {
currentlySelectedImageId = currentId;
newImageId = newId;
}
function assignCanvasToImageList(canvasObj) {
canvas = canvasObj;
}
function addSingleImageToList(imageMetadata, imageData) {
addNewImage(imageMetadata.name, imageData);
setDefaultImageProperties(images[newImageId], imageMetadata);
displayUploadedImage(imageMetadata, false);
newImageId += 1;
}
function addImageFromMultiUploadToList(imageMetadata, imageData, isfirstFromMany) {
addNewImage(imageMetadata.name, imageData);
setDefaultImageProperties(images[newImageId], imageMetadata);
if (images.length === 1 || isfirstFromMany) {
displayUploadedImage(imageMetadata, isfirstFromMany);
}
newImageId += 1;
}
// to replicate the bug, carry out the following:
// upload image, draw bounding box, upload new image, come back to the first
// and use diagonal scaling to the right edge
// NOTE: some of the code to fix a similar bug is located in the purgeAllMouseEvents.js file
function fixForObjectScalingBugOnCanvasSwitch() {
const { canvas1, canvas2 } = getCanvasReferences();
if (canvas1 && canvas1.__eventListeners && canvas1.__eventListeners['object:scaling'] && canvas1.__eventListeners['object:scaling'].length > 1) {
assignDefaultEvents(canvas2, null, false);
}
}
// the following function is implemented to set properties for unseen shapes that have been
// uploaded where I had to originally setDefault(false) in order to trigger some of the contained
// functions in order to set the objects correctly. There is still some scepticism for whether
// this is working correctly, hence please be cautions.
function resetCanvasForUnseenShapes() {
purgeCanvasMouseEvents(canvas);
setDefaultCursorMode(canvas);
assignDefaultEvents(canvas, null, false);
}
// the reason why we do not use scaleX/scaleY is because these are returned in
// a promise as the image is drawn hence we do not have it at this time
// (for the new image)
function changeToExistingImage(id) {
if (currentlySelectedImageId >= 0) { captureCurrentImageData(); }
removeAllLabelListItems();
const timesZoomedOut = resetZoom(true);
drawImageFromList(images[id].data);
repopulateLabelAndShapeObjects(images[id].shapes, images[id].labels,
images[id].imageDimensions, images[id].data);
switchCanvasContainerElements();
changeExistingImagesMovability(images[id].shapes);
if (currentlySelectedImageId >= 0) {
zoomOutObjectOnImageSelect(images[currentlySelectedImageId].shapes,
images[currentlySelectedImageId].labels, timesZoomedOut);
}
setCurrentImageId(id);
highlightImageThumbnail(images[id].thumbnailElementRef.childNodes[1]);
scrollIntoViewIfNeeded(images[id].thumbnailElementRef, imageContainerElement);
fixForObjectScalingBugOnCanvasSwitch();
currentlySelectedImageId = id;
changeCurrentImageNameElementText(images[currentlySelectedImageId].name);
resetCanvasForUnseenShapes();
setToolkitStylingOnNewImage();
if (getCrosshairUsedOnCanvasState()) moveCanvasCrosshairViaLastCanvasPositionAsync();
}
function switchImage(direction) {
if (direction === 'previous') {
if (currentlySelectedImageId !== 0) {
changeToExistingImage(currentlySelectedImageId - 1);
}
} else if (direction === 'next') {
if (currentlySelectedImageId !== images.length - 1) {
changeToExistingImage(currentlySelectedImageId + 1);
}
} else if (direction !== currentlySelectedImageId) {
changeToExistingImage(direction);
}
}
function canSwitchImage(direction) {
if (direction === 'previous') {
return currentlySelectedImageId > 0;
}
if (direction === 'next') {
return currentlySelectedImageId < (images.length - 1);
}
return direction !== currentlySelectedImageId;
}
function changeImageThumbnailBorderColorToRed() {
if (currentlyActiveElement) {
hasCurrentImageThumbnailRedBorder = true;
currentlyActiveElement.style.borderColor = '#ff4d4d';
}
}
function resetImageThumbnailBorderColor() {
hasCurrentImageThumbnailRedBorder = false;
if (currentlyActiveElement) {
currentlyActiveElement.style.borderColor = '';
}
}
function saveAndRemoveCurrentImageDetails() {
if (images.length > 1) {
captureCurrentImageData();
} else {
setEditShapesButtonToActive();
}
removeAllLabelListItems();
const timesZoomedOut = resetZoom(false);
zoomOutObjectOnImageSelect(images[currentlySelectedImageId].shapes,
images[currentlySelectedImageId].labels, timesZoomedOut);
setMLGeneratedShapesToOriginalColorPallette();
currentlySelectedImageId = newImageId;
setCurrentImageId(newImageId);
}
function setToolkitStylingOnNewImage() {
if (!getDefaultState() && getContinuousDrawingState()) {
const lastDrawnShapeState = getLastDrawingModeState();
if (lastDrawnShapeState === 'polygon') {
setCreatePolygonButtonToActive();
setCreateBoundingBoxButtonToDefault();
} else if (lastDrawnShapeState === 'boundingBox') {
setCreateBoundingBoxButtonToActive();
setCreatePolygonButtonToDefault();
} else {
assignDefaultEvents(canvas, null, false);
setCreatePolygonButtonToDefault();
setCreateBoundingBoxButtonToDefault();
}
} else {
setCreatePolygonButtonToDefault();
setCreateBoundingBoxButtonToDefault();
}
setPolygonEditingButtonsToDefault();
setRemoveImagesButtonDefault();
setZoomInButtonToDefault();
}
function setDefaultImageThumbnailHighlightToMLSelected(element) {
element.childNodes[1].classList.replace('image-list-thumbnail-default', 'image-list-thumbnail-machine-learning-selected');
const imageId = element.childNodes[0].id;
displayTickSVGOverImageThumbnail(imageId);
}
function setDefaultImageThumbnailHighlightToML(element) {
element.childNodes[1].classList.replace('image-list-thumbnail-default', 'image-list-thumbnail-machine-learning');
element.childNodes[1].style.display = 'block';
const imageId = element.childNodes[0].id;
displayTickSVGOverImageThumbnail(imageId);
}
function removeMLThumbnailHighlight(element) {
element.childNodes[1].classList.replace('image-list-thumbnail-machine-learning', 'image-list-thumbnail-default');
element.childNodes[1].style.display = 'none';
}
function removeSelectedMLThumbnailHighlight(element) {
element.childNodes[1].classList.replace('image-list-thumbnail-machine-learning-selected', 'image-list-thumbnail-default');
}
function setMLThumbnailOverlayToMLSelected(element) {
if (element.classList.contains('image-list-thumbnail-machine-learning')) {
element.classList.replace('image-list-thumbnail-machine-learning', 'image-list-thumbnail-machine-learning-selected');
}
}
function setSelectedMLThumbnailColourOverlayBackToDefault(element) {
if (element.classList.contains('image-list-thumbnail-machine-learning-selected')) {
element.classList.replace('image-list-thumbnail-machine-learning-selected', 'image-list-thumbnail-default');
displayTickSVGOverImageThumbnail();
}
}
function setMLGeneratedShapesToOriginalColorPallette() {
if (images[currentlySelectedImageId].numberOfMLGeneratedShapes > 0) {
Object.keys(images[currentlySelectedImageId].shapes).forEach((key) => {
const shape = images[currentlySelectedImageId].shapes[key].shapeRef;
if (shape.MLPallette) {
shape.fill = shape.trueFill;
shape.stroke = shape.trueStroke;
shape.MLPallette = false;
}
});
images[currentlySelectedImageId].numberOfMLGeneratedShapes = 0;
}
}
// making JSON file
function exportJSON(){
let objectJSON = {};
objectJSON = getStatementsForCurrentImageToJSON(images);
writeJSON(objectJSON);
}
function writeJSON(objectJSON){
const stringJSON = JSON.stringify(objectJSON);
downloadJSON(stringJSON, 'annotation.json');
}
function downloadJSON(jsonData, filename) {
// Convert the JSON data to a Blob
const jsonDataBlob = new Blob([jsonData], { type: 'application/json' });
// Create a URL for the Blob
const downloadUrl = URL.createObjectURL(jsonDataBlob);
// Create a temporary anchor element
const link = document.createElement('a');
link.href = downloadUrl;
link.download = filename; // Set the desired filename for the downloaded file
// Append the anchor to the DOM and trigger the download
document.body.appendChild(link);
link.click();
// Clean up the URL and anchor
URL.revokeObjectURL(downloadUrl);
document.body.removeChild(link);
}
// evoked before uploading new image or swithching the images
function captureCurrentImageData() {
getStatementsForCurrentImageToJSON(images);
images[currentlySelectedImageId].labels = retrieveAllLabelRefs();
images[currentlySelectedImageId].shapes = retrieveAllShapeRefs();
removeAllLabelRefs();
removeAllShapeRefs();
const currentlySelectedImageProperties = getImageProperties();
const imageDimensions = {};
imageDimensions.scaleX = currentlySelectedImageProperties.scaleX;
imageDimensions.scaleY = currentlySelectedImageProperties.scaleY;
imageDimensions.originalWidth = currentlySelectedImageProperties.originalWidth;
imageDimensions.originalHeight = currentlySelectedImageProperties.originalHeight;
imageDimensions.oldImageHeightRatio = calculateCurrentImageHeightRatio();
imageDimensions.polygonOffsetLeft = labelProperties.pointOffsetProperties().left;
imageDimensions.polygonOffsetTop = labelProperties.pointOffsetProperties().top;
images[currentlySelectedImageId].imageDimensions = imageDimensions;
}
export {
assignCanvasToImageList,
setDefaultImageThumbnailHighlightToML, switchImage, canSwitchImage,
changeImageThumbnailBorderColorToRed, resetImageThumbnailBorderColor,
displayTickSVGOverImageThumbnail, getAllImageData, initialiseImageList,
addSingleImageToList, setSelectedMLThumbnailColourOverlayBackToDefault,
addImageFromMultiUploadToList, updateCurrentImageIds, getLastImageIdByName,
setDefaultImageThumbnailHighlightToMLSelected, removeTickSVGOverImageThumbnail,
removeMLThumbnailHighlight, removeSelectedMLThumbnailHighlight,
exportJSON, GetFileObjectFromURL,
};