-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
135 lines (97 loc) · 3.26 KB
/
main.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
let video;
let poseNet;
let poses = [];
let skeletons = [];
let frames = 0;
let myMovement = 0;
let myOrientation = 0;
let calibrationTime = 300;
let totalTime = 1000; //totalTime = exerciseTime - calibrationTime
var numReps = 0;
let idx = 0;
var listOfRepInfo = [];
//vars for determining form
var numRedFrames = 0;
var repRedCounts = [];
var redCountsIdx = 0;
//array of Booleans for each frame in report
//store the formdata
var FinalSetFormData = [];
var COLOR = Object.freeze({"BLUE":1, "GREEN":2, "YELLOW":3, "RED":4});
let currentColor = COLOR.BLUE;
function setup() {
var cnv = createCanvas(640, 480);
cnv.position((windowWidth - width) / 2, (windowHeight - height) / 2);
video = createCapture(VIDEO);
video.size(width, height);
// Create a new poseNet method with a single detection
poseNet = ml5.poseNet(video, modelReady);
// This sets up an event that fills the global variable "poses"
// with an array every time new poses are detected
poseNet.on('pose', function (results) {
poses = results;
});
// Hide the video element, and just show the canvas
video.hide();
}
function modelReady() {
select('#status').html('Model Loaded');
}
function draw() {
image(video, 0, 0, width, height);
// We can call both functions to draw all keypoints and the skeletons
// printKeypoints();
// console.log(model_getPoseData());
// console.log(determineMovement());
// console.log(model_getPartCoordinate("nose", minConfidence));
drawKeypoints(currentColor);
drawSkeleton(currentColor);
if (frames < calibrationTime) {
//initial calibration
if (bodyPosIsValid()) {
//determine the movement & orientation
if (determineMovement() != undefined && determineOrientation() != undefined) {
myMovement = myMovement + determineMovement();
myOrientation = myOrientation + determineOrientation();
frames++;
}
}
} else if (frames == calibrationTime) {
//determine exercise and orientation
myMovement = Math.round(0.0 + myMovement / calibrationTime);
myOrientation = Math.round(0.0 + myOrientation / calibrationTime);
changeTextLive(myMovement);
frames++;
} else if (frames < totalTime) {
//analyze exercise for (totalTime - calibrationTime) microseconds
let formQuality = getFormQuality(myMovement, myOrientation, 260, 35);
if (formQuality != null) {
listOfRepInfo[idx] = formQuality;
currentColor = formQuality[1];
//if red, increment the reds
if (currentColor == COLOR.RED) {
numRedFrames++;
}
// console.log(" COLOR" + currentColor)
// console.log(" listOfRepInfo[idx - 2] " + currentColor)
if(currentColor == COLOR.BLUE && idx > 1 && listOfRepInfo[idx - 2][1] != COLOR.BLUE){
repRedCounts[redCountsIdx] = numRedFrames;
redCountsIdx++;
numReps++;
console.log("Reps you've done: " + numReps);
console.log("idx" + idx);
numRedFrames = 0;
}
//
// if (idx > 0 && currentColor == COLOR.BLUE && listOfRepInfo[idx - 1][1] != COLOR.BLUE){
// console.log("REPPPPP!PPPPP!P!PP!P!P!P!P!");
// numReps++;
// }
idx++;
frames++;
}
} else {
currentColor = COLOR.BLUE;
window.open('results.html')
}
}