-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclassify.js
188 lines (157 loc) · 4.55 KB
/
classify.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
179
180
181
182
183
184
185
186
187
188
let video;
let pose;
let skeleton;
let pose_names = ["MOUNTAIN", "GODDESS", "GARLAND", "PLANK"];
let pose_images = {"MOUNTAIN" :"image1.png", "GODDESS": "image2.jpeg", "GARLAND": "image3.jpg", "PLANK": "image4.jpg"};
// let pose_names = ["START"];
let posesDropdown;
let selected_pose;
let poseLabel;
let poseNet;
let knn;
let count = {correct: 0, total: 0}; // [incorrect, got result called]
const DATA_PATH = "./data.json";
// let knee, hip, ankle, kneeFlexion, dorsiflexion, hipFlexion, shoulder, anKnee, sHip, trunkLean;
// Loading the data before
function setup() {
let cv = createCanvas(640, 500);
cv.parent("imager");
video = createCapture(VIDEO);
video.size(640, 480);
video.hide();
// create options of poses in dropdown
posesDropdown = document.getElementById("poses_dropdown");
// pose_names.forEach((name) => {
// posesDropdown.options[posesDropdown.options.length] = new Option(
// name,
// name
// );
// });
// setup posenet model
// posenet requires input image/video and a callback function
poseNet = ml5.poseNet(video, modelLoaded);
// on() function is called on seeing a pose.
// the object of bodypoints is given to callback function getPoses
poseNet.on("pose", getPoses);
// getPoses initializes pose and skeleton variables
knn = ml5.KNNClassifier();
// load the saved model
}
function image_maker(){
let posesDropdownVal = document.getElementById("poses_dropdown").value;
// console.log(typeof(posesDropdownVal))
img_path = pose_images[posesDropdownVal];
// console.log(pose_val)
document.getElementById("pose_img").src=img_path;
}
function networkLoaded() {
console.log("KNN loaded!");
classifyPose();
}
// call back function
function modelLoaded() {
console.log("PoseNet model loaded!");
knn.load(DATA_PATH, networkLoaded);
}
function classifyPose() {
// classify pose
if (pose) {
const poseArray = pose.keypoints.map((p) => [
p.score,
p.position.x,
p.position.y,
]);
setInterval(() => {
knn.classify(poseArray, gotResult);
}, 3000);
} else {
setTimeout(classifyPose, 1000);
}
}
function gotResult(error, results) {
if (results) {
// console.log(results);
count.total++;
poseLabel = pose_names[parseInt(results.label)];
if (posesDropdown.value == poseLabel){
count.correct++;
}
classifyPose();
}
}
function getPoses(poses) {
if (poses.length > 0) {
pose = poses[0].pose;
skeleton = poses[0].skeleton;
}
}
function drawKeypoints() {
if (pose) {
for (let j = 0; j < pose.keypoints.length; j++) {
// A keypoint is an object describing a body part (like rightArm or leftShoulder)
let keypoint = pose.keypoints[j];
// Only draw an ellipse is the pose probability is bigger than 0.2
if (keypoint.score > 0.2) {
fill(255, 0, 0);
noStroke();
ellipse(keypoint.position.x, keypoint.position.y, 10, 10);
}
}
}
}
// A function to draw the skeletons
function drawSkeleton() {
// Loop through all the skeletons detected
// let skeleton = poses[i].skeleton;
// For every skeleton, loop through all body connections
if (pose) {
for (let j = 0; j < skeleton.length; j++) {
let partA = skeleton[j][0];
let partB = skeleton[j][1];
stroke(255, 0, 0);
line(
partA.position.x,
partA.position.y,
partB.position.x,
partB.position.y
);
}
}
}
function draw() {
if (selected_pose != "none") {
image(video, 0, 0, width, height);
// We can call both functions to draw all keypoints and the skeletons
drawKeypoints();
drawSkeleton();
fill(0, 255, 0);
textSize(30);
//text(classificationResult, width/2, height/2);
if (posesDropdown.value != poseLabel)
{
text("INCORRECT POSE", width / 4, height - 10);
} else{
if (poseLabel == "MOUNTAIN") {
text("MOUNTAIN POSE", width / 4, height - 10);
} else if (poseLabel == "GODDESS") {
text("GODDESS POSE", width / 4, height - 10);
} else if (poseLabel == "GARLAND") {
text("GARLAND POSE", width / 4, height - 10);
} else if (poseLabel == "PLANK") {
text("PLANK POSE", width / 4, height - 10);
}
}
// else if (poseLabel == "COBRA") {
// text("COBRA", width/2, height/2);
// }
}
selected_pose = posesDropdown.value;
}
function end() {
// document.location.href = "http://localhost:5501";
console.log(count);
console.log("Accuracy: ",count.correct/count.total)*100;
count = { correct: 0, total: 0 };
video.stop();
video.remove();
}