Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | /**
* @import { DimensionsInput } from '$lib/database.js';
*/
import { toRelativeCoords } from '$lib/BoundingBoxes.svelte';
import { processExifData } from '$lib/exif';
import { tables } from '$lib/idb.svelte';
import { errorMessageImageTooLarge, imageId, resizeToMaxSize, storeImageBytes } from '$lib/images';
import { uiState } from '$lib/state.svelte.js';
import { toasts } from '$lib/toasts.svelte';
import * as dates from 'date-fns';
import { imageLimits } from './inference_utils';
import { serializeMetadataValues } from './metadata';
export const ACCEPTED_IMPORT_TYPES = [
'image/jpeg',
'application/zip',
'image/png',
'image/tiff',
'.cr2',
'.rw2',
'.dng',
'.crw',
'.raw',
'.cr3'
];
/**
* @param {File} file
* @param {string} id
*/
export async function processImageFile(file, id) {
if (!uiState.currentProtocol) {
toasts.error('Aucun protocole sélectionné');
return;
}
const originalBytes = await file.arrayBuffer();
if (originalBytes.byteLength > imageLimits.maxMemoryUsageInMB * Math.pow(2, 20)) {
toasts.error(errorMessageImageTooLarge());
return;
}
const [[width, height], resizedBytes] = await resizeToMaxSize({ source: file });
await storeImageBytes({
id,
resizedBytes,
originalBytes,
contentType: file.type,
filename: file.name,
width,
height
});
await tables.Image.set({
id: imageId(id, 0),
filename: file.name,
addedAt: dates.formatISO(Date.now()),
contentType: file.type,
dimensions: { width, height },
fileId: id,
metadata: {}
});
// We have to remove the file from the processing files list once the Image database object has been created
uiState.processing.removeFile(id);
await processExifData(uiState.currentProtocol.id, id, originalBytes, file).catch((error) => {
console.error(error);
toasts.error(`Erreur lors de l'extraction des métadonnées EXIF pour ${file.name}`);
});
}
/**
* @param {import('swarpc').SwarpcClient<typeof import('$worker/procedures.js').PROCEDURES>} swarpc
* @param {undefined | Map<string, import("swarpc").CancelablePromise["cancel"]>} cancellers
* @param {string} fileId
* @returns {Promise<void>}
*/
export async function inferBoundingBoxes(swarpc, cancellers, fileId) {
if (!uiState.currentProtocol) {
toasts.error('Aucun protocole sélectionné');
return;
}
if (!uiState.currentProtocol.crop.infer) {
console.warn(
'No crop inference defined, not analyzing image. Configure crop inference in the protocol (crop.infer) if this was not intentional.'
);
return;
}
if (!uiState.cropInferenceAvailable) {
return;
}
const image = await tables.Image.get(imageId(fileId, 0));
if (!image) {
throw new Error(`Image ${fileId} not found in database`);
}
if (!image.fileId) {
throw new Error(`Image ${fileId} has no associated ImageFile in database`);
}
const inference = swarpc.inferBoundingBoxes.cancelable({
fileId: image.fileId,
taskSettings: $state.snapshot(uiState.currentProtocol.crop.infer[uiState.selectedCropModel])
});
cancellers?.set(image.fileId, inference.cancel);
const { boxes, scores } = await inference.request.catch((error) => {
if (/(maxMemoryUsageInMB|maxResolutionInMP) limit exceeded/.test(error?.toString())) {
return Promise.reject(new Error(errorMessageImageTooLarge()));
}
return Promise.reject(error);
});
let [firstBoundingBox] = boxes;
let [firstScore] = scores;
if (!firstBoundingBox || !firstScore) {
await tables.Image.update(image.id, 'boundingBoxesAnalyzed', true);
return;
}
/**
* @param {[number, number, number, number]} param0
*/
const toCropBox = ([x, y, w, h]) => toRelativeCoords(uiState.currentProtocol)({ x, y, w, h });
for (let i = 0; i < boxes.length; i++) {
await tables.Image.set({
...image,
id: imageId(image.fileId, i),
addedAt: dates.formatISO(i === 0 ? image.addedAt : Date.now()),
boundingBoxesAnalyzed: true,
metadata: {
...serializeMetadataValues(image.metadata),
[uiState.cropMetadataId]: {
value: JSON.stringify(toCropBox(boxes[i])),
confidence: scores[i],
alternatives: {},
manuallyModified: false
}
}
});
}
}
|