-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path03_videoClassification.js
53 lines (43 loc) · 1.06 KB
/
03_videoClassification.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
/* ===
ML5 SFPC Workshop
Example 03
Webcam Classification with ML5.js and p5.js
=== */
let imagenet;
let video;
let result;
let confidence;
function preload() {
// Initialize ImageNet with the MobileNet model.
imagenet = new ml5.ImageNet('MobileNet');
}
function setup() {
createCanvas(320, 240);
video = createCapture(VIDEO);
// Create the elements to hold the results
result = createP('');
confidence = createP('');
// Set the input size and hide the video
video.attribute('width', 227);
video.attribute('height', 227);
video.hide();
guess();
}
function guess() {
// Get a prediction for that image
imagenet.predict(video.elt, 10, gotResult);
}
function draw() {
// Draw a larger video on the canvas
image(video, 0, 0, width, height);
}
// When we get the results
function gotResult(results) {
// The results are in an array ordered by probability.
result.html(results[0].label);
confidence.html(results[0].probability, 0, 2);
// Console the results
// console.log(results);
// Start again every 250ms
setTimeout(guess, 250);
}