-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
113 lines (98 loc) · 2.65 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Daniel Shiffman
// http://youtube.com/thecodingtrain
// http://codingtra.in
// Transfer Learning Feature Extractor Classification with ml5
// https://youtu.be/eeO-rWYFuG0
let mobilenet;
let classifier;
let video;
let loss;
let label = 'test';
let img;
let flowerTyp;
let data = {};
let dataFlower = [];
let canvas;
function preload() {
data = loadJSON('json_ml5data.json', jsonReady);
}
// Change the status when the model loads.
function modelReady(){
select('#status').html('Model Loaded')
}
// Change the status when the model loads.
function jsonReady(){
select('#status2').html('JSON Loaded')
}
function whileTraining(loss) {
if (loss == null) {
console.log('Training Complete');
// classifier.classify(gotResults);
} else {
console.log(loss);
}
}
function setup() {
var c = createCanvas(244, 244);
background(100);
c.drop(gotFile);
const options = {version: 1, epochs: 50 };
mobilenet = ml5.featureExtractor('MobileNet', options, modelReady);
classifier = mobilenet.classification();
for (let n = 0; n < 5; n++){
for (let i = 0; i < 22; i++){
// Load the image
imgPfad = data.children[n].children[i].path;
flowerTyp = data.children[n].children[i].type;
select('#pfad').html(imgPfad);
htmlPic = "<img src=\"" + imgPfad + "\">";
img = createImg(imgPfad).hide();
classifier.addImage(img, flowerTyp);
console.log(imgPfad + " hinzugefügt als " + flowerTyp);
}
}
trainButton = createButton('train');
trainButton.mousePressed(function() {
classifier.train(whileTraining);
});
saveButton = createButton('save');
saveButton.mousePressed(function() {
classifier.save();
console.log('Custom Model saved!!');
});
loadButton = createButton('load');
loadButton.mousePressed(function() {
classifier.load('model.json');
console.log('Custom Model loaded');
});
testButton = createButton('test');
testButton.mousePressed(function() {
img = createImg('../img/Hohler_Lerchensporn/8.jpg', testModel);
});
}
function testModel() {
// Get a prediction for that image
classifier.classify(img, function(err, result) {
console.log(result);
select('#result').html(result);
});
}
function gotFile(file) {
// If it's an image file
if (file.type === 'image') {
// Create an image DOM element but don't show it
var img = createImg(file.data, testModel).hide();
// Draw the image onto the canvas
image(img, 0, 0, width, height);
} else {
console.log('Not an image file!');
}
}
function draw() {
fill(255);
noStroke();
textSize(12);
textAlign(CENTER);
text('Drag an image after loading Custom Model.', width/2, height/2);
noLoop();
}