-
Notifications
You must be signed in to change notification settings - Fork 2
/
ml.js
174 lines (143 loc) · 5.41 KB
/
ml.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
173
174
// https://teachablemachine.withgoogle.com/models/m3rMjtrvw/
let modelUrl; // for storing the model URL
let isPredicting = true;
const options = {proabilityTheshold: 0.7};
let classifier; // the ml5 classifier
/*
used to store microbit responses to different inputs
the syntax is
gotClassName: servoSequence, writeDisplay
where servoSequence and writeDisplay are functions for controlling the servos and displays respectively
*/
let predictFns = [];
let modelUrlInput = document.querySelector('input#model');
let modelLoadBtn = document.getElementById('load-model-btn');
//TODO remove after testing
// let form = React.createElement(EventForm, { label: 'test', key: 0} );
// ReactDOM.render(form, document.getElementById('form-container'));
modelLoadBtn.addEventListener('click', ()=> {
let url = modelUrlInput.value;
// check that it's a valid URL
let isValid = validURL(url);
if(isValid){
// add model.json to URL name
url += "model.json";
// clear all existing labels
clearLabels();
try{
document.getElementById('form-test').remove();
}catch(error){};
// disable btn
modelLoadBtn.disabled = true;
modelLoadBtn.innerHTML = "Loading";
modelLoadBtn.classList += 'loading';
// load the new classifier
classifier = ml5.soundClassifier(url, options, modelReady);
}else{
alert('Invalid URL - please try pasting again');
}
});
function modelReady(){
console.log('model ready');
loadLabels();
document.getElementById('predictions').classList -= 'hidden';
// re-enable load button
modelLoadBtn.disabled = false;
modelLoadBtn.innerHTML = "Load Model";
modelLoadBtn.classList -= 'loading';
classifier.classify(gotResult);
}
function gotResult(error, result){
if(error){
console.log(error);
return;
}
// check if the result is from the most recently loaded model
let latestModelLabels = classifier.model.allLabels;
let resultLabels = Array.from(result, item => item.label);
let isLatestModel = latestModelLabels.every(a => resultLabels.some(b => a===b))
&& latestModelLabels.length == resultLabels.length;
// console.log(latestModelLabels, ' ', resultLabels);
if(isLatestModel){
let currentLabel = result[0].label;
if(isPredicting){
// console.log(currentLabel, ' ', result[0].confidence*100, '%');
removeActiveLabels();
setActiveLabel(formatLabel(currentLabel));
}
try{
if(isPredicting){
// run the script to control the microbit, as defined in programmingUI.js
predictFns[`got${formatLabel(currentLabel)}`]();
}
}catch(error){
console.error(error);
}
}
}
function removeActiveLabels(){
Array.from(document.getElementsByClassName('label')).forEach(
(el) => {
el.classList.remove('active');
}
);
Array.from(document.querySelectorAll('form .header')).forEach(
(el) => {
el.classList.remove('active');
}
);
}
function formatLabel(label){
return label.toLowerCase().replace(/\s/g, '');
}
function setActiveLabel(label){
document.getElementById(label).classList += ' active ';
document.querySelector(`#form-${label} .header`).classList += ' active';
}
function clearLabels(){
Array.from(document.querySelectorAll('#predictions .label')).forEach((el) => el.remove());
let formContainer = document.getElementById('form-container');
formContainer.innerHTML = '';
}
function loadLabels(){
let container = document.getElementById('predictions');
let labels = classifier.model.allLabels;
for(i=0; i<labels.length; i++){
let trimmedLabel = formatLabel(labels[i]);
// add prediction labels
let predictLabel = document.createElement('div');
predictLabel.innerHTML = labels[i];
predictLabel.classList += 'label ';
predictLabel.id = trimmedLabel;
container.prepend(predictLabel);
// create forms
let form = React.createElement(EventForm, { label: labels[i], key: i} );
let formContainer = document.createElement('div');
formContainer.id = `form-${trimmedLabel}`;
document.getElementById('form-container').prepend(formContainer);
ReactDOM.render(form, document.getElementById(formContainer.id));
// populate predictFns
let fnName = `got${trimmedLabel}`;
predictFns[fnName] = new Function([], null);
}
}
function togglePrediction(){
console.log('toggle prediction');
let togglePredictionBtn = document.getElementById('toggle-prediction-btn');
togglePredictionBtn.classList.toggle('isPredicting');
if(togglePredictionBtn.classList.contains('isPredicting')){
togglePredictionBtn.innerHTML = 'Pause Prediction';
}else{
togglePredictionBtn.innerHTML = 'Restart Prediction';
}
isPredicting = !isPredicting;
}
function validURL(str) {
var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator
return !!pattern.test(str);
}