-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
137 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
class Pose { | ||
constructor() { | ||
this.mappedPose = []; | ||
for(let i=0; i<17; i++) { | ||
this.mappedPose.push([0,0]); | ||
} | ||
} | ||
|
||
update(pose) { | ||
for(let i=0; i<pose.length; i++) { | ||
this.mappedPose[i] = [ this.mapX(pose[i].position.x), this.mapY(pose[i].position.y) ]; | ||
} | ||
} | ||
|
||
draw() { | ||
for(let i=0; i< this.mappedPose.length; i++) { | ||
if(i<5) { | ||
noStroke(); | ||
fill(255,255,0); | ||
rect(this.mappedPose[i][0], this.mappedPose[i][1],10, 10); | ||
} | ||
/* | ||
stroke(255,0,100); | ||
strokeWeight(2); | ||
line(this.mappedPose[5][0], this.mappedPose[5][1], this.mappedPose[6][0], this.mappedPose[6][1]); | ||
line(this.mappedPose[5][0], this.mappedPose[5][1], this.mappedPose[7][0], this.mappedPose[7][1]); | ||
line(this.mappedPose[6][0], this.mappedPose[6][1], this.mappedPose[8][0], this.mappedPose[8][1]); | ||
line(this.mappedPose[8][0], this.mappedPose[8][1], this.mappedPose[10][0], this.mappedPose[10][1]); | ||
line(this.mappedPose[7][0], this.mappedPose[7][1], this.mappedPose[9][0], this.mappedPose[9][1]); | ||
line(this.mappedPose[5][0], this.mappedPose[5][1], this.mappedPose[11][0], this.mappedPose[11][1]); | ||
line(this.mappedPose[6][0], this.mappedPose[6][1], this.mappedPose[8][0], this.mappedPose[8][1]); | ||
line(this.mappedPose[6][0], this.mappedPose[6][1], this.mappedPose[12][0], this.mappedPose[12][1]); | ||
line(this.mappedPose[11][0],this.mappedPose[11][1], this.mappedPose[12][0], this.mappedPose[12][1]); | ||
line(this.mappedPose[11][0],this.mappedPose[11][1], this.mappedPose[13][0], this.mappedPose[13][1]); | ||
line(this.mappedPose[13][0],this.mappedPose[13][1], this.mappedPose[15][0], this.mappedPose[15][1]); | ||
line(this.mappedPose[12][0],this.mappedPose[12][1], this.mappedPose[14][0], this.mappedPose[14][1]); | ||
line(this.mappedPose[14][0],this.mappedPose[14][1], this.mappedPose[16][0], this.mappedPose[16][1]); | ||
*/ | ||
|
||
noStroke(); | ||
text(i, this.mappedPose[i][0]+10,this.mappedPose[i][1]); | ||
} | ||
} | ||
|
||
mapX(x) { | ||
return map(x,0,640,(width-height*captureRatio)/2, (width-height*captureRatio) /2 + height * captureRatio); | ||
} | ||
|
||
mapY(y) { | ||
// raw html5 video capture differs from p5js capture aspects for some reason... | ||
return map(y,0,360,0 + height/10,height-height/10); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,101 @@ | ||
let capture; | ||
const captureRatio = 1.333; | ||
let cPoses = []; | ||
let cPoseNum = 0; | ||
let pPoseNum = 0; | ||
|
||
var VerletPhysics2D = toxi.physics2d.VerletPhysics2D, | ||
VerletParticle2D = toxi.physics2d.VerletParticle2D, | ||
AttractionBehavior = toxi.physics2d.behaviors.AttractionBehavior, | ||
GravityBehavior = toxi.physics2d.behaviors.GravityBehavior, | ||
Vec2D = toxi.geom.Vec2D, | ||
Rect = toxi.geom.Rect; | ||
|
||
let NUM_PARTICLES = 10; | ||
|
||
let physics; | ||
let mouseAttractor; | ||
let mousePos; | ||
|
||
let headAttractor; | ||
let headPos; | ||
|
||
function setup() { | ||
var canvas = createCanvas(windowWidth,windowHeight); | ||
canvas.position(0,0); | ||
canvas.id("mCanvas"); | ||
capture = createCapture(VIDEO); | ||
capture.hide(); | ||
|
||
physics = new VerletPhysics2D(); | ||
physics.setDrag(0.05); | ||
physics.setWorldBounds(new Rect((width-height*captureRatio)/2, 0, height * captureRatio, height)); | ||
physics.addBehavior(new GravityBehavior(new Vec2D(0, 0.15))); | ||
|
||
headPos = new Vec2D(width/2,height/2); | ||
headAttractor = new AttractionBehavior(headPos, 500, 0.9); | ||
physics.addBehavior(headAttractor); | ||
} | ||
|
||
function addParticle() { | ||
let randLoc = Vec2D.randomVector().scale(5).addSelf(width / 2, 0); | ||
let p = new VerletParticle2D(randLoc); | ||
physics.addParticle(p); | ||
physics.addBehavior(new AttractionBehavior(p, 20, -1.2, 0.01)); | ||
} | ||
|
||
function draw() { | ||
background(0); | ||
push(); | ||
tint(255,255); | ||
tint(255,50); | ||
scale(-1,1); | ||
translate(-width + ((width-height*captureRatio)/2),0); | ||
image(capture,0,0,height*captureRatio,height); | ||
pop(); | ||
|
||
// recreate pose array if there is a change in number of poses | ||
cPoseNum = mPoses.length; | ||
if(cPoseNum != pPoseNum) { | ||
cPoses = []; | ||
for(let i=0; i<cPoseNum; i++) { | ||
cPoses.push(new Pose(mPoses[i])); | ||
} | ||
} | ||
|
||
// update each pose | ||
for(let i=0; i<cPoseNum; i++) { | ||
cPoses = []; | ||
for(let i=0; i<mPoses.length; i++) { | ||
cPoses.push(new Pose(mPoses[i])); | ||
for(let j=0;j<mPoses[i].keypoints.length;j++) { | ||
if(mPoses[i].score>0.2 && mPoses[i].score < 0.5) { | ||
cPoses[i].update(mPoses[i].keypoints); | ||
} | ||
} | ||
} | ||
// draw each pose | ||
for(let p of cPoses) { | ||
p.draw(); | ||
} | ||
|
||
// set previous pose numbers to check if there is change | ||
pPoseNum = cPoseNum; | ||
} | ||
if(cPoses.length>0) headPos.set(cPoses[0].mappedPose[0][0], cPoses[0].mappedPose[0][1]-300); | ||
|
||
class Pose { | ||
constructor() { | ||
this.mappedPose = []; | ||
} | ||
console.log(headPos); | ||
|
||
update(pose) { | ||
this.mappedPose = []; | ||
for(let i=0; i<pose.length; i++) { | ||
this.mappedPose.push( [ this.mapX(pose[i].position.x), this.mapY(pose[i].position.y) ] ); | ||
} | ||
} | ||
if (physics.particles.length < NUM_PARTICLES) { | ||
addParticle(); | ||
} | ||
physics.update(); | ||
|
||
for (let i=0;i<physics.particles.length;i++) { | ||
let p = physics.particles[i]; | ||
fill(255); | ||
rect(p.x, p.y, 10, 10); | ||
} | ||
|
||
fill(255); | ||
text(frameRate(), 30, 30); | ||
} | ||
|
||
draw() { | ||
for(let i=0; i< this.mappedPose.length; i++) { | ||
if(i<5) { | ||
noStroke(); | ||
fill(255,255,0); | ||
ellipse(this.mappedPose[i][0], this.mappedPose[i][1],10, 10); | ||
} | ||
stroke(255,0,100); | ||
strokeWeight(2); | ||
line(this.mappedPose[5][0], this.mappedPose[5][1], this.mappedPose[6][0], this.mappedPose[6][1]); | ||
line(this.mappedPose[5][0], this.mappedPose[5][1], this.mappedPose[7][0], this.mappedPose[7][1]); | ||
line(this.mappedPose[6][0], this.mappedPose[6][1], this.mappedPose[8][0], this.mappedPose[8][1]); | ||
line(this.mappedPose[8][0], this.mappedPose[8][1], this.mappedPose[10][0], this.mappedPose[10][1]); | ||
line(this.mappedPose[7][0], this.mappedPose[7][1], this.mappedPose[9][0], this.mappedPose[9][1]); | ||
line(this.mappedPose[5][0], this.mappedPose[5][1], this.mappedPose[11][0], this.mappedPose[11][1]); | ||
line(this.mappedPose[6][0], this.mappedPose[6][1], this.mappedPose[8][0], this.mappedPose[8][1]); | ||
line(this.mappedPose[6][0], this.mappedPose[6][1], this.mappedPose[12][0], this.mappedPose[12][1]); | ||
line(this.mappedPose[11][0],this.mappedPose[11][1], this.mappedPose[12][0], this.mappedPose[12][1]); | ||
line(this.mappedPose[11][0],this.mappedPose[11][1], this.mappedPose[13][0], this.mappedPose[13][1]); | ||
line(this.mappedPose[13][0],this.mappedPose[13][1], this.mappedPose[15][0], this.mappedPose[15][1]); | ||
line(this.mappedPose[12][0],this.mappedPose[12][1], this.mappedPose[14][0], this.mappedPose[14][1]); | ||
line(this.mappedPose[14][0],this.mappedPose[14][1], this.mappedPose[16][0], this.mappedPose[16][1]); | ||
} | ||
} | ||
function mousePressed() { | ||
addParticle(); | ||
mousePos = new Vec2D(mouseX, mouseY); | ||
mouseAttractor = new AttractionBehavior(mousePos, 250, 0.9); | ||
physics.addBehavior(mouseAttractor); | ||
} | ||
|
||
mapX(x) { | ||
return map(x,0,640,(width-height*captureRatio)/2, (width-height*captureRatio) /2 + height * captureRatio); | ||
} | ||
function mouseDragged() { | ||
mousePos.set(mouseX, mouseY); | ||
} | ||
|
||
mapY(y) { | ||
// raw html5 video capture differs from p5js capture aspects... | ||
return map(y,0,360,0 + height/10,height-height/10); | ||
} | ||
function mouseReleased() { | ||
physics.removeBehavior(mouseAttractor); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.