-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
56 lines (47 loc) · 1.34 KB
/
main.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
let classifier;
let featureExtractor;
window.addEventListener("load", function () {
featureExtractor = ml5.featureExtractor("MobileNet", modelLoaded);
});
function modelLoaded() {
console.log("Model Ready");
classifier = featureExtractor.classification(videoElement, videoReady);
}
function videoReady() {
console.log("Video Ready");
loadData();
}
document.getElementById("phone").addEventListener("click", function () {
classifier.addImage("phone");
});
document.getElementById("book").addEventListener("click", function () {
classifier.addImage("book");
});
document.getElementById("training").addEventListener("click", function () {
classifier.train(function (lossValue) {
console.log("Loss is", lossValue);
if (lossValue === null) {
console.log("Training Complete");
document.getElementById("save").addEventListener("click", function () {
classifier.save();
});
}
});
});
function loadData() {
classifier.load("./model.json", readyToClassify);
}
function readyToClassify() {
console.log("Model Ready");
featureExtractor.classify(videoElement, gotResults);
}
function gotResults(error, results) {
if (error) {
console.error(error);
} else {
result.innerHTML = results[0].label + " " + results[0].confidence;
setTimeout(function () {
readyToClassify();
}, 1000);
}
}