-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.js
171 lines (134 loc) · 3.57 KB
/
image.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
let video;
let poseNet;
let pose;
let skeleton;
let img;
let brain;
let poseLabel = "A";
let confScore = 0;
function setup() {
createCanvas(1280,720);
//createCanvas(640, 480);
img = createImg("./data/a.jpg",imageReady);
// set the image size to the size of the canvas
img.size(width, height);
img.hide();
frameRate(1); // set the frameRate to 1 since we don't need it to be running quickly in this case
let options = {
inputs: 34,
outputs: 4,
task: 'classification',
debug: true
}
brain = ml5.neuralNetwork(options);
// create an object to load model relaed data.
const modelInfo = {
model: 'models/model.json',
metadata: 'models/model_meta.json',
weights: 'models/model.weights.bin',
};
brain.load(modelInfo, brainLoaded);
}
// when the image is ready, then load up poseNet
function imageReady() {
// set some options
const options = {
minConfidence: 0.1,
inputResolution: { width, height },
};
// assign poseNet
poseNet = ml5.poseNet('single',modelLoaded, options);
// This sets up an event that listens to 'pose' events
poseNet.on("pose", function(poses) {
console.log('[INFO] Posenet >>> finding poses...');
if (poses.length > 0) {
pose = poses[0].pose;
skeleton = poses[0].skeleton;
}
});
}
function brainLoaded() {
console.log('[INFO] model ready!');
classifyPose();
}
function classifyPose() {
console.log('[INFO] Classifying...!');
if (pose) {
console.log('[INFO] Found a pose...!');
let inputs = [];
// ready the input we got from posenet.
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);
}
brain.classify(inputs, gotResult);
} else {
setTimeout(classifyPose, 100);
}
}
function gotResult(error, results) {
if(error){
console.error(error);
return;
}
else{
console.log(results);
if (results[0].confidence > 0.75) {
poseLabel = results[0].label.toUpperCase();
}
console.log(results[0].confidence);
confScore = results[0].confidence;
//classifyPose();
}
}
function gotPoses(poses) {
console.log('[INFO] Posenet >>> finding poses...');
if (poses.length > 0) {
pose = poses[0].pose;
skeleton = poses[0].skeleton;
}
}
function modelLoaded() {
console.log('[INFO] posenet loaded...');
poseNet.singlePose(img);
}
function draw() {
if (pose) {
push();
translate(img.width, 0);
scale(-1, 1);
image(img, 0, 0, img.width, img.height);
for (let i = 0; i < skeleton.length; i++) {
let a = skeleton[i][0];
let b = skeleton[i][1];
strokeWeight(2);
stroke(0);
line(a.position.x, a.position.y, b.position.x, b.position.y);
}
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);
}
pop();
fill(255, 0, 255);
noStroke();
textSize(100);
textAlign(CENTER, CENTER);
text(poseLabel, width / 2, height / 2);
fill(255, 255, 255);//white
noStroke();
textSize(30);
textAlign(RIGHT, BOTTOM);
var n = (confScore*100);
n = n.toFixed(2);
// text('c-score: '+n+'%', 1100,600);
text('c-score: '+n+'%', 512,400);
// console.log(typeof(confScore));
//noLoop(); // stop looping when the poses are estimated
}
}