-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData-Col.js
More file actions
137 lines (119 loc) · 3.62 KB
/
Data-Col.js
File metadata and controls
137 lines (119 loc) · 3.62 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
//Step 1 CHange the number of files to be download in the for loop
//Step 2 Download the file as
let img;
let posenet;
let pose;
let PWClassifier; // Plank and Wallsit classifier -> neural network
let targetLabel; //change when changing the folder
let finished = false;
let TOTAL_IMAGE = 50; //Change accordingly depending on the number of data
// let folder = ["Plank", "Wallsit", "Stand"];
let folder = ["Wallsit"];
// let folder = ["pushdown", "pushup"];
async function keyPressed() {
if (key == "s") {
console.log("Saving the data");
PWClassifier.saveData();
}
}
function delay(time) {
return new Promise((resolve, reject) => {
setTimeout(resolve, time);
});
}
//Preload Neural network and Posenet
function preload() {
//nn options
let options = {
inputs: 34, //17 pairs, single pose
outputs: 3, //since the 2 labels- wallsit and plankl
task: "classification",
debug: true,
};
PWClassifier = ml5.neuralNetwork(options);
// LOAD TRAINING DATA
PWClassifier.loadData("Dataset/json2/standwallsit100.json");
}
// function dataReady() {
// console.log.out("Loaded wallsit dataset");
// }
async function setup() {
//poseNet options
for (var fi = 0; fi < folder.length; fi++) {
for (var i = 100; i < 122; i++) {
//iterate through all the images in the folder
await new Promise((next) => {
//LOADING THE IMAGE
targetLabel = folder[fi].toLowerCase();
newImg = folder[fi].toLowerCase() + "(" + i + ").jpg"; //CHANGE WHEN CHANGING THE FOLDER
path = "Dataset/" + folder[fi] + "/" + newImg; //CHANGE WHEN CHANGING THE FOLDER
// newImg = "plank(" + i + ").jpg"; //CHANGE WHEN CHANGING THE FOLDER
// path = "Dataset/Plank/" + newImg; //CHANGE WHEN CHANGING THE FOLDER
img = loadImage(path);
console.log("loaded " + path);
//posenet options
let posenetOpts = {
architecture: "ResNet50",
// imageScaleFactor: 0.3,
outputStride: 32,
// flipHorizontal: false,
minConfidence: 0.5,
maxPoseDetections: 1,
minPartConfidence: 0.5,
scoreThreshold: 0.5,
nmsRadius: 20,
detectionType: "single",
inputResolution: 256,
multiplier: 0.75,
quantBytes: 2,
};
posenet = ml5.poseNet(loaded, posenetOpts);
//GET KEYPOINT FOR THE LOADED IMAGE
posenet.on("pose", gotPoses);
async function finish() {
await delay(750);
if (finished) {
finished = false;
// console.log('Done with ' + newImg);
next();
} else {
finish(); //wait
}
}
finish();
});
}
}
console.log("Finished all the images, saving the data...");
PWClassifier.saveData();
}
function loaded() {
posenet.singlePose(img);
}
function gotPoses(results) {
if (results.length > 0) {
//Dealing with >= 1 valid pose
for (let index = 0; index < results.length; index++) {
if (results[index].pose.score > 0.2) {
pose = results[index].pose;
}
}
let inputs = [];
for (let i = 0; i < pose.keypoints.length; i++) {
// console.log(pose.keypoints[i].part);
let x = pose.keypoints[i].position.x;
let y = pose.keypoints[i].position.y;
// console.log(i + " x: " + x + " / y: " + y);
inputs.push(x);
inputs.push(y);
}
let target = [targetLabel];
console.log(inputs, target);
PWClassifier.addData(inputs, target);
console.log("Added");
finished = true;
} else {
console.log("Didn't find pose, moving next");
finished = true;
}
}