forked from silvia0619/2021HackHealth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
221 lines (181 loc) · 5.97 KB
/
script.js
File metadata and controls
221 lines (181 loc) · 5.97 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//start
// More API functions here:
// https://github.com/googlecreativelab/teachablemachine-community/tree/master/libraries/pose
// the link to your model provided by Teachable Machine export panel
const URL = "./my_model/";
let model, webcam, ctx, labelContainer, maxPredictions;
async function init() {
const modelURL = URL + "model.json";
const metadataURL = URL + "metadata.json";
// load the model and metadata
// Refer to tmImage.loadFromFiles() in the API to support files from a file picker
// Note: the pose library adds a tmPose object to your window (window.tmPose)
model = await tmPose.load(modelURL, metadataURL);
maxPredictions = model.getTotalClasses();
// Convenience function to setup a webcam
const size = 400;
const flip = true; // whether to flip the webcam
webcam = new tmPose.Webcam(size, size, flip); // width, height, flip
await webcam.setup(); // request access to the webcam
await webcam.play();
window.requestAnimationFrame(loop);
// append/get elements to the DOM
const canvas = document.getElementById("canvas");
canvas.width = size; canvas.height = size;
ctx = canvas.getContext("2d");
labelContainer = document.getElementById("label-container");
for (let i = 0; i < maxPredictions; i++) { // and class labels
labelContainer.appendChild(document.createElement("div"));
}
}
var time = 0;
async function loop(timestamp) {
webcam.update(); // update the webcam frame
await predict();
window.requestAnimationFrame(loop);
}
var classNum = 0;
var executed = false;
var started = false;
var specialflag1 = false
var specialflag2 = false
async function predict() {
// Prediction #1: run input through posenet
// estimatePose can take in an image, video or canvas html element
const { pose, posenetOutput } = await model.estimatePose(webcam.canvas);
// Prediction 2: run input through teachable machine classification model
const prediction = await model.predict(posenetOutput);
// for (let i = 0; i < maxPredictions; i++) {
// const classPrediction =
// prediction[i].className + ": " + prediction[i].probability.toFixed(2);
// labelContainer.childNodes[i].innerHTML = classPrediction;
// }
if (classNum == 6){
specialflag1 = true;
classNum = 5;
}
if (classNum == 4 && !specialflag2){
var voice = new Audio("./voice/Full body stretch will begin. Fit your full body in the webcam.mp3");
voice.play();
specialflag2 = true;
}
if(prediction[classNum].probability == 1.0 && !started){
onlyOnce();
}
// finally draw the poses
drawPose(pose);
}
function onlyOnce(){
//console.log(executed);
// if(!executed){
// executed = true;
// }
var forGrayIdd = "images/gray-" + classNum + "-1.jpg";
var forGrayId = "images/gray-" + classNum + ".jpg";
var whereTo = "pose" + classNum + "img";
if(classNum == 0 || classNum == 2 || classNum == 4){
document.getElementById(whereTo).src = forGrayIdd;
}
else{
document.getElementById(whereTo).src = forGrayId;
}
theTimer();
add();
}
function add(){
console.log("class Number", classNum);
classNum++;
//executed = false;
}
function drawPose(pose) {
if (webcam.canvas) {
ctx.drawImage(webcam.canvas, 0, 0);
// draw the keypoints and skeleton
if (pose) {
const minPartConfidence = 0.5;
tmPose.drawKeypoints(pose.keypoints, minPartConfidence, ctx);
tmPose.drawSkeleton(pose.keypoints, minPartConfidence, ctx);
}
}
}
//start & end button
function startButton(){
console.log("*******init function test");
location.replace('stretches.html');
//init();
}
function getBackToWorkButton(){
window.close();
}
//popup
$ = function(id) {
return document.getElementById(id);
}
var show = function(id) {
$(id).style.display ='block';
}
var hide = function(id) {
$(id).style.display ='none';
}
function okButton(){
hide('popup1');
//init();
}
//count down
function theTimer(){
started = true;
var theClassNum = classNum
console.log(theClassNum, "theclassNum");
console.log("how many times does the timer work?");
if(classNum == 0 || classNum == 2 || classNum == 4){
time = 20;
}
else{
time = 10;
}
var sec = "";
var timer = setInterval(function(){
sec = time;
document.getElementById("timerId").innerHTML = sec;
if(time == 11){
var forIdd = "images/" + theClassNum + "-2.jpg"
document.getElementById("poseImg").src = forIdd;
var voice = new Audio("./voice/Also stretch the other side.mp3")
voice.play();
}
if(time <= 5){
var voice = new Audio("./voice/"+ (time) + ".mp3")
voice.play();
}
time--;
if(time == -1 && classNum == 5 && specialflag1) {
location.replace("./goodjob.html");
}
if(time < 0){
console.log("NOW!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
var forIdd = "images/" + (theClassNum + 1) + "-1.jpg";
var forId = "images/" + (theClassNum + 1) + ".jpg";
if((theClassNum + 1) == 0 || (theClassNum + 1) == 2 || (theClassNum + 1) == 4){
document.getElementById("poseImg").src = forIdd;
}
else{
if(classNum == 6) {
location.replace("./goodjob.html");
}
document.getElementById("poseImg").src = forId;
}
}
if(sec<1){
clearInterval(timer);
started = false;
}
}, 1000);
}
//next button
function next(){
console.log("nnnnnnnnnnnnn");
classNum++;
//executed = false;
started = false;
time = 10;
}