Add blur sensitivity control and styling
Browse files- Add a 0px to 50px blur range slider
- Apply blur to the actual model input
- Show a live blurred preview
- Style the blur and top-k sliders consistently
- Support repeated score comparisons using the same image
- classifier.js +45 -9
classifier.js
CHANGED
|
@@ -41,12 +41,45 @@ export function getModelOption(key) {
|
|
| 41 |
return MODEL_OPTIONS.find((model) => model.key === key) || MODEL_OPTIONS[0];
|
| 42 |
}
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
export async function loadClassifier(modelKey, progressCallback) {
|
| 45 |
const option = getModelOption(modelKey);
|
| 46 |
-
|
| 47 |
-
if (classifierCache.has(option.key)) {
|
| 48 |
-
return classifierCache.get(option.key);
|
| 49 |
-
}
|
| 50 |
|
| 51 |
const classifier = await pipeline(option.task, option.modelId, {
|
| 52 |
dtype: option.dtype,
|
|
@@ -62,24 +95,27 @@ export async function classifyImage(imageDataUrl, options = {}) {
|
|
| 62 |
const modelKey = options.modelKey || MODEL_OPTIONS[0].key;
|
| 63 |
const topK = Number.isFinite(options.topK) ? options.topK : 5;
|
| 64 |
const option = getModelOption(modelKey);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
const classifier = await loadClassifier(modelKey, options.progressCallback);
|
| 66 |
|
| 67 |
let predictions;
|
| 68 |
-
|
| 69 |
if (option.task === 'zero-shot-image-classification') {
|
| 70 |
-
predictions = await classifier(
|
| 71 |
hypothesis_template: 'a photo of {}',
|
| 72 |
});
|
| 73 |
} else if (option.task === 'object-detection') {
|
| 74 |
-
predictions = await classifier(
|
| 75 |
threshold: options.threshold ?? 0.25,
|
| 76 |
percentage: false,
|
| 77 |
});
|
| 78 |
} else {
|
| 79 |
-
predictions = await classifier(
|
| 80 |
}
|
| 81 |
|
| 82 |
return [...predictions]
|
| 83 |
.sort((a, b) => Number(b.score) - Number(a.score))
|
| 84 |
.slice(0, topK);
|
| 85 |
-
}
|
|
|
|
| 41 |
return MODEL_OPTIONS.find((model) => model.key === key) || MODEL_OPTIONS[0];
|
| 42 |
}
|
| 43 |
|
| 44 |
+
function getSelectedBlurPixels() {
|
| 45 |
+
const slider = document.getElementById('blur-range');
|
| 46 |
+
if (!slider) return 0;
|
| 47 |
+
const value = Number(slider.value);
|
| 48 |
+
return Number.isFinite(value) ? Math.min(50, Math.max(0, value)) : 0;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
async function applyBlurToImageDataUrl(imageDataUrl, blurPixels) {
|
| 52 |
+
if (blurPixels <= 0) return imageDataUrl;
|
| 53 |
+
|
| 54 |
+
const image = new Image();
|
| 55 |
+
image.decoding = 'async';
|
| 56 |
+
image.src = imageDataUrl;
|
| 57 |
+
await image.decode();
|
| 58 |
+
|
| 59 |
+
const canvas = document.createElement('canvas');
|
| 60 |
+
canvas.width = image.naturalWidth || image.width;
|
| 61 |
+
canvas.height = image.naturalHeight || image.height;
|
| 62 |
+
|
| 63 |
+
const context = canvas.getContext('2d');
|
| 64 |
+
if (!context) throw new Error('Canvas is unavailable for blur preprocessing.');
|
| 65 |
+
|
| 66 |
+
// Expand the draw area so the blur does not create transparent edge pixels.
|
| 67 |
+
const padding = Math.ceil(blurPixels * 2);
|
| 68 |
+
context.filter = `blur(${blurPixels}px)`;
|
| 69 |
+
context.drawImage(
|
| 70 |
+
image,
|
| 71 |
+
-padding,
|
| 72 |
+
-padding,
|
| 73 |
+
canvas.width + padding * 2,
|
| 74 |
+
canvas.height + padding * 2,
|
| 75 |
+
);
|
| 76 |
+
|
| 77 |
+
return canvas.toDataURL('image/jpeg', 0.92);
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
export async function loadClassifier(modelKey, progressCallback) {
|
| 81 |
const option = getModelOption(modelKey);
|
| 82 |
+
if (classifierCache.has(option.key)) return classifierCache.get(option.key);
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
const classifier = await pipeline(option.task, option.modelId, {
|
| 85 |
dtype: option.dtype,
|
|
|
|
| 95 |
const modelKey = options.modelKey || MODEL_OPTIONS[0].key;
|
| 96 |
const topK = Number.isFinite(options.topK) ? options.topK : 5;
|
| 97 |
const option = getModelOption(modelKey);
|
| 98 |
+
const blurPixels = Number.isFinite(options.blurPixels)
|
| 99 |
+
? Math.min(50, Math.max(0, options.blurPixels))
|
| 100 |
+
: getSelectedBlurPixels();
|
| 101 |
+
const processedImageDataUrl = await applyBlurToImageDataUrl(imageDataUrl, blurPixels);
|
| 102 |
const classifier = await loadClassifier(modelKey, options.progressCallback);
|
| 103 |
|
| 104 |
let predictions;
|
|
|
|
| 105 |
if (option.task === 'zero-shot-image-classification') {
|
| 106 |
+
predictions = await classifier(processedImageDataUrl, options.candidateLabels, {
|
| 107 |
hypothesis_template: 'a photo of {}',
|
| 108 |
});
|
| 109 |
} else if (option.task === 'object-detection') {
|
| 110 |
+
predictions = await classifier(processedImageDataUrl, {
|
| 111 |
threshold: options.threshold ?? 0.25,
|
| 112 |
percentage: false,
|
| 113 |
});
|
| 114 |
} else {
|
| 115 |
+
predictions = await classifier(processedImageDataUrl, { top_k: topK });
|
| 116 |
}
|
| 117 |
|
| 118 |
return [...predictions]
|
| 119 |
.sort((a, b) => Number(b.score) - Number(a.score))
|
| 120 |
.slice(0, topK);
|
| 121 |
+
}
|