-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
executable file
·172 lines (149 loc) · 3.78 KB
/
sketch.js
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
/*
KNN Classification took from https://www.youtube.com/watch?v=Mwo5_bUVhlA
*/
let video;
let features;
let knn;
let labelP;
let labelQuest;
let labelScore;
let ready = false;
let label = 'nothing';
let start = false;
class Question {
constructor(quest, answ) {
this.question = quest;
this.answer = answ;
}
}
let counter = 0;
let score = 0;
let questionIndex = -1;
let currentQuestion = new Question("Ready?", true);
let questions = [
new Question("Mark Zuckerberg is human", false),
new Question("Tabs are better than spaces!", true),
new Question("AI is just a bunch of if statement", true)
];
function setup() {
let canvas = createCanvas(500, 400);
canvas.parent('canvascontainer');
textAlign(CENTER, CENTER);
textSize(32);
background(61);
video = createCapture(VIDEO);
video.size(200, 200);
video.position(21, -5);
features = ml5.featureExtractor('MobileNet', modelReady);
knn = ml5.KNNClassifier();
}
function draw() {
if (!ready && knn.getNumLabels() > 0) {
goClassify();
ready = true;
}
if (start) {
if (counter > 150) {
if (label == "true" && currentQuestion.answer)
score++;
if (label == "false" && !currentQuestion.answer)
score++;
questionIndex++;
currentQuestion = questions[questionIndex];
counter = 0;
}
if (questionIndex < questions.length) {
if (label != "nothing")
counter++;
text((questionIndex + 1) + ". " + currentQuestion.question, width / 2, height / 2 + 80)
text("Score: " + score, width - 140, 90)
} else {
video.hide();
if (score > questions.length / 2)
background("#0fff4b");
else
background("#ff430f");
text("Score: " + score, width / 2, height / 2);
}
} else {
fill(255);
text("Train the classifier!", width / 2, height / 2 + 70 - 16);
text("Press S to start", width / 2, height / 2 + 70 + 16);
}
}
function keyPressed() {
const logits = features.infer(video);
if (key == 'T') {
knn.addExample(logits, 'true');
console.log('true');
} else if (key == 'F') {
knn.addExample(logits, 'false');
console.log('false');
} else if (key == 'X') {
save(knn, 'model.json');
//knn.save('model.json');
} else if (key == 'S') {
start = true;
} else if (key == 'L') {
knn.load('./model.json', function () {
console.log('knn loaded');
});
}
}
function modelReady() {
console.log('model ready!');
// Comment back in to load your own model!
// knn.load('model.json', function() {
// console.log('knn loaded');
// });
}
function goClassify() {
const logits = features.infer(video);
knn.classify(logits, function (error, result) {
if (error) {
console.error(error);
} else {
label = result.label;
if (label == "true")
background("#0fff4b");
if (label == "false")
background("#ff430f");
goClassify();
}
});
}
// Temporary save code until ml5 version 0.2.2
const save = (knn, name) => {
const dataset = knn.knnClassifier.getClassifierDataset();
if (knn.mapStringToIndex.length > 0) {
Object.keys(dataset).forEach(key => {
if (knn.mapStringToIndex[key]) {
dataset[key].label = knn.mapStringToIndex[key];
}
});
}
const tensors = Object.keys(dataset).map(key => {
const t = dataset[key];
if (t) {
return t.dataSync();
}
return null;
});
let fileName = 'myKNN.json';
if (name) {
fileName = name.endsWith('.json') ? name : `${name}.json`;
}
saveFile(fileName, JSON.stringify({ dataset, tensors }));
};
const saveFile = (name, data) => {
const downloadElt = document.createElement('a');
const blob = new Blob([data], { type: 'octet/stream' });
const url = URL.createObjectURL(blob);
downloadElt.setAttribute('href', url);
downloadElt.setAttribute('download', name);
downloadElt.style.display = 'none';
document.body.appendChild(downloadElt);
downloadElt.click();
document.body.removeChild(downloadElt);
URL.revokeObjectURL(url);
};