Skip to content

Commit 97beb35

Browse files
Human tracking haar detection algorithm
1 parent 1f126ea commit 97beb35

1 file changed

Lines changed: 115 additions & 36 deletions

File tree

src/tracker/human/human.js

Lines changed: 115 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,73 +7,152 @@
77
data: {},
88

99
defaults: {
10+
blockSize: 20,
11+
12+
blockJump: 50,
13+
14+
blockScale: 1.25,
15+
1016
data: 'frontal_face'
1117
},
1218

13-
analisa_: function(stage, gray, i, j, w, h) {
19+
evalStage_: function(stage, integralImage, integralImageSquare, i, j, blockSize) {
1420
var instance = this,
21+
defaults = instance.defaults,
1522
stageIndex = stage[0],
1623
stageThreshold = stage[1],
1724
tree = stage[2],
1825
treeLen = tree.length,
19-
t;
26+
t,
27+
28+
inverseArea = 1/(blockSize*blockSize),
29+
scale = blockSize/defaults.blockSize,
30+
31+
stageSum = 0;
2032

2133
for (t = 0; t < treeLen; t++) {
2234
var node = tree[t],
23-
x1 = node[0],
24-
y1 = node[1],
25-
w1 = node[2],
26-
h1 = node[3],
27-
p1 = node[4],
28-
29-
x2 = node[5],
30-
y2 = node[6],
31-
w2 = node[7],
32-
h2 = node[8],
33-
p2 = node[9],
3435

3536
nodeThreshold = node[10],
3637
left = node[11],
37-
right = node[12];
38-
39-
var a = [];
38+
right = node[12],
39+
40+
total,
41+
totalSquare,
42+
mean,
43+
variance,
44+
45+
wb1 = i*blockSize + j,
46+
wb2 = i*blockSize + (j + blockSize),
47+
wb3 = (i + blockSize)*blockSize + j,
48+
wb4 = (i + blockSize)*blockSize + (j + blockSize),
49+
50+
rectsSum = 0,
51+
rectsLen = (node.length - 3)/5,
52+
r,
53+
x, y, width, height, weight, w1, w2, w3, w4;
54+
55+
total = integralImage[wb1] - integralImage[wb2] - integralImage[wb3] + integralImage[wb4];
56+
totalSquare = integralImageSquare[wb1] - integralImageSquare[wb2] - integralImageSquare[wb3] + integralImageSquare[wb4];
57+
mean = total*inverseArea;
58+
variance = totalSquare*inverseArea - mean*mean;
59+
60+
if (variance > 1) {
61+
variance = Math.sqrt(variance);
62+
}
63+
else {
64+
variance = 1;
65+
}
66+
67+
for (r = 0; r < rectsLen; r++) {
68+
x = j + node[r*5];
69+
y = i + node[r*5 + 1];
70+
width = ~~(node[r*5 + 2]*scale);
71+
height = ~~(node[r*5 + 3]*scale);
72+
weight = node[r*5 + 4];
73+
74+
w1 = y*width + x;
75+
w2 = y*width + (x + width);
76+
w3 = (y + height)*width + x;
77+
w4 = (y + height)*width + (x + width);
78+
79+
rectsSum = (integralImage[w1] - integralImage[w2] - integralImage[w3] + integralImage[w4])*weight*inverseArea;
80+
}
81+
82+
if (rectsSum < nodeThreshold*variance) {
83+
stageSum += left;
84+
}
85+
else {
86+
stageSum += right;
87+
}
4088
}
89+
90+
return (stageSum > stageThreshold);
4191
},
4292

4393
track: function(trackerGroup, video) {
4494
var instance = this,
45-
defaults = instance.defaults,
4695
config = trackerGroup[0],
47-
height = video.canvas.get('height'),
48-
width = video.canvas.get('width'),
49-
gray = new Uint8ClampedArray(width*height),
50-
p,
51-
96+
defaults = instance.defaults,
97+
imageData = video.getVideoCanvasImageData(),
98+
canvas = video.canvas,
99+
height = canvas.get('height'),
100+
width = canvas.get('width'),
101+
integralImage = new Int32Array(width*height),
102+
integralImageSquare = new Int32Array(width*height),
103+
104+
imageLen = 0,
52105
g,
53-
grayLen = 0,
54106

55107
stages = instance.data[config.data || defaults.data],
56108
stagesLen = stages.length,
57-
s;
58-
59-
video.canvas.forEach(video.getVideoCanvasImageData(), function(r, g, b, a, w, i, j) {
60-
p = r*0.299 + b*0.587 + g*0.114;
61-
gray[grayLen++] = p;
62-
});
109+
s,
110+
pixel,
111+
pixelSum = 0,
112+
pixelSumSquare = 0;
63113

64-
// ...
114+
canvas.forEach(imageData, function(r, g, b, a, w, i, j) {
115+
pixel = ~~(r*0.299 + b*0.587 + g*0.114);
65116

66-
for (g = 0; g < grayLen; g++) {
67-
var i, j, w, h;
68-
}
117+
pixelSum += pixel;
118+
pixelSumSquare += pixel*pixel;
69119

70-
for (s = 0; s < stagesLen; s++) {
71-
var stage = stages[s];
120+
integralImage[imageLen] = pixelSum;
121+
integralImageSquare[imageLen] = pixelSumSquare;
122+
imageLen++;
123+
});
72124

73-
instance.analisa_(stage, gray, i, j, w, h);
125+
var i,
126+
j,
127+
blockJump = defaults.blockJump,
128+
blockScale = defaults.blockScale,
129+
blockSize = defaults.blockSize,
130+
maxBlockSize = Math.min(width, height);
131+
132+
for (; blockSize <= maxBlockSize; blockSize = ~~(blockScale*blockSize)) {
133+
for (i = 0; i < (height - blockSize); i+=blockJump) {
134+
for (j = 0; j < (width - blockSize); j+=blockJump) {
135+
for (s = 0; s < stagesLen; s++) {
136+
var stage = stages[s];
137+
138+
if (!instance.evalStage_(stage, integralImage, integralImageSquare, i, j, blockSize)) {
139+
// if (stage[0] > 10) {
140+
// debugger;
141+
// }
142+
break;
143+
}
144+
145+
console.log('ROSTO');
146+
}
147+
}
148+
}
74149
}
75150
}
76151

77152
};
78153

154+
// canvas.setImageData(imageData);
155+
// canvas.context.strokeStyle = "rgb(255,0,0)";
156+
// canvas.context.strokeRect(j, i, blockSize, blockSize);
157+
79158
}( window ));

0 commit comments

Comments
 (0)