-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
143 lines (121 loc) · 3.13 KB
/
Copy pathscript.js
File metadata and controls
143 lines (121 loc) · 3.13 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
const LABEL_TO_DIGIT = {
zero: 0,
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9,
};
let nn;
let canvas;
let input;
let hasDrawn = false;
// DOM handles, filled in on setup()
const ui = {};
function setup() {
canvas = createCanvas(400, 400);
canvas.parent("stage");
background(0);
cacheElements();
buildScoreRows();
ui.clear.addEventListener("click", clearCanvas);
nn = ml5.neuralNetwork({
inputs: [56, 56, 4],
task: "imageClassification",
debug: true,
});
const modelDetails = {
model: "model/model.json",
metadata: "model/model_meta.json",
weights: "model/model.weights.bin",
};
nn.load(modelDetails, () => {
console.log("Model Loaded!");
setStatus("ready", "Model ready");
classifyCanvas();
});
}
function cacheElements() {
ui.stage = document.getElementById("stage");
ui.hint = document.getElementById("hint");
ui.clear = document.getElementById("clear");
ui.status = document.querySelector(".status");
ui.statusText = document.querySelector(".status__text");
ui.guessDigit = document.getElementById("guess-digit");
ui.guessConfidence = document.getElementById("guess-confidence");
ui.scores = document.getElementById("scores");
ui.rows = {};
}
function buildScoreRows() {
for (let digit = 0; digit <= 9; digit++) {
const row = document.createElement("li");
row.className = "score";
row.innerHTML = `
<span class="score__digit">${digit}</span>
<span class="score__value">0.0%</span>`;
ui.scores.appendChild(row);
ui.rows[digit] = {
row,
value: row.querySelector(".score__value"),
};
}
}
function setStatus(state, text) {
ui.status.dataset.status = state;
ui.statusText.textContent = text;
}
function draw() {
if (mouseIsPressed && isOnCanvas()) {
if (!hasDrawn) {
hasDrawn = true;
ui.hint.hidden = true;
}
stroke(255);
strokeWeight(40);
strokeCap(ROUND);
line(mouseX, mouseY, pmouseX, pmouseY);
}
}
function isOnCanvas() {
return mouseX >= 0 && mouseX <= width && mouseY >= 0 && mouseY <= height;
}
function clearCanvas() {
background(0);
hasDrawn = false;
ui.hint.hidden = false;
}
function keyPressed() {
if (keyCode === 67) clearCanvas();
}
function classifyCanvas() {
input = createGraphics(28, 28);
input.copy(canvas, 0, 0, width, height, 0, 0, 28, 28);
nn.classify({ image: input }, (error, result) => {
if (error) {
console.error(error);
setStatus("error", "Classification failed");
return;
}
renderResults(result);
classifyCanvas();
});
}
function renderResults(result) {
const top = result[0];
const topDigit = LABEL_TO_DIGIT[top.label];
ui.guessDigit.textContent = hasDrawn ? topDigit : "–";
ui.guessConfidence.textContent = hasDrawn
? `${(top.confidence * 100).toFixed(1)}%`
: "—";
for (const { label, confidence } of result) {
const digit = LABEL_TO_DIGIT[label];
const row = ui.rows[digit];
if (!row) continue;
row.value.textContent = `${(confidence * 100).toFixed(1)}%`;
row.row.classList.toggle("score--top", hasDrawn && digit === topDigit);
}
}