-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataCollect.js
112 lines (94 loc) · 2.63 KB
/
dataCollect.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
let video;
let poseNet;
let pose;
let skeleton;
let brain;
let state ='waiting';
let targetLabel;
function setup(){
createCanvas(640,480);
video = createCapture(VIDEO);
video.hide();
poseNet = ml5.poseNet(video,onModelLoad);
poseNet.on('pose',gotPose);
let options = {
inputs:34,
outputs:4,
task:'classification',
debug:true
}
brain = ml5.neuralNetwork(options);
}
function gotPose(poses){
//console.log(poses);
if(poses.length >0){
pose = poses[0].pose;
skeleton = poses[0].skeleton;
if(state == 'collecting'){
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);
}
let target = [targetLabel];
brain.addData(inputs,target);
}
}
}
function onModelLoad(){
console.log('posenet model ready');
}
function keyPressed(){
if(key == 's'){
console.log('saving data...');
brain.saveData();//creates a json file of model trained.
}
else{
targetLabel = key;
console.log(targetLabel);
setTimeout(function() {
console.log('collecting......');
state = 'collecting';
setTimeout(function() {
console.log('stopped collecting');
state = 'waiting';
},15000);// collect for 15 sec
},10000);// get ready in 10 sec.
}
}
function draw(){
// image(video,0,0);
push();
translate(video.width, 0);
scale(-1, 1);
image(video, 0, 0, video.width, video.height);
if(pose){
/*
// mark nose
fill(255,0,0);
ellipse(pose.nose.x,pose.nose.y,25);
// mark wrists
fill(0,255,0);
ellipse(pose.leftWrist.x,pose.leftWrist.y,25);
ellipse(pose.rightWrist.x,pose.rightWrist.y,25);
*/
// mark all keypoints
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);
stroke(255);
ellipse(x, y, 16, 16);
}
// Draw Skeleton lines
for(let i = 0; i <skeleton.length;i++){
let a = skeleton[i][0];
let b = skeleton[i][1];
strokeWeight(2);
stroke(0);//white 255, black-0
line(a.position.x,a.position.y,b.position.x,b.position.y);
}
}
}