-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
164 lines (142 loc) · 3.64 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
/**
* Todo when p5 updates:
* change canvas size
* change video.size() with
* video.size(960, 720);
*
*/
let video;
let poseNet;
let pose;
let target;
let loaded = false;
let started = false;
let calibrated = false;
let difficulty = "EASY";
const loadingDiv = document.getElementById("loading");
const startDiv = document.getElementById("start");
const timeDiv = document.getElementById("time");
const infoDiv = document.getElementById("info");
const hideElement = (element) => {
if (!element.classList.contains("hide")) {
element.classList.toggle("hide");
}
};
const showElement = (element) => {
if (element.classList.contains("hide")) {
element.classList.toggle("hide");
}
};
function setup() {
let cnv = createCanvas(640, 480);
cnv.parent("canvasContainer");
video = createCapture(VIDEO);
video.hide();
poseNet = ml5.poseNet(video, modelLoaded);
poseNet.on("pose", getPose);
}
const modelLoaded = () => {
loaded = true;
hideElement(loadingDiv);
showElement(startDiv);
};
const getPose = (poses) => {
if (poses.length > 0) {
pose = poses[0].pose;
}
};
const drawPosePoints = (pose, p) => {
for (const point of pose.keypoints) {
if (partSet.has(point.part)) {
const { x, y } = point.position;
if (p) {
p.strokeWeight(0);
p.fill(255, 0, 0);
p.ellipse(x, y, 8);
} else {
strokeWeight(0);
fill(255, 0, 0);
ellipse(x, y, 8);
}
}
}
};
function draw() {
// move image by the width of image to the right
// then scale it by -1 in the x-axis to flip the image
// draw video capture feed as image inside p5 canvas
translate(video.width, 0);
scale(-1, 1);
const frame = video.get();
image(frame, 0, 0);
if (loaded && started) {
target.draw();
}
if (pose && !calibrated) {
drawPosePoints(pose);
}
}
const start = (diff) => {
difficulty = diff;
started = true;
target = new TargetPose("TPOSE", starterPose, difficulty);
hideElement(startDiv);
showElement(infoDiv);
infoDiv.innerText = "Match the pose to start!";
calibrate();
};
const calibrate = () => {
showElement(timeDiv);
let onTargetTime = 4;
const interval = setInterval(() => {
const score = target.score(pose);
if (score === 110) {
onTargetTime -= 1;
hideElement(infoDiv);
timeDiv.innerText = onTargetTime;
} else {
onTargetTime = 4;
showElement(infoDiv);
timeDiv.innerText = "";
}
if (onTargetTime <= 0) {
clearInterval(interval);
calibrated = true;
game();
}
}, 1000);
};
const makeNewTarget = (targets, index) => {
const targetData = targets[index];
target = new TargetPose(targetData.name, targetData.points, difficulty);
};
const game = () => {
let targets = shuffle(targetList);
let totalScore = 0;
let currentIndex = 0;
makeNewTarget(targets, currentIndex);
let timeLeft = 4;
timeDiv.innerText = "MATCH THIS!";
const interval = setInterval(() => {
timeLeft -= 1;
timeDiv.innerText = timeLeft;
if (timeLeft <= 0) {
const targetScore = target.score(pose);
totalScore += targetScore;
snaps.push(new Snapshot(currentIndex, video.get(), target, pose, targetScore));
currentIndex++;
if (currentIndex >= targets.length) {
clearInterval(interval);
hideElement(timeDiv);
showElement(infoDiv);
infoDiv.innerText = `Your score was ${totalScore}!`;
snaps.forEach((snap) => snap.show());
showElement(document.getElementById("refresh"));
} else {
makeNewTarget(targets, currentIndex);
timeLeft = 4;
timeDiv.innerText = "MATCH THIS!";
}
}
}, 1000);
};