-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
178 lines (157 loc) · 3.75 KB
/
sketch.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// HOW TO USE
// predictWebcam(video) will start predicting landmarks
// pass a video MediaElement using createCapture
// make sure to call predictWebcam as a callback to createCapture
// this ensures the video is ready
// parts index:
// https://developers.google.com/mediapipe/solutions/vision/pose_landmarker/index
let capture;
let loadedCamera;
let confidence = 0.0;
let yOff = 0;
let defaultWeight = 270;
let lerpRate = 0.4;
let madeClone = false;
let lerpLandmarks;
function setup() {
createCanvas(windowWidth, windowHeight);
captureWebcam();
}
function draw() {
background(255);
// image(capture, 0, 0);
if (landmarks.length > 0) {
if (!madeClone) {
lerpLandmarks = JSON.parse(JSON.stringify(landmarks));
madeClone = true;
}
landmarks.forEach((person, index) => {
makeBezier({
index: index,
start: 8,
cont1: 5,
cont2: 2,
end: 7,
});
makeBezier({
index: index,
start: 8,
cont1: 12,
cont2: 14,
end: 16,
});
makeBezier({
index: index,
start: 7,
cont1: 11,
cont2: 13,
end: 15,
});
makeBezier({
index: index,
start: 16,
cont1: 24,
cont2: 26,
end: 28,
});
makeBezier({
index: index,
start: 15,
cont1: 23,
cont2: 25,
end: 27,
});
makeBezier({
index: index,
start: 27,
cont1: 25,
cont2: 26,
end: 28,
});
});
}
}
function makeBezier({
index,
start,
cont1,
cont2,
end,
fillColour,
strokeColour = color(0),
weight = 270,
}) {
if (fillColour !== undefined) {
fill(fillColour);
} else {
noFill();
}
if (strokeColour === null) {
noStroke();
} else {
stroke(strokeColour);
}
strokeWeight(weight);
let p = landmarks[index];
let l = lerpLandmarks[index];
// sometimes we don't have a person for a bit, if not then return
if (!l || !p) return;
l[start].x = simpLerp(l[start].x, p[start].x, lerpRate);
l[start].y = simpLerp(l[start].y, p[start].y, lerpRate);
l[cont1].x = simpLerp(l[cont1].x, p[cont1].x, lerpRate);
l[cont1].y = simpLerp(l[cont1].y, p[cont1].y, lerpRate);
l[cont2].x = simpLerp(l[cont2].x, p[cont2].x, lerpRate);
l[cont2].y = simpLerp(l[cont2].y, p[cont2].y, lerpRate);
l[end].x = simpLerp(l[end].x, p[end].x, lerpRate);
l[end].y = simpLerp(l[end].y, p[end].y, lerpRate);
bezier(
l[start].x * width,
l[start].y * height + yOff,
l[cont1].x * width,
l[cont1].y * height + yOff,
l[cont2].x * width,
l[cont2].y * height + yOff,
l[end].x * width,
l[end].y * height + yOff
);
// console.log(landmarks[0][start].x);
}
function captureWebcam() {
capture = createCapture(
{
audio: false,
video: {
facingMode: "user",
},
},
function (e) {
captureEvent = e;
// do things when video ready
// until then, the video element will have no dimensions, or default 640x480
setCameraDimensions();
predictWebcam(capture);
}
);
capture.elt.setAttribute("playsinline", "");
capture.hide();
}
function setCameraDimensions() {
loadedCamera = captureEvent.getTracks()[0].getSettings();
// console.log("cameraDimensions", loadedCamera);
if (capture.width > capture.height) {
capture.size(width, (capture.height / capture.width) * width);
} else {
capture.size((capture.width / capture.height) * height, height);
}
// console.log(capture);
}
function getAngle(v0x, v0y, v1x, v1y) {
return atan2(v1y - v0y, v1x - v0x);
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
setCameraDimensions();
}
function simpLerp(a, b, rate) {
return a + rate * (b - a);
}