-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsketch.js
204 lines (185 loc) · 4.32 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
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
let video;
let poseNet;
let pose;
let skeleton;
let brain;
let state = 'waiting';
let debugState = true;
let targetLabel;
let poseLabel = '';
const poseEnum = {'1': 'Namaskar', '2': 'Salute', '3': 'Tree', '4': 'Flying'};
function keyPressed() {
if(key.length > 1) {
return;
}
console.log(key);
if(key === 's' || key === 'S') { // Save Data
brain.saveData('posedata');
} if (key === 't' || key === 'T') { // Load dataset, Train model and Save Model
brain.loadData('posedata.json', dataReady);
} if(key === 'l' || key === 'L') { // Load Saved Model Model
loadBrain();
} else {
updateAddDataState(key);
}
}
function updateDebugState() {
console.log('updateDebugState is called ----- ')
setTimeout(() => {
debugState = false;
console.log('updateDebugState is called ----- ', debugState)
}, 4000)
}
function loadBrain() {
const modelInfo = {
model: 'model/model.json',
metadata: 'model/model_meta.json',
weights: 'model/model.weights.bin'
};
brain.load(modelInfo, brainLoaded);
updateDebugState();
}
function brainLoaded() {
console.log('Brain Loaded');
classifyPose();
}
function classifyPose() {
if(pose) {
const inputs = [];
for (let i = 0; i < pose.keypoints.length; i++) {
const x = pose.keypoints[i].position.x;
const y = pose.keypoints[i].position.y;
inputs.push(x);
inputs.push(y);
}
brain.classify(inputs, gotResult);
} else {
setTimeout(() => {
classifyPose();
}, 100)
}
}
function gotResult(error, results) {
if(error) {
}
customLog(`results -- ${JSON.stringify(results)}`);
if(results) {
const confidence = results[0].confidence;
if(confidence > 0.80) {
poseLabel = poseEnum[results[0].label];
// console.log(`Label: ${results[0].label}, Confidence : ${confidence}`);
}
}
classifyPose();
}
function dataReady() {
brain.normalizeData();
brain.train({epochs: 50}, finished);
}
function finished() {
console.log('Model trained');
brain.save();
}
function updateAddDataState(key) {
targetLabel = key;
console.log(`targetLabel : ${targetLabel}`);
setTimeout(() => {
state = 'collecting';
console.log(`state : ${state}`);
setTimeout(() => {
state = 'waiting';
console.log(`state : ${state}`);
}, 20000)
}, 3000)
}
function setup() {
const width = window.innerWidth;
const height = window.innerHeight;
createCanvas(width, window.innerHeight);
video = createCapture(VIDEO);
video.size(width, height)
video.hide();
poseNet = ml5.poseNet(video, modelLoaded);
poseNet.on('pose', gotPoses);
}
function initBrain() {
const options = {
inputs: 34,
outputs: 4,
task: 'classification',
debug: true
};
brain = ml5.neuralNetwork(options);
loadBrain();
}
function gotPoses(poses) {
//console.log(poses);
if (poses.length > 0) {
pose = poses[0].pose;
skeleton = poses[0].skeleton;
addData();
}
}
function addData() {
if(state === 'collecting') {
const inputs = [];
for (let i = 0; i < pose.keypoints.length; i++) {
const x = pose.keypoints[i].position.x;
const y = pose.keypoints[i].position.y;
inputs.push(x);
inputs.push(y);
}
const target = [targetLabel];
brain.addData(inputs, target);
}
}
function modelLoaded() {
console.log('poseNet ready');
initBrain();
}
function draw() {
image(video, 0, 0);
if (pose) {
// drawNose();
// drawPose();
// drawSkelton();
drawPoseLabel();
}
}
function drawNose() {
const eyeL = pose.leftEye;
const eyeR = pose.rightEye;
const nose = pose.nose;
const d = dist(eyeR.x, eyeR.y, eyeL.x, eyeL.y);
fill(255,0,0);
ellipse(nose.x, nose.y, d);
}
function drawPose() {
for (let i = 0; i < pose.keypoints.length; i++) {
let x = pose.keypoints[i].position.x;
let y = pose.keypoints[i].position.y;
fill(0, 255, 0);
ellipse(x, y, 16, 16);
}
}
function drawSkelton() {
for (let i = 0; i < skeleton.length; i++) {
let a = skeleton[i][0];
let b = skeleton[i][1];
strokeWeight(2);
stroke(255);
line(a.position.x, a.position.y, b.position.x, b.position.y);
}
}
function drawPoseLabel() {
fill(255, 255,0);
noStroke();
textSize(100);
textAlign(CENTER, CENTER);
text(poseLabel, width/2, height-50);
}
function customLog(message) {
if(debugState) {
console.log(message);
}
}