forked from cris-maillo/yogAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn.js
189 lines (169 loc) · 5.12 KB
/
learn.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
189
let video;
let poseNet;
let pose;
let skeleton;
let thirtysecs;
let posesArray = ['Mountain', 'Tree', 'Downward Dog', 'Warrior I', 'Warrior II', 'Chair'];
var imgArray = new Array();
var poseImage;
let yogi;
let poseLabel;
var targetLabel;
var errorCounter;
var iterationCounter;
var poseCounter;
var target;
var timeLeft;
function setup() {
var canvas = createCanvas(640, 480);
canvas.position(130, 210);
video = createCapture(VIDEO);
video.hide();
poseNet = ml5.poseNet(video, modelLoaded);
poseNet.on('pose', gotPoses);
imgArray[0] = new Image();
imgArray[0].src = 'imgs/mountain.svg';
imgArray[1] = new Image();
imgArray[1].src = 'imgs/tree.svg';
imgArray[2] = new Image();
imgArray[2].src = 'imgs/dog.svg';
imgArray[3] = new Image();
imgArray[3].src = 'imgs/warrior1.svg';
imgArray[4] = new Image();
imgArray[4].src = 'imgs/warrior2.svg';
imgArray[5] = new Image();
imgArray[5].src = 'imgs/chair.svg';
poseCounter = 0;
targetLabel = 1;
target = posesArray[poseCounter];
document.getElementById("poseName").textContent = target;
timeLeft = 10;
document.getElementById("time").textContent = "00:" + timeLeft;
errorCounter = 0;
iterationCounter = 0;
document.getElementById("poseImg").src = imgArray[poseCounter].src;
let options = {
inputs: 34,
outputs: 6,
task: 'classification',
debug: true
}
yogi = ml5.neuralNetwork(options);
const modelInfo = {
model: 'modelv2/model2.json',
metadata: 'modelv2/model_meta2.json',
weights: 'modelv2/model.weights2.bin',
};
yogi.load(modelInfo, yogiLoaded);
}
function yogiLoaded(){
console.log("Model ready!");
classifyPose();
}
function classifyPose(){
if (pose) {
let inputs = [];
for (let i = 0; i < pose.keypoints.length; i++) {
let x = pose.keypoints[i].position.x;
let y = pose.keypoints[i].position.y;
inputs.push(x);
inputs.push(y);
}
yogi.classify(inputs, gotResult);
} else {
console.log("Pose not found");
setTimeout(classifyPose, 100);
}
}
function gotResult(error, results) {
document.getElementById("welldone").textContent = "";
document.getElementById("sparkles").style.display = "none";
if (results[0].confidence > 0.70) {
console.log("Confidence");
if (results[0].label == targetLabel.toString()){
console.log(targetLabel);
iterationCounter = iterationCounter + 1;
console.log(iterationCounter)
if (iterationCounter == 10) {
console.log("30!")
iterationCounter = 0;
nextPose();}
else{
console.log("doin this")
timeLeft = timeLeft - 1;
if (timeLeft < 10){
document.getElementById("time").textContent = "00:0" + timeLeft;
}else{
document.getElementById("time").textContent = "00:" + timeLeft;}
setTimeout(classifyPose, 1000);}}
else{
errorCounter = errorCounter + 1;
console.log("error");
if (errorCounter >= 4){
console.log("four errors");
iterationCounter = 0;
timeLeft = 10;
if (timeLeft < 10){
document.getElementById("time").textContent = "00:0" + timeLeft;
}else{
document.getElementById("time").textContent = "00:" + timeLeft;}
errorCounter = 0;
setTimeout(classifyPose, 100);
}else{
setTimeout(classifyPose, 100);
}}}
else{
console.log("whatwe really dont want")
setTimeout(classifyPose, 100);
}}
function gotPoses(poses) {
if (poses.length > 0) {
pose = poses[0].pose;
skeleton = poses[0].skeleton;
}
}
function modelLoaded() {
document.getElementById("rectangle").style.display = "none";
console.log('poseNet ready');
}
function draw() {
push();
translate(video.width, 0);
scale(-1,1);
image(video, 0, 0, video.width, video.height);
if (pose) {
for (let i = 0; i < skeleton.length; i++) {
let a = skeleton[i][0];
let b = skeleton[i][1];
strokeWeight(8);
stroke(244, 194, 194);
line(a.position.x, a.position.y, b.position.x, b.position.y);
}
}
pop();
}
function nextPose(){
if (poseCounter >= 5) {
console.log("Well done, you have learnt all poses!");
document.getElementById("finish").textContent = "Amazing!";
document.getElementById("welldone").textContent = "All poses done.";
document.getElementById("sparkles").style.display = 'block';
}else{
console.log("Well done, you all poses!");
//var stars = document.getElementById("starsid");
//stars.classList.add("stars.animated");
errorCounter = 0;
iterationCounter = 0;
poseCounter = poseCounter + 1;
targetLabel = poseCounter + 1;
console.log("next pose target label" + targetLabel)
target = posesArray[poseCounter];
document.getElementById("poseName").textContent = target;
document.getElementById("welldone").textContent = "Well done, next pose!";
document.getElementById("sparkles").style.display = 'block';
document.getElementById("poseImg").src = imgArray[poseCounter].src;
console.log("classifying again");
timeLeft = 10;
document.getElementById("time").textContent = "00:" + timeLeft;
setTimeout(classifyPose, 4000)}
}