-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
81 lines (71 loc) · 2.42 KB
/
app.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
// helpful documentation:
// 1. https://ml5js.org/reference/api-KNNClassifier/
// 2. https://learn.ml5js.org/docs/#/
const trainBtn1 = document.querySelector(".btn-1");
const trainBtn2 = document.querySelector(".btn-2");
const submitBtn = document.querySelector(".submit-btn");
const imageCounter1 = document.querySelector(".image-counter1");
const imageCounter2 = document.querySelector(".image-counter2");
const resultText = document.querySelector(".result-text");
let video1 = document.getElementById("video1");
let video2 = document.getElementById("video2");
let video3 = document.getElementById("video3");
let ml5Features = ml5.featureExtractor("MobileNet", () => {});
let knn = ml5.KNNClassifier();
//setting up the cam-video capture component
async function setUpVideo(v) {
const stream = await navigator.mediaDevices.getUserMedia({
video: true,
});
v.srcObject = stream;
v.play();
}
setUpVideo(video1);
setUpVideo(video2);
video3.style.display = "none";
resultText.style.display = "none";
//adding data, training the model
function addData(v, className) {
const logits = ml5Features.infer(v);
knn.addExample(logits, className);
const totalImage = knn.getCount();
imageCounter1.innerText = `${totalImage[0] ? totalImage[0] : 0}/ minimum 10`;
imageCounter2.innerText = `${totalImage[1] ? totalImage[1] : 0}/ minimum 10`;
}
//getting the result
function getResult(v) {
const logits = ml5Features.infer(v);
//classifying the video instance (logits) and getting the result based on training data
knn.classify(logits, (error, result) => {
if (error) {
console.error(error);
} else {
//showing result on the dom
resultText.innerText = result.label;
//running the classification function continuously.
setTimeout(() => {
getResult(v);
}, 50);
}
});
}
trainBtn1.addEventListener("click", () => {
addData(video1, "class1");
});
trainBtn2.addEventListener("click", () => {
addData(video2, "class2");
});
submitBtn.addEventListener("click", () => {
//removing some element and adding some element
video1.style.display = "none";
video2.style.display = "none";
trainBtn1.style.display = "none";
trainBtn2.style.display = "none";
submitBtn.style.display = "none";
imageCounter1.style.display = "none";
imageCounter2.style.display = "none";
video3.style.display = "inline";
resultText.style.display = "block";
setUpVideo(video3);
getResult(video3);
});