xxxxxxxxxx
148
let video;
let poseNet;
let pose;
let w = 10;
let columns, rows;
let board;
function setup() {
frameRate(18)
createCanvas(windowWidth, windowHeight);
video = createCapture(VIDEO);
video.hide();
video.elt.style.display = "none";
poseNet = ml5.poseNet(video, modelLoaded);
poseNet.on('pose', gotPoses);
columns = max(1, floor(width / w));
rows = max(1, floor(height / w));
board = create2DArray(columns, rows);
for (let i = 1; i < columns - 1; i++) {
for (let j = 1; j < rows - 1; j++) {
board[i][j] = new Cell(0, i * w, j * w, w);
}
}
}
function modelLoaded() {
console.log('posenet ready');
}
function gotPoses(poses) {
if (poses.length > 0) {
pose = poses[0].pose;
}
}
function draw() {
background(0);
let videoWidth = windowWidth;
let videoHeight = (video.height / video.width) * videoWidth;
translate(width, 0);
scale(-1, 1);
//image(video, 0, 0, videoWidth, videoHeight);
//filter(GRAY);
//filter(BLUR)
if (pose) {
let leftWristGridX = floor((pose.leftWrist.x / video.width) * videoWidth / w);
let leftWristGridY = floor((pose.leftWrist.y / video.height) * videoHeight / w);
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
let xIndex = leftWristGridX + i;
let yIndex = leftWristGridY + j;
if (
xIndex >= 0 &&
xIndex < columns &&
yIndex >= 0 &&
yIndex < rows &&
xIndex + 1 < columns &&
xIndex - 1 >= 0 &&
yIndex - 1 >= 0 &&
yIndex + 1 < rows
) {
board[xIndex][yIndex].state = 1;
board[xIndex + 1][yIndex].state = 1;
board[xIndex - 1][yIndex].state = 1;
board[xIndex][yIndex - 1].state = 1;
board[xIndex][yIndex + 1].state = 1;
}
}
}
let rightWristGridX = floor((pose.rightWrist.x / video.width) * videoWidth / w);
let rightWristGridY = floor((pose.rightWrist.y / video.height) * videoHeight / w);
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
let xIndex = rightWristGridX + i;
let yIndex = rightWristGridY + j;
if (
xIndex >= 0 &&
xIndex < columns &&
yIndex >= 0 &&
yIndex < rows &&
xIndex + 1 < columns &&
xIndex - 1 >= 0 &&
yIndex - 1 >= 0 &&
yIndex + 1 < rows
) {
board[xIndex][yIndex].state = 1;
board[xIndex + 1][yIndex].state = 1;
board[xIndex - 1][yIndex].state = 1;
board[xIndex][yIndex - 1].state = 1;
board[xIndex][yIndex + 1].state = 1;
}
}
}
}
// Loop through the cells to update the states based on the rules
for (let x = 1; x < columns - 1; x++) {
for (let y = 1; y < rows - 1; y++) {
let neighbors = 0;
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
neighbors += board[x + i][y + j].previous;
}
}
neighbors -= board[x][y].previous;
if (board[x][y].state == 1 && neighbors > 0) {
board[x][y].state = 0;
} else if (board[x][y].state == 1 && neighbors > 2) {
board[x][y].state = 0;
} else if (board[x][y].state == 0 && neighbors == 3) {
board[x][y].state = 1;
}
}
}
for (let i = 0; i < columns; i++) {
for (let j = 0; j < rows; j++) {
board[i][j].show();
board[i][j].previous = board[i][j].state;
}
}
}
function create2DArray(columns, rows) {
let arr = new Array(columns);
for (let i = 0; i < columns; i++) {
arr[i] = new Array(rows);
for (let j = 0; j < rows; j++) {
arr[i][j] = new Cell(0, i * w, j * w, w);
}
}
return arr;
}