xxxxxxxxxx
69
let handPose;
let video;
let hands = [];
let targetLabel;
let brain;
let state = 'waiting';
function preload() {
// Load the handPose model
handPose = ml5.handPose({ flipped: true });
}
function setup() {
createCanvas(640, 480);
video = createCapture(VIDEO, { flipped: true });
video.size(640, 480);
video.hide();
handPose.detectStart(video, gotHands);
let options = {
inputs: 42,
outputs: 3,
task: "classification",
debug: true,
};
brain = ml5.neuralNetwork(options);
brain.loadData("shapes.json", trainModel);
}
function trainModel() {
brain.normalizeData();
brain.train({ epochs: 100 }, finished);
}
function finished() {
console.log("model trained");
brain.save("shapes_trained");
}
function gotHands(results) {
// save the output to the hands variable
hands = results;
}
function draw() {
image(video, 0, 0, width, height);
if (hands.length > 0) {
for (let i = 0; i < hands.length; i++) {
let hand = hands[i];
let inputs = [];
for (let j = 0; j < hand.keypoints.length; j++) {
let keypoint = hand.keypoints[j];
let x = keypoint.x;
let y = keypoint.y;
inputs.push(x);
inputs.push(y);
noStroke();
circle(x, y, 10);
}
let target = [targetLabel];
brain.addData(inputs, target);
// console.log(inputs);
}
}
}