-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
62 lines (51 loc) · 1.32 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
// Initialize the Image Classifier method with MobileNet
const classifier = ml5.imageClassifier('MobileNet', modelLoaded);
let img;
let capture;
let label = "Empty";
let canvasWidth = 500;
let canvasHeight = 500;
let labelHeight = 100;
let textHeight = 25;
let textVertPadding = 20;
let textHorizPadding = 20;
let modelOK = false;
// When the model is loaded
function modelLoaded() {
console.log('Model Loaded!');
label = "AI is ready....to take over the Wrold! mwah, mwah, mwah!"
modelOK = true;
}
function classify() {
// Make a prediction with a selected image
classifier.classify(capture, (err, results) => {
console.log(results);
if (err) {
console.err(err);
} else {
label = results[0].label;
}
});
}
function preload() {
img = loadImage("imgs/penguin.jpeg");
capture = createCapture(VIDEO);
capture.size(canvasWidth, canvasHeight);
capture.hide();
}
function setup() {
createCanvas(canvasWidth, canvasHeight);
textSize(textHeight);
fill(255);
stroke(0);
strokeWeight(4);
setInterval(classify, 500);
}
function draw() {
background(220);
// Displays the image at its actual size at point (0,0)
image(capture, 0, 0, width, height - labelHeight);
if (modelOK) {
text(label, 0 + textHorizPadding, height-labelHeight + textVertPadding, width, labelHeight);
}
}