-
Notifications
You must be signed in to change notification settings - Fork 911
/
sketch.js
68 lines (56 loc) 路 1.55 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
// Copyright (c) 2019 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
/* ===
ml5 Example
Image classification using Convolutional Neural Network
This example uses a callback pattern to create the classifier
=== */
let nn;
const IMAGE_WIDTH = 64;
const IMAGE_HEIGHT = 64;
const IMAGE_CHANNELS = 4;
const images = [];
let testA;
function preload() {
for (let i = 1; i < 7; i += 1) {
const a = loadImage(`images/A_0${i}.png`);
const b = loadImage(`images/B_0${i}.png`);
images.push({ image: a, label: 'A' });
images.push({ image: b, label: 'B' });
}
testA = loadImage(`images/A_test.png`);
}
function setup() {
createCanvas(128, 128);
image(testA, 0, 0, width, height);
const options = {
inputs: [IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS],
task: 'imageClassification',
debug: true,
};
// construct the neural network
nn = ml5.neuralNetwork(options);
// add data
for (let i = 0; i < images.length; i += 1) {
nn.addData({ image: images[i].image }, { label: images[i].label });
}
// normalize data
nn.normalizeData();
nn.train({ epochs: 20 }, finishedTraining);
}
function finishedTraining() {
console.log('finished training');
// method 1: you can pass in an object with a matching key and the p5 image
nn.classify({ image: testA }, gotResults);
}
function gotResults(err, results) {
if (err) {
console.log(err);
return;
}
console.log(results);
const percent = 100 * results[0].confidence;
createP(`${results[0].label} ${nf(percent, 2, 1)}%`);
}