-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
76 lines (61 loc) · 1.4 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
let mobilenet;
let classifier;
let video;
let label = 'Loading model';
let goodBatti;
let badBatti;
let trainButton;
function modelReady() {
label='Model ready';
console.log('Model is ready!!!');
}
function videoReady() {
console.log('Video is ready!!!');
}
function setup() {
createCanvas(320, 270);
video = createCapture(VIDEO);
video.hide();
background(0);
mobilenet = ml5.featureExtractor('MobileNet', modelReady);
classifier = mobilenet.classification(video, videoReady);
goodBatti = createButton('Good Batti');
goodBatti.mousePressed(function () {
classifier.addImage('Good Batti');
});
badBatti = createButton('Bad Batti');
badBatti.mousePressed(function () {
classifier.addImage('Bad Batti');
});
trainButton = createButton('Train');
trainButton.mousePressed(function () {
classifier.train(whileTraining);
});
saveButton = createButton('Save');
saveButton.mousePressed(function () {
classifier.save();
});
}
function draw() {
background(0);
image(video, 0, 0, 320, 240);
fill(255);
textSize(16);
text(label, 10, height - 10);
}
function whileTraining(loss) {
if (loss == null) {
console.log('Training Complete');
classifier.classify(gotResults);
} else {
console.log(loss);
}
}
function gotResults(error, result) {
if (error) {
console.error(error);
} else {
label = result;
classifier.classify(gotResults);
}
}