-
Notifications
You must be signed in to change notification settings - Fork 1
/
ml.js
155 lines (147 loc) · 7.08 KB
/
ml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//-----Version that uses ML5 instead of Tensorflow------//
let lerpPoses = []; //Array for interpolated PoseNet points
let options = { //PoseNet options
imageScaleFactor: 0.3,
outputStride: 16,
flipHorizontal: true,
minConfidence: 0.5,
maxPoseDetections: 5,
scoreThreshold: 0.5,
nmsRadius: 20,
detectionType: 'multiple',
multiplier: 0.75,
}
//---Smoothing points---//
const poseCapture = (poses,canvas) => {
//Locks play button until video loads
if(document.querySelector('.play').classList.contains('disabled')){
document.querySelector('.play').classList.remove('disabled')
}
if(poses.length > 0){
if(poses.length < lerpPoses.length){
lerpPoses = lerpPoses.slice(poses.length)
}
for (let [i,e] of poses.entries()){
if(e.pose.score > 0.25){
if(lerpPoses[i] === undefined){
lerpPoses[i] = e
}else{
for(let [j,f] of lerpPoses[i].pose.keypoints.entries()){
let xDif = e.pose.keypoints[j].position.x - f.position.x
let yDif = e.pose.keypoints[j].position.y - f.position.y
if(Math.abs(xDif) > 50 || Math.abs(yDif) > 50){
f.position.x = e.pose.keypoints[j].position.x
f.position.y = e.pose.keypoints[j].position.y
}else{
f.position.x = canvas.lerp(f.position.x,e.pose.keypoints[j].position.x,0.5)
f.position.y = canvas.lerp(f.position.y,e.pose.keypoints[j].position.y,0.5)
f.position.dx = xDif
f.position.dy = yDif
}
f.score = e.pose.keypoints[j].score
}
}
}else{
}
}
}else{
lerpPoses = []
}
}
//---For Rendering---//
const poseRender = (canvas) => {
if(lerpPoses.length > 0 && play === true){
if(!flock.textChange){
flock.assemble = true
}
flock.portals = []
//For performance, limit physics to the 1st pose
if(lerpPoses[0]){
let keypoints = lerpPoses[0].pose.keypoints
if (isMobile){
let nose = keypoints[0]
flock.portals.push(canvas.createVector(nose.position.x,nose.position.y))
canvas.ellipse(nose.position.x,nose.position.y,16,16)
}else{
let leftWrist = keypoints[10]
let rightWrist = keypoints[9]
flock.portals.push(canvas.createVector(leftWrist.position.x,leftWrist.position.y))
flock.portals.push(canvas.createVector(rightWrist.position.x,rightWrist.position.y))
canvas.image(handR, keypoints[9].position.x - handR.width/2,keypoints[9].position.y - handR.height/2,16,16);
canvas.image(handL, keypoints[10].position.x - handL.width/2,keypoints[10].position.y - handL.height/2,16,16);
}
}
}else{
flock.assemble = false
}
}
//---Class for pose skeleton---//
class Figure {
constructor(keypoints,canvas) {
this.points = keypoints
this.d = canvas.dist(keypoints[0].position.x,keypoints[0].position.y,keypoints[1].position.x,keypoints[1].position.y);
this.canvas = canvas
}
eyes(){
this.canvas.ellipse(this.points[1].position.x,this.points[1].position.y,this.d/2)
this.canvas.ellipse(this.points[2].position.x,this.points[2].position.y,this.d/2)
};
nose(){
this.canvas.ellipse(this.points[0].position.x,this.points[0].position.y,this.d)
};
ears(){
this.canvas.ellipse(this.points[3].position.x,this.points[3].position.y,this.d)
this.canvas.ellipse(this.points[4].position.x,this.points[4].position.y,this.d)
};
head(){
this.canvas.push()
this.canvas.fill(0,0,0,0)
this.canvas.stroke(118,0,245)
this.canvas.ellipse(this.points[0].position.x,this.points[0].position.y,this.d*3,this.d*4)
this.canvas.pop()
}
allPoints(){
for (let e of this.points){
this.canvas.fill(235, 58, 246)
this.canvas.ellipse(e.position.x,e.position.y,this.d)
}
}
allLines(){
this.canvas.stroke(118,0,245);
this.canvas.strokeWeight(this.d*.3)
this.canvas.line(this.points[5].position.x, this.points[5].position.y, this.points[6].position.x, this.points[6].position.y);
this.canvas.line(this.points[11].position.x, this.points[11].position.y, this.points[12].position.x, this.points[12].position.y);
this.canvas.line(this.points[6].position.x, this.points[6].position.y, this.points[12].position.x, this.points[12].position.y);
this.canvas.line(this.points[5].position.x, this.points[5].position.y, this.points[11].position.x, this.points[11].position.y);
this.canvas.line(this.points[5].position.x, this.points[5].position.y, this.points[7].position.x, this.points[7].position.y);
this.canvas.line(this.points[7].position.x, this.points[7].position.y, this.points[9].position.x, this.points[9].position.y);
this.canvas.line(this.points[6].position.x, this.points[6].position.y, this.points[8].position.x, this.points[8].position.y);
this.canvas.line(this.points[8].position.x, this.points[8].position.y, this.points[10].position.x, this.points[10].position.y);
this.canvas.line(this.points[11].position.x, this.points[11].position.y, this.points[13].position.x, this.points[13].position.y);
this.canvas.line(this.points[13].position.x, this.points[13].position.y, this.points[15].position.x, this.points[15].position.y);
this.canvas.line(this.points[12].position.x, this.points[12].position.y, this.points[14].position.x, this.points[14].position.y);
this.canvas.line(this.points[14].position.x, this.points[14].position.y, this.points[16].position.x, this.points[16].position.y);
}
torso(){
this.canvas.line(this.points[5].position.x, this.points[5].position.y, this.points[6].position.x, this.points[6].position.y);
this.canvas.line(this.points[11].position.x, this.points[11].position.y, this.points[12].position.x, this.points[12].position.y);
this.canvas.line(this.points[6].position.x, this.points[6].position.y, this.points[12].position.x, this.points[12].position.y);
this.canvas.line(this.points[5].position.x, this.points[5].position.y, this.points[11].position.x, this.points[11].position.y);
}
leftArm(){
this.canvas.line(this.points[5].position.x, this.points[5].position.y, this.points[7].position.x, this.points[7].position.y);
this.canvas.line(this.points[7].position.x, this.points[7].position.y, this.points[9].position.x, this.points[9].position.y);
}
rightArm(){
this.canvas.line(this.points[6].position.x, this.points[6].position.y, this.points[8].position.x, this.points[8].position.y);
this.canvas.line(this.points[8].position.x, this.points[8].position.y, this.points[10].position.x, this.points[10].position.y);
}
leftLeg(){
this.canvas.line(this.points[11].position.x, this.points[11].position.y, this.points[13].position.x, this.points[13].position.y);
this.canvas.line(this.points[13].position.x, this.points[13].position.y, this.points[15].position.x, this.points[15].position.y);
}
rightLeg(){
this.canvas.line(this.points[12].position.x, this.points[12].position.y, this.points[14].position.x, this.points[14].position.y);
this.canvas.line(this.points[14].position.x, this.points[14].position.y, this.points[16].position.x, this.points[16].position.y);
}
}