xxxxxxxxxx
191
// A regression using MobileNet, ml5.js, p5js. Based on:
// https://editor.p5js.org/ml5/sketches/FeatureExtractor_Image_Regression
let featureExtractorX;
let featureExtractorY;
let regressorX;
let regressorY;
let video;
let statusString = "STATUS_ADD_SAMPLES";
let trainingLossX;
let trainingLossY;
let nSamples = 0;
let predictionValue01;
let predictionValue02;
let xpoints = [60, 330, 580];
let ypoints = [98, 440];
//--------------------------------------
function setup() {
createCanvas(680, 480);
video = createCapture(VIDEO);
video.hide();
featureExtractorX = ml5.featureExtractor('MobileNet');
regressorX = featureExtractorX.regression(video);
featureExtractorY = ml5.featureExtractor('MobileNet');
regressorY = featureExtractorY.regression(video);
}
//--------------------------------------
function draw() {
background('white');
tint(255, 255, 255, 64);
image(video, 0, 0, 680, 480);
// draw a very simple "slider"
noFill();
stroke(0);
rect(0, 0, width, 10);
rect(width-10, 0, 10, height);
var mx = mouseX;
var my = mouseY;
if (predictionValue01) {
mx = map(predictionValue01, 0, 1, 0, width);
}
if (predictionValue02) {
my = map(predictionValue02, 0, 1, 0, height);
}
rect(mx, 0, 1, 10);
rect(width-10, my, 10, 1);
//text(constrain(mx, 0, width), 200, 30);
//text(constrain(my, 0, height), 200, 40);
// draw a red circle whose X location is
// proportional to our predicted value
var positionX = width / 2;
var positionY = height / 2;
if (predictionValue01) {
positionX = map(predictionValue01, 0, 1, 0, width);
}
if (predictionValue02) {
positionY = map(predictionValue02, 0, 1, 0, height);
}
noStroke();
fill(255, 0, 0);
ellipse(positionX, positionY, 50, 50);
// draw diagnostic/debug information
fill('black');
var instructions = "Press s and adjust mouseX and mouseY to add samples. \n";
instructions += "Press t to train model. \n";
instructions += "Press p to start predicting. \n";
text(instructions, 15, 30);
text("status: " + statusString, 15, 75);
text("nSamples: " + nSamples, 15, 90);
text("trainingLossX: " + trainingLossX, 15, 105);
text("trainingLossY: " + trainingLossY, 15, 120);
var pStr = (predictionValue01) ? nf(predictionValue01,1,3):"undefined";
var pStr2 = (predictionValue02) ? nf(predictionValue02,1,3):"undefined";
text("prediction1: " + pStr, 15, 135);
text("prediction2: " + pStr2, 15, 150);
}
//--------------------------------------
function keyPressed() {
if (key == 's') { // add sample
/*var xValue = constrain(map(mouseX, 0, width, 0, 1), 0, 1);
var yValue = constrain(map(mouseY, 0, height, 0, 1), 0, 1);
regressorX.addImage(xValue);
regressorY.addImage(yValue);
nSamples++;*/
for(var i = 0; i < 6; i++) {
let filename = "/dataset" + str(i) + "/";
for(var j = 0; j < 5; j++) {
filename += "d" + str(i) + "_" + str(j) + ".jpeg.jpg";
regressorX.addImage(filename, xpoints[i-3]);
regressorY.addImage(filename, ypoints[i/3]);
nSamples++;
}
}
} else if (key == '1') {
for(var q = 0; q < 46; q++) {
let filename = "/CENT_TRIG/IMG_" + str(q+6167) + ".JPG";
regressorX.addImage(filename, xpoints[1]);
regressorY.addImage(filename, ypoints[1]);
nSamples++;
}
} else if (key == '2') {
for(var w = 0; w < 40; w++) {
let filename = "/CENT_UNTRIG/IMG_" + str(w+6106) + ".JPG";
regressorX.addImage(filename, xpoints[1]);
regressorY.addImage(filename, ypoints[0]);
nSamples++;
}
} else if (key == '3') {
for(var e = 0; e < 37; e++) {
let filename = "/LEFT_TRIG/IMG_" + str(e+6304) + ".JPG";
regressorX.addImage(filename, xpoints[0]);
regressorY.addImage(filename, ypoints[1]);
nSamples++;
}
} else if (key == '4') {
for(var r = 0; r < 36; r++) {
let filename = "/LEFT_UNTRIG/IMG_" + str(r+6231) + ".JPG";
regressorX.addImage(filename, xpoints[0]);
regressorY.addImage(filename, ypoints[0]);
nSamples++;
}
} else if (key == '5') {
for(var t = 0; t < 34; t++) {
let filename = "/RIGHT_TRIG/IMG_" + str(t+6398) + ".JPG";
regressorX.addImage(filename, xpoints[2]);
regressorY.addImage(filename, ypoints[1]);
nSamples++;
}
} else if (key == '6') {
for(var y = 0; y < 40; y++) {
let filename = "/RIGHT_UNTRIG/IMG_" + str(y+6341) + ".JPG";
regressorX.addImage(filename, xpoints[2]);
regressorY.addImage(filename, ypoints[0]);
nSamples++;
}
} else if (key == 't') { // train the regressor
regressorX.train(function(lossValueX) {
if (lossValueX) {
trainingLossX = lossValueX;
statusString = "STATUS_TRAINING_MODEL X";
} else {
statusString = "STATUS_DONE_TRAINING X";
}
});
regressorY.train(function(lossValueY) {
if (lossValueY) {
trainingLossY = lossValueY;
statusString += "...STATUS_TRAINING_MODEL Y";
} else {
statusString += "...STATUS_DONE_TRAINING Y";
}
});
} else if (key == 'p') { // initiate prediction
statusString = "STATUS_PREDICTING";
regressorX.predict(gotResultsCallbackX);
regressorY.predict(gotResultsCallbackY);
}
}
//--------------------------------------
// Store the results, and restart the process.
function gotResultsCallbackX(err, result) {
if (err) {
console.error(err);
}
if (result && result.value) {
predictionValue01 = result.value;
regressorX.predict(gotResultsCallbackX);
}
}
function gotResultsCallbackY(err, result) {
if (err) {
console.error(err);
}
if (result && result.value) {
predictionValue02 = result.value;
regressorY.predict(gotResultsCallbackY);
}
}