-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
209 lines (172 loc) · 6.99 KB
/
script.js
File metadata and controls
209 lines (172 loc) · 6.99 KB
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
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const classifyBtn = document.getElementById('classifyBtn');
const clearBtn = document.getElementById('clearBtn');
const resultDiv = document.getElementById('result');
const loadingDiv = document.getElementById('loading');
const errorDiv = document.getElementById('error');
const debugModeCheckbox = document.getElementById('debugMode');
const debugPanel = document.getElementById('debugPanel');
const debugImagesContainer = document.getElementById('debugImagesContainer');
const GRID_SIZE = 28;
const PIXEL_SIZE = canvas.width / GRID_SIZE;
let pixels = Array(GRID_SIZE).fill().map(() => Array(GRID_SIZE).fill(0));
let isDrawing = false;
const API_URL = 'http://localhost:5000/predict';
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.imageSmoothingEnabled = false;
function drawGrid() {
ctx.strokeStyle = '#333';
ctx.lineWidth = 0.5;
for (let i = 0; i <= GRID_SIZE; i++) {
ctx.beginPath();
ctx.moveTo(i * PIXEL_SIZE, 0);
ctx.lineTo(i * PIXEL_SIZE, canvas.height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, i * PIXEL_SIZE);
ctx.lineTo(canvas.width, i * PIXEL_SIZE);
ctx.stroke();
}
}
function getGridPosition(e) {
const rect = canvas.getBoundingClientRect();
const x = (e.clientX || e.touches[0].clientX) - rect.left;
const y = (e.clientY || e.touches[0].clientY) - rect.top;
const gridX = Math.floor(x / PIXEL_SIZE);
const gridY = Math.floor(y / PIXEL_SIZE);
return { gridX, gridY };
}
function fillPixel(gridX, gridY, intensity = 255) {
if (gridX < 0 || gridX >= GRID_SIZE || gridY < 0 || gridY >= GRID_SIZE) return;
pixels[gridY][gridX] = Math.min(255, pixels[gridY][gridX] + intensity);
const pixelValue = pixels[gridY][gridX];
ctx.fillStyle = `rgb(${pixelValue}, ${pixelValue}, ${pixelValue})`;
ctx.fillRect(gridX * PIXEL_SIZE, gridY * PIXEL_SIZE, PIXEL_SIZE, PIXEL_SIZE);
}
function fillPixelWithBrush(gridX, gridY) {
fillPixel(gridX, gridY, 255);
const neighbors = [
{x: gridX-1, y: gridY, intensity: 80},
{x: gridX+1, y: gridY, intensity: 80},
{x: gridX, y: gridY-1, intensity: 80},
{x: gridX, y: gridY+1, intensity: 80},
{x: gridX-1, y: gridY-1, intensity: 40},
{x: gridX+1, y: gridY-1, intensity: 40},
{x: gridX-1, y: gridY+1, intensity: 40},
{x: gridX+1, y: gridY+1, intensity: 40}
];
neighbors.forEach(n => {
if (n.x >= 0 && n.x < GRID_SIZE && n.y >= 0 && n.y < GRID_SIZE) {
fillPixel(n.x, n.y, n.intensity);
}
});
}
function startDrawing(e) {
e.preventDefault();
isDrawing = true;
const { gridX, gridY } = getGridPosition(e);
fillPixelWithBrush(gridX, gridY);
}
function draw(e) {
if (!isDrawing) return;
e.preventDefault();
const { gridX, gridY } = getGridPosition(e);
fillPixelWithBrush(gridX, gridY);
}
function stopDrawing(e) {
if (e) e.preventDefault();
isDrawing = false;
}
canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', stopDrawing);
canvas.addEventListener('mouseleave', stopDrawing);
canvas.addEventListener('touchstart', startDrawing);
canvas.addEventListener('touchmove', draw);
canvas.addEventListener('touchend', stopDrawing);
canvas.addEventListener('touchcancel', stopDrawing);
canvas.addEventListener('contextmenu', (e) => e.preventDefault());
clearBtn.addEventListener('click', () => {
pixels = Array(GRID_SIZE).fill().map(() => Array(GRID_SIZE).fill(0));
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
drawGrid();
resultDiv.classList.add('hidden');
errorDiv.classList.add('hidden');
debugPanel.classList.add('hidden');
});
classifyBtn.addEventListener('click', async () => {
try {
resultDiv.classList.add('hidden');
errorDiv.classList.add('hidden');
debugPanel.classList.add('hidden');
loadingDiv.classList.remove('hidden');
classifyBtn.disabled = true;
const debugMode = debugModeCheckbox.checked;
const response = await fetch(API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
pixels: pixels,
debug: debugMode
})
});
if (!response.ok) {
throw new Error('Failed to classify image');
}
const data = await response.json();
document.getElementById('predictionValue').textContent = data.prediction;
document.getElementById('confidenceValue').textContent =
(data.confidence * 100).toFixed(2) + '%';
const probContainer = document.getElementById('probabilitiesContainer');
probContainer.innerHTML = '';
for (let i = 0; i < 10; i++) {
const prob = data.probabilities[i] * 100;
const item = document.createElement('div');
item.className = 'prob-item';
item.innerHTML = `
<span class="prob-label">${i}:</span>
<div class="prob-bar-container">
<div class="prob-bar" style="width: ${prob}%"></div>
</div>
<span class="prob-value">${prob.toFixed(2)}%</span>
`;
probContainer.appendChild(item);
}
resultDiv.classList.remove('hidden');
if (debugMode && data.debug_images) {
debugImagesContainer.innerHTML = '';
const stages = [
{ key: 'original', label: 'Original (28×28)' },
{ key: 'cropped', label: 'Cropped' },
{ key: 'resized', label: 'Resized (~20×20)' },
{ key: 'before_blur', label: 'Centered (28×28)' },
{ key: 'final', label: 'Final (after blur)' }
];
stages.forEach(stage => {
if (data.debug_images[stage.key]) {
const container = document.createElement('div');
container.className = 'debug-image-container';
container.innerHTML = `
<div class="debug-image-label">${stage.label}</div>
<img src="${data.debug_images[stage.key]}" class="debug-image" alt="${stage.label}">
`;
debugImagesContainer.appendChild(container);
}
});
debugPanel.classList.remove('hidden');
}
} catch (error) {
errorDiv.textContent = 'Error: ' + error.message +
'. Make sure the backend server is running!';
errorDiv.classList.remove('hidden');
} finally {
loadingDiv.classList.add('hidden');
classifyBtn.disabled = false;
}
});
drawGrid();