All files / lib classification.svelte.js

0% Statements 0/14
0% Branches 0/4
0% Functions 0/6
0% Lines 0/14

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                                                                                                                                   
import { match, type } from 'arktype';
 
import { tables } from './idb.svelte.js';
import { MetadataInferOptionsNeural } from './schemas/metadata.js';
import { uiState } from './state.svelte.js';
 
/**
 * Classifies an image using the current protocol and selected model.
 * @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é');
	}
 
	if (!uiState.classificationMetadataId) {
		console.warn(
			'No metadata with neural inference defined, not analyzing image. Configure neural inference on a enum metadata (set metadata.<your metadata id>.infer.neural) if this was not intentional.'
		);
		return;
	}
 
	const { cancel, request: done } = swarpc.classify.cancelable({
		imageId: id,
		metadataIds: {
			cropbox: uiState.cropMetadataId,
			target: uiState.classificationMetadataId
		},
		taskSettings: classificationInferenceSettings(
			uiState.currentProtocol,
			uiState.selectedClassificationModel
		)
	});
 
	cancellers?.set(id, cancel);
 
	await done;
 
	await tables.Image.refresh(uiState.currentSessionId);
}
 
/**
 *
 * @param {import('$lib/database.js').Protocol} protocol
 * @param {number} modelIndex index du modèle à utiliser dans la liste des modèles pour le protocole actuel
 */
export function classificationInferenceSettings(protocol, modelIndex) {
	const matcher = match
		.case(
			{
				id: type.string.narrow((id) => protocol.metadata.includes(id)),
				type: '"enum"',
				infer: MetadataInferOptionsNeural
			},
			(m) => m.infer.neural[modelIndex]
		)
		.default(() => undefined);
 
	return tables.Metadata.state
		.map((m) => matcher(m))
		.filter(Boolean)
		.at(0);
}