-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ObjectTracker.js
169 lines (145 loc) · 4.4 KB
/
ObjectTracker.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
(function() {
/**
* ObjectTracker utility.
* @constructor
* @param {string|Array.<string|Array.<number>>} opt_classifiers Optional
* object classifiers to track.
* @extends {tracking.Tracker}
*/
tracking.ObjectTracker = function(opt_classifiers) {
tracking.ObjectTracker.base(this, 'constructor');
if (opt_classifiers) {
if (!Array.isArray(opt_classifiers)) {
opt_classifiers = [opt_classifiers];
}
if (Array.isArray(opt_classifiers)) {
opt_classifiers.forEach(function(classifier, i) {
if (typeof classifier === 'string') {
opt_classifiers[i] = tracking.ViolaJones.classifiers[classifier];
}
if (!opt_classifiers[i]) {
throw new Error('Object classifier not valid, try `new tracking.ObjectTracker("face")`.');
}
});
}
}
this.setClassifiers(opt_classifiers);
};
tracking.inherits(tracking.ObjectTracker, tracking.Tracker);
/**
* Specifies the edges density of a block in order to decide whether to skip
* it or not.
* @default 0.2
* @type {number}
*/
tracking.ObjectTracker.prototype.edgesDensity = 0.2;
/**
* Specifies the initial scale to start the feature block scaling.
* @default 1.0
* @type {number}
*/
tracking.ObjectTracker.prototype.initialScale = 1.0;
/**
* Specifies the scale factor to scale the feature block.
* @default 1.25
* @type {number}
*/
tracking.ObjectTracker.prototype.scaleFactor = 1.25;
/**
* Specifies the block step size.
* @default 1.5
* @type {number}
*/
tracking.ObjectTracker.prototype.stepSize = 1.5;
/**
* Gets the tracker HAAR classifiers.
* @return {TypedArray.<number>}
*/
tracking.ObjectTracker.prototype.getClassifiers = function() {
return this.classifiers;
};
/**
* Gets the edges density value.
* @return {number}
*/
tracking.ObjectTracker.prototype.getEdgesDensity = function() {
return this.edgesDensity;
};
/**
* Gets the initial scale to start the feature block scaling.
* @return {number}
*/
tracking.ObjectTracker.prototype.getInitialScale = function() {
return this.initialScale;
};
/**
* Gets the scale factor to scale the feature block.
* @return {number}
*/
tracking.ObjectTracker.prototype.getScaleFactor = function() {
return this.scaleFactor;
};
/**
* Gets the block step size.
* @return {number}
*/
tracking.ObjectTracker.prototype.getStepSize = function() {
return this.stepSize;
};
/**
* Tracks the `Video` frames. This method is called for each video frame in
* order to emit `track` event.
* @param {Uint8ClampedArray} pixels The pixels data to track.
* @param {number} width The pixels canvas width.
* @param {number} height The pixels canvas height.
*/
tracking.ObjectTracker.prototype.track = function(pixels, width, height) {
var self = this;
var classifiers = this.getClassifiers();
if (!classifiers) {
throw new Error('Object classifier not specified, try `new tracking.ObjectTracker("face")`.');
}
var results = [];
classifiers.forEach(function(classifier) {
results = results.concat(tracking.ViolaJones.detect(pixels, width, height, self.getInitialScale(), self.getScaleFactor(), self.getStepSize(), self.getEdgesDensity(), classifier));
});
this.emit('track', {
data: results
});
};
/**
* Sets the tracker HAAR classifiers.
* @param {TypedArray.<number>} classifiers
*/
tracking.ObjectTracker.prototype.setClassifiers = function(classifiers) {
this.classifiers = classifiers;
};
/**
* Sets the edges density.
* @param {number} edgesDensity
*/
tracking.ObjectTracker.prototype.setEdgesDensity = function(edgesDensity) {
this.edgesDensity = edgesDensity;
};
/**
* Sets the initial scale to start the block scaling.
* @param {number} initialScale
*/
tracking.ObjectTracker.prototype.setInitialScale = function(initialScale) {
this.initialScale = initialScale;
};
/**
* Sets the scale factor to scale the feature block.
* @param {number} scaleFactor
*/
tracking.ObjectTracker.prototype.setScaleFactor = function(scaleFactor) {
this.scaleFactor = scaleFactor;
};
/**
* Sets the block step size.
* @param {number} stepSize
*/
tracking.ObjectTracker.prototype.setStepSize = function(stepSize) {
this.stepSize = stepSize;
};
}());