-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
165 lines (145 loc) · 3.22 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
let capture;
let loadedCamera;
let confidence = 0.0;
let yOff = 0;
function setup() {
createCanvas(windowWidth, windowHeight);
captureWebcam();
angleMode(DEGREES);
startPoseNet(capture);
rectMode(CENTER);
}
function draw() {
blendMode(BLEND);
yOff = (height - capture.height) / 2;
clear();
background(255);
// display video
image(capture, 0, yOff);
fill(255, 245);
rect(width / 2, height / 2, width, height);
blendMode(MULTIPLY);
noStroke();
fill("red");
// keypoints.forEach((keypoint) => {
// circle(keypoint.position.x, keypoint.position.y, 10);
// });
fill(255, 255, 0);
let headAngle = getAngle(
person.rightEye.x,
person.rightEye.y,
person.leftEye.x,
person.leftEye.y
);
push();
translate(person.nose.x, person.nose.y + yOff);
rotate(headAngle);
rect(
0,
0,
dist(
person.rightWrist.x,
person.rightWrist.y,
person.leftWrist.x,
person.leftWrist.y
)
);
pop();
makeShape(
person.rightShoulder,
person.rightWrist,
person.rightWrist,
color(0, 255, 255)
);
makeShape(
person.leftShoulder,
person.leftWrist,
person.leftWrist,
color(255, 0, 255)
);
makeShape(
person.rightShoulder,
person.rightElbow,
person.rightElbow,
color(0, 255, 255),
"circle"
);
makeShape(
person.leftShoulder,
person.leftElbow,
person.leftElbow,
color(255, 0, 255),
"circle"
);
makeShape(
person.rightKnee,
person.rightHip,
person.rightKnee,
color(0, 255, 255),
"rect"
);
makeShape(
person.leftKnee,
person.leftHip,
person.leftKnee,
color(255, 0, 255),
"rect"
);
}
function makeShape(anglePart1, anglePart2, anchor, colour, shape = "rect", conf = confidence) {
angleMode(CENTER)
// get the angle
let angle = getAngle(anglePart1.x, anglePart1.y, anglePart2.x, anglePart2.y);
let partSize = dist(anglePart1.x, anglePart1.y, anglePart2.x, anglePart2.y);
if (
anglePart1.confidence > conf &&
anglePart2.confidence > conf &&
anchor.confidence > conf
) {
fill(colour);
push();
translate(anchor.x, anchor.y + yOff);
rotate(angle);
if(shape === "rect") {
rect(0, 0, partSize);
} else if (shape === "circle") {
circle(0, 0, partSize);
}
pop();
}
}
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();
}
);
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();
}