xxxxxxxxxx
130
// https://www.youtube.com/watch?v=FYgYyq-xqAw&list=PLRqwX-V7Uu6YPSwT06y_AEYTqIwbeam3y&index=24&ab_channel=TheCodingTrain
let video;
let poseNet;
let pose;
let skeleton;
let brain;
let state = "waiting";
let targetLabel;
let shapesData;
function preload(){
shapesData = loadJSON('shapes.json');
}
function setup() {
createCanvas(640, 480);
video = createCapture(VIDEO);
video.hide();
poseNet = ml5.poseNet(video, modelLoaded);
poseNet.on("pose", gotPoses);
let options = {
inputs: 34,
//outputs: circle, triangle, rectangle, rhombus
outputs: 4,
task: "classification",
debug: true,
};
brain = ml5.neuralNetwork(options);
/*
the error is you typed shapes.JSON (it should be in lower case .json)
*/
brain.loadData('shapes.json', dataReady);
// console.log(typeof int('shapes.json'));
}
function keyPressed() {
if (key == "t") {
console.log("starting training");
/*
You typed model.normalizeData(), but your neural network variable is brain. so it is brain.normalizeData()
*/
brain.normalizeData();
let options = {
epochs: 50,
};
// brain.train(options, whileTraining, finishedTraining);
} else {
targetLabel = key;
}
}
function whileTraining(epoch, loss) {
console.log(epoch);
}
function finishedTraining() {
console.log("finished training");
}
function dataReady() {
brain.normalizeData();
brain.train({ epochs: 50 }, whileTraining, finishedTraining);
}
function finished() {
console.log("model trained");
brain.save();
}
function gotPoses(poses) {
// console.log(poses);
if (poses.length > 0) {
pose = poses[0].pose;
skeleton = poses[0].skeleton;
if (state == "collecting");
{
let inputs = [];
for (let i = 0; i < pose.keypoints.length; i++) {
let x = pose.keypoints[i].position.x;
let y = pose.keypoints[i].position.y;
inputs.push(x);
inputs.push(y);
}
let target = [targetLabel];
brain.addData(inputs, target);
}
}
}
function modelLoaded() {
console.log("poseNet ready");
}
function draw() {
translate(video.width, 0);
scale(-1, 1);
image(video, 0, 0, video.width, video.height);
if (pose) {
// The line that connects each keypoint
for (let i = 0; i < skeleton.length; i++) {
let a = skeleton[i][0];
let b = skeleton[i][1];
strokeWeight(2);
stroke(255);
line(a.position.x, a.position.y, b.position.x, b.position.y);
}
// The seven keypoints
for (let i = 0; i < pose.keypoints.length; i++) {
let x = pose.keypoints[i].position.x;
let y = pose.keypoints[i].position.y;
fill(0, 255, 0);
stroke(255);
ellipse(x, y, 16, 16);
}
}
}