-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmtcnn.js
261 lines (198 loc) · 9.17 KB
/
mtcnn.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
const tf = require('@tensorflow/tfjs-node');
const Model = require('./models');
const {calibrate_box, convert_to_square, get_image_boxes, generate_boxes, preprocess} = require('./box_utils');
DEF_THRESHOLDS = [0.5, 0.5, 0.5]
DEF_NMS_THRESHOLDS = [0.6, 0.6, 0.6]
class MTCNN{
// Top level class for mtcnn detection.
constructor(pnet_path, rnet_path, onet_path,
min_face_size=20.0,
thresholds=null,
mns_thresholds=null,
max_output_size=300){
this.pnet = Model(pnet_path)
this.rnet = Model(rnet_path)
this.onet = Model(onet_path)
this.min_face_size = min_face_size
this.thresholds = thresholds || DEF_THRESHOLDS
this.mns_thresholds = mns_thresholds || DEF_NMS_THRESHOLDS
this.max_output_size = max_output_size
this.scale_cache = {}
}
async detect(img){
// """Detect faces and facial landmarks on an image
// Parameters:
// img: rgb image, numpy array of shape [h, w, 3]
// Returns:
// bboxes: float tensor of shape [n, 4], face bounding boxes
// landmarks: float tensor of shape [n, 10], 5 facial landmarks,
// first 5 numbers of array are x coords, last are y coords
// scores: float tensor of shape [n], confidence scores
// """
var [height, width] = img.shape
const scales = this.get_scale(height, width)
var boxes = await this.stage_one(img, scales)
if (boxes.shape[0] == 0){
return {boxes: null, landmarks: null, scores: null}
}
boxes = await this.stage_two(img, boxes, height, width, boxes.shape[0])
if (boxes.shape[0] == 0){
return {boxes: null, landmarks: null, scores: null}
}
const data = await this.stage_three(img, boxes, height, width, boxes.shape[0])
return data
}
get_scale(height, width){
// """Compute scaling factors for given image dimensions
// Parameters:
// height: float
// width: float
// Returns:
// list of floats, scaling factors
// """
var min_length = Math.min(height, width)
if (min_length in this.scale_cache){
return this.scale_cache[min_length]
}
const min_detection_size = 12.0
const factor = 0.707
const scales = []
const m = min_detection_size / this.min_face_size
min_length *= m
var factor_count = 0
while (min_length > min_detection_size){
scales.push(m * factor ** factor_count)
min_length = min_length * factor
factor_count += 1
}
this.scale_cache[min_length] = scales
return scales
}
async stage_one_scale(img, height, width, scale) {
// """Perform stage one part with a given scaling factor
// Parameters:
// img: rgb image, float tensor of shape [h, w, 3]
// height: image height, float
// width: image width, float
// scale: scaling factor, float
// Returns:
// float tensor of shape [n, 9]
// """
var hs = tf.ceil(tf.mul(height, scale))
var ws = tf.ceil(tf.mul(width, scale))
var img_in = tf.image.resizeBilinear(img, [hs.dataSync()[0], ws.dataSync()[0]])
img_in = preprocess(img_in)
img_in = tf.expandDims(img_in, 0)
var [probs, offsets] = this.pnet.predict(img_in)
const probs_zero = tf.tensor(probs.arraySync()[0])
const offsets_zero = tf.tensor(offsets.arraySync()[0])
const boxes = await generate_boxes(probs_zero, offsets_zero, scale, this.thresholds[0])
if(boxes.shape[0] == 0){
return boxes
}
const keep = tf.image.nonMaxSuppression(tf.slice(boxes, [0,0], [-1,4]),
tf.reshape(tf.slice(boxes, [0,4], [-1,1]), [-1]),
this.max_output_size, 0.5)
return tf.gather(boxes, keep)
}
stage_one_filter(boxes){
// """Filter out boxes in stage one
// Parameters:
// boxes: collected boxes with different scales, float tensor of shape [n, 9]
// Returns:
// float tensor of shape [n, 4]
// """
var boxess = tf.slice(boxes, [0,0], [-1,4])
const scores = tf.reshape(tf.slice(boxes, [0,4], [-1,1]), [-1])
const offsets = tf.slice(boxes, [0,5], [-1,-1])
boxess = calibrate_box(boxess, offsets)
boxess = convert_to_square(boxess)
const keep = tf.image.nonMaxSuppression(boxess, scores, this.max_output_size, this.mns_thresholds[0])
boxess = tf.gather(boxess, keep)
return boxess
}
async stage_one(img, scales){
// """Run stage one on the input image
// Parameters:
// img: rgb image, float tensor of shape [h, w, 3]
// scales: scaling factors, list of floats
// Returns:
// float tensor of shape [n, 4], predicted bounding boxes
// """
const [height, width] = img.shape
var boxes = []
for (let i = 0; i < scales.length; i++){
boxes.push(await this.stage_one_scale(img, height, width, scales[i]))
}
boxes = await tf.concat(boxes, 0)
if(boxes.shape[0] == 0){
return []
}
return this.stage_one_filter(boxes)
}
async stage_two(img, boxes, height, width, num_boxes){
// """Run stage two on the input image
// Parameters:
// img: rgb image, float tensor of shape [h, w, 3]
// bboxes: bounding boxes from stage one, float tensor of shape [n, 4]
// height: image height, float
// width: image width, float
// num_boxes: number of rows in bboxes, int
// Returns:
// float tensor of shape [n, 4], predicted bounding boxes
// """
var img_boxes = get_image_boxes(boxes, img, height, width, num_boxes, 24)
const data = this.rnet.predict(img_boxes)
var [probs, offsets] = data
const slice_probs = tf.reshape(tf.slice(probs, [0,1], [-1,1]), [-1])
var keep = tf.reshape(tf.slice(await tf.whereAsync(tf.greater(slice_probs, this.thresholds[1])), [0,0], [-1,1]), [-1])
boxes = tf.gather(boxes, keep)
offsets = tf.gather(offsets, keep)
const scores = tf.gather(tf.reshape(tf.slice(probs, [0,1], [-1,1]), [-1]), keep)
boxes = calibrate_box(boxes, offsets)
boxes = convert_to_square(boxes)
keep = tf.image.nonMaxSuppression(boxes, scores, this.max_output_size, this.mns_thresholds[1])
boxes = tf.gather(boxes, keep)
return boxes
}
async stage_three(img, boxes, height, width, num_boxes){
// """Run stage three on the input image
// Parameters:
// img: rgb image, float tensor of shape [h, w, 3]
// bboxes: bounding boxes from stage two, float tensor of shape [n, 4]
// height: image height, float
// width: image width, float
// num_boxes: number of rows in bboxes, int
// Returns:
// bboxes: float tensor of shape [n, 4], face bounding boxes
// landmarks: float tensor of shape [n, 10], 5 facial landmarks,
// first 5 numbers of array are x coords, last are y coords
// scores: float tensor of shape [n], confidence scores
// """
const img_boxes = get_image_boxes(boxes, img, height, width, num_boxes, 48)
const data = this.onet.predict(img_boxes)
var [probs, offsets, landmarks] = data
const slice_probs = tf.reshape(tf.slice(probs, [0,1], [-1,1]), [-1])
var keep = tf.reshape(tf.slice(await tf.whereAsync(tf.greater(slice_probs, this.thresholds[2])), [0,0], [-1,1]), [-1])
boxes = tf.gather(boxes, keep)
offsets = tf.gather(offsets, keep)
var scores = tf.gather(tf.reshape(tf.slice(probs, [0,1], [-1,1]), [-1]), keep)
landmarks = tf.gather(landmarks, keep)
//compute landmak points
width = tf.expandDims(tf.reshape(tf.slice(boxes, [0,2], [-1,1]), [-1]).sub(tf.reshape(tf.slice(boxes, [0,0], [-1,1]), [-1])).add(1.0), 1)
height = tf.expandDims(tf.reshape(tf.slice(boxes, [0,3], [-1,1]), [-1]).sub(tf.reshape(tf.slice(boxes, [0,1], [-1,1]), [-1])).add(1.0), 1)
const xmin = tf.expandDims(tf.reshape(tf.slice(boxes, [0,0], [-1,1]), [-1]), 1)
const ymin = tf.expandDims(tf.reshape(tf.slice(boxes, [0,1], [-1,1]), [-1]), 1)
landmarks = tf.concat([
tf.mul(tf.slice(landmarks, [0,0], [-1,5]), width).add(xmin),
tf.mul(tf.slice(landmarks, [0,5], [-1,5]), height).add(ymin)
], 1)
boxes = calibrate_box(boxes, offsets)
keep = tf.image.nonMaxSuppression(boxes, scores, this.max_output_size, this.mns_thresholds[2])
boxes = tf.gather(boxes, keep).arraySync()[0]
scores = tf.gather(scores, keep).arraySync()[0]
landmarks = tf.gather(landmarks, keep).arraySync()[0]
return {boxes, landmarks, scores}
}
}
module.exports = MTCNN