xxxxxxxxxx
131
let video;
let poseNet;
let poses = [];
let skeletons = [];
let keypoint = [];
//const osc = new p5.Oscillator();
let osc, playing, freq, amp;
function setup() {
canvas = createCanvas(screen.width, screen.height);
video = createCapture(VIDEO);
video.size(width, height);
poseNet = ml5.poseNet(video, modelReady);
poseNet.on('pose', function (results) {
poses = results;
});
video.hide();
//const osc = new p5.Oscillator('sine');
//osc.freq(440);
//osc.start();
}
function modelReady() {
select('#status').html('Model Loaded');
}
function draw() {
background(0,10);
// image(video, 0, 0, width, height);
drawKeypoints();
drawSkeleton();
}
function drawKeypoints() {
for (let i = 0; i < poses.length; i++) {
// For each pose detected, loop through all the keypoints
for (let j = 0; j < poses[i].pose.keypoints.length; j++) {
let keypoint = poses[i].pose.keypoints[j];
// Only draw an ellipse is the pose probability is bigger than 0.2
if (keypoint.score > 0.2) {
noStroke();
fill (255, random(0, 125), 0);
// fill(255, 0, 0);
ellipse(keypoint.position.x, keypoint.position.y, random(10, 40));
//}
// osc.start();
// osc.freq(map(keypoint.position.x, 0, width, 200, 800));
if (mouseIsPressed){
image(video, 0, 0, width, height);
fill(255, 0, 0);
noStroke();
ellipse(keypoint.position.x, keypoint.position.y, 10, 10);
noFill();
stroke(255, 0, 0);
strokeWeight(random (2,10));
ellipse(keypoint.position.x, keypoint.position.y, random(20, 80));
}
}
}
}
}
// A function to draw the skeletons
function drawSkeleton() {
// Loop through all the skeletons detected
for (let i = 0; i < poses.length; i++) {
// For every skeleton, loop through all body connections
for (let j = 0; j < poses[i].skeleton.length; j++) {
let partA = poses[i].skeleton[j][0];
let partB = poses[i].skeleton[j][1];
stroke(255, 0, 0);
strokeWeight(random(2,6));
line(partA.position.x, partA.position.y, partB.position.x, partB.position.y);
}
}
}
function keyTyped() {
if (key === 's') {
save('myCanvas.jpg');
}
if (key == 'm'){
osc.start();
const osc = new p5.Oscillator('sine');
osc.freq(map(keypoint.position.x, 0, width, 200, 800));
}
}
//function makeSound() {
// osc.freq(map(keypoint.position.x, 0, width, 200, 800));
//}
/*function count(){
if (poses[i]>20) {
square(keypoint.position.x, keypoint.position.y, random(20, 80));
}
}*/