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 | import { RequestCancelledError } from 'swarpc';
import { databaseHandle, tables } from './idb.svelte.js';
import { inferenceModelId } from './inference.js';
import { storeMetadataErrors } from './metadata/storage.js';
import { uiState } from './state.svelte.js';
import { safeJSONStringify } from './utils.js';
/**
* Classifies an image using the current protocol with all configured classification models.
* @param {import('swarpc').SwarpcClient<typeof import('$worker/procedures.js').PROCEDURES>} swarpc
* @param {string} id
* @param {Map<string, import("swarpc").CancelablePromise["cancel"]>} [cancellers]
*/
export async function classifyImage(swarpc, id, cancellers) {
if (!uiState.currentProtocol) {
throw new Error('Aucun protocole sélectionné');
}
// Get all classification metadata for the current protocol
const allClassificationMetadata = uiState.enabledClassificationMetadata;
if (allClassificationMetadata.length === 0) {
console.warn(
'No metadata with neural inference defined, not analyzing image. Configure neural inference on enum metadata (set metadata.<your metadata id>.infer.neural) if this was not intentional.'
);
return;
}
// Classify with all metadata
const promises = allClassificationMetadata.map(async (metadata) => {
const modelIndex = uiState.selectedClassificationModels[metadata.id] ?? 0;
const allModels = uiState.allClassificationModels[metadata.id];
if (!allModels || !allModels[modelIndex]) {
console.warn(
`No model found for metadata ${metadata.id} at index ${modelIndex}, skipping`
);
return;
}
const settings = $state.snapshot(allModels[modelIndex]);
// Generate the inference session ID based on the protocol and model
const inferenceSessionId = inferenceModelId(uiState.currentProtocol.id, settings.model);
const taskSettings = {
...settings,
output: {
name: settings.output?.name ?? 'output0',
// XXX: $state.snapshot turns the type of output.select into a string, idk why cuz at runtime it isnt
select: allModels[modelIndex].output?.select?.toJSON(),
},
};
const { cancel, request: done } = swarpc.classify.cancelable({
imageId: id,
taskSettings,
inferenceSessionId,
metadataIds: {
cropbox: uiState.cropMetadataId,
target: metadata.id,
},
});
cancellers?.set(`${id}:${metadata.id}`, cancel);
try {
await done;
} catch (error) {
if (error instanceof RequestCancelledError) throw error;
if (!uiState.currentSessionId) throw error;
await storeMetadataErrors(
{
db: databaseHandle(),
subjectId: id,
sessionId: uiState.currentSessionId,
metadataId: metadata.id,
},
{
kind: 'inference',
message: error instanceof Error ? error.message : String(error),
stack: error instanceof Error ? (error.stack ?? '') : '',
ignored: false,
details: {
taskSettings,
image: await tables.Image.get(id),
fullError: safeJSONStringify(error),
},
}
);
}
});
await Promise.all(promises);
await tables.Image.refresh(uiState.currentSessionId);
}
|