xxxxxxxxxx
146
// 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 targetLabel;
let poseLabel = "circle";
function setup() {
createCanvas(640, 480);
video = createCapture(VIDEO);
video.hide();
poseNet = ml5.bodyPose(video, gotPoses);
// poseNet.on("pose", );
rectMode(CENTER);
ellipseMode(CENTER);
let options = {
inputs: 34,
// outputs: circle, triangle, rectangle
outputs: 3,
task: "classification",
debug: true,
};
brain = ml5.neuralNetwork(options);
const modelInfo = {
model: "model/model.json",
metadata: "model/model_meta.json",
weights: "model/model.weights.bin",
};
brain.load(modelInfo, brainLoaded);
// brain.loadData('shapes.json', trainModel);
}
function brainLoaded() {
console.log("pose classification ready!");
classifyPose();
}
function classifyPose() {
if (pose) {
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);
}
brain.classify(inputs, gotResult);
} else {
setTimeout(classifyPose, 100);
}
}
function gotResult(error, results) {
poseLabel = results[0].label;
console.log(results);
console.log(results[0].label);
classifyPose();
}
function trainModel() {
brain.normalizeData();
brain.train({ epochs: 100 }, finished);
}
function finished() {
console.log("model trained");
brain.save();
}
function modelLoaded() {
console.log("poseNet ready");
}
function gotPoses(poses) {
// console.log(poses);
if (poses.length > 0) {
pose = poses[0].pose;
skeleton = poses[0].skeleton;
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 draw() {
push();
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);
}
}
pop();
// Draw the shapes
if (poseLabel == "t") {
fill(255, 0, 255, 80);
noStroke();
triangle(100, height- 100, width / 2, 100, width-100, height-100);
}
if (poseLabel == "c") {
fill(255, 0, 255, 80);
noStroke();
circle(width / 2, height / 2, 400);
}
if (poseLabel == "r") {
fill(255, 0, 255, 80);
noStroke();
rect(width / 2, height / 2, 400);
}
}