Skip to content

Commit bb5b1be

Browse files
committed
VanFace
1 parent 98abdbc commit bb5b1be

File tree

5 files changed

+350
-0
lines changed

5 files changed

+350
-0
lines changed

Diff for: detect_facemarks_by_dnn_vanface.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
use CV\Scalar, CV\Size;
4+
use function CV\{imread, imwrite, cvtColor, circle};
5+
6+
$netDet = \CV\DNN\readNetFromCaffe('models/ssd/res10_300x300_ssd_deploy.prototxt', 'models/ssd/res10_300x300_ssd_iter_140000.caffemodel');
7+
$netRecogn = \CV\DNN\readNetFromCaffe('models/VanFace/VanFace.prototxt', 'models/VanFace/VanFace.caffemodel');
8+
9+
$src = imread("images/faces.jpg");
10+
$size = $src->size(); // 2000x500
11+
12+
$minSide = min($size->width, $size->height);
13+
$divider = $minSide / 300;
14+
\CV\resize($src, $resized, new Size($size->width / $divider, $size->height / $divider)); // 1200x300
15+
16+
//var_export($resized);
17+
18+
$blob = \CV\DNN\blobFromImage($resized, 1, new Size(), new Scalar(104, 177, 123), true, false);
19+
20+
$netDet->setInput($blob);
21+
22+
$r = $netDet->forward();
23+
24+
//var_export($r->shape);
25+
26+
$faces = [];
27+
$scalar = new Scalar(0, 0, 255);
28+
for ($i = 0; $i < $r->shape[2]; $i++) {
29+
$confidence = $r->atIdx([0,0,$i,2]);
30+
if ($confidence > 0.9) {
31+
var_export($confidence);echo "\n";
32+
$startX = $r->atIdx([0,0,$i,3]) * $src->cols;
33+
$startY = $r->atIdx([0,0,$i,4]) * $src->rows;
34+
$endX = $r->atIdx([0,0,$i,5]) * $src->cols;
35+
$endY = $r->atIdx([0,0,$i,6]) * $src->rows;
36+
37+
$face = $src->getImageROI(new \CV\Rect($startX, $startY, $endX - $startX, $endY - $startY));
38+
\CV\resize($face, $resized, new Size(60,60));
39+
$resized = cvtColor($resized, \CV\COLOR_BGR2GRAY, 2);
40+
\CV\meanStdDev($resized, $mean, $sdv);
41+
42+
$m = $mean->data()[0];
43+
$s = $sdv->data()[0];
44+
45+
$blob = \CV\DNN\blobFromImage($resized, 1 / (0.000001 + $s), new Size(60,60), new Scalar($m, $m, $m));
46+
$netRecogn->setInput($blob);
47+
//var_export($blob);die();
48+
$out = $netRecogn->forward();
49+
$data = $out->data();
50+
51+
for ($j=0;$j<68;$j++) {
52+
$point = new \CV\Point($startX + $data[$j*2] * $face->cols, $startY + $data[$j*2+1] * $face->rows);
53+
circle($src, $point, 2, new Scalar(0, 0, 255), 2);
54+
//var_export($point);
55+
}
56+
}
57+
}
58+
59+
imwrite("results/_detect_facemarks_by_dnn_vanface.jpg", $src);

Diff for: models/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ Sources:
1010
* ssdlite_mobilenet_v2_coco - https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md
1111
* ssd_mobilenet_v2_coco - https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md
1212
* insightface - https://github.com/axinc-ai/ailia-models/tree/master/face_identification/insightface
13+
* VanFace - https://github.com/lsy17096535/face-landmark

Diff for: models/VanFace/VanFace.caffemodel

3.15 MB
Binary file not shown.

Diff for: models/VanFace/VanFace.prototxt

+290
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
name: "landmark"
2+
input: "data"
3+
input_dim: 1
4+
input_dim: 1
5+
input_dim: 60
6+
input_dim: 60
7+
########################################
8+
# the actual net
9+
# layer 1
10+
layer {
11+
name: "Conv1"
12+
type: "Convolution"
13+
bottom: "data"
14+
top: "Conv1"
15+
param {
16+
lr_mult: 1
17+
decay_mult: 1
18+
}
19+
param {
20+
lr_mult: 2
21+
decay_mult: 0
22+
}
23+
convolution_param {
24+
num_output: 20
25+
pad: 2
26+
kernel_size: 5
27+
stride: 1
28+
weight_filler {
29+
type: "xavier"
30+
std: 0.1
31+
}
32+
bias_filler {
33+
type: "constant"
34+
value: 0.2
35+
}
36+
}
37+
}
38+
39+
layer {
40+
name: "ActivationTangH1"
41+
bottom: "Conv1"
42+
top: "ActivationTangH1"
43+
type: "TanH"
44+
}
45+
46+
layer {
47+
name: "ActivationAbs1"
48+
bottom: "ActivationTangH1"
49+
top: "Abs1"
50+
type: "AbsVal"
51+
}
52+
53+
layer {
54+
name: "Pool1"
55+
type: "Pooling"
56+
bottom: "Abs1"
57+
top: "Pool1"
58+
pooling_param {
59+
pool: MAX
60+
kernel_size: 2
61+
stride: 2
62+
}
63+
}
64+
65+
layer {
66+
name: "Conv2"
67+
type: "Convolution"
68+
bottom: "Pool1"
69+
top: "Conv2"
70+
param {
71+
lr_mult: 1
72+
decay_mult: 1
73+
}
74+
param {
75+
lr_mult: 2
76+
decay_mult: 0
77+
}
78+
convolution_param {
79+
num_output: 48
80+
pad: 2
81+
kernel_size: 5
82+
stride: 1
83+
weight_filler {
84+
type: "xavier"
85+
std: 0.1
86+
}
87+
bias_filler {
88+
type: "constant"
89+
value: 0.2
90+
}
91+
}
92+
}
93+
94+
layer {
95+
name: "ActivationTangH2"
96+
bottom: "Conv2"
97+
top: "ActivationTangH2"
98+
type: "TanH"
99+
}
100+
101+
layer {
102+
name: "ActivationAbs2"
103+
bottom: "ActivationTangH2"
104+
top: "Abs2"
105+
type: "AbsVal"
106+
}
107+
108+
109+
layer {
110+
name: "Pool2"
111+
type: "Pooling"
112+
bottom: "Abs2"
113+
top: "Pool2"
114+
pooling_param {
115+
pool: MAX
116+
kernel_size: 2
117+
stride: 2
118+
}
119+
}
120+
121+
# layer 3
122+
layer {
123+
name: "Conv3"
124+
type: "Convolution"
125+
bottom: "Pool2"
126+
top: "Conv3"
127+
param {
128+
lr_mult: 1
129+
decay_mult: 1
130+
}
131+
param {
132+
lr_mult: 2
133+
decay_mult: 0
134+
}
135+
convolution_param {
136+
num_output: 64
137+
pad: 0
138+
kernel_size: 3
139+
stride: 1
140+
weight_filler {
141+
type: "xavier"
142+
std: 0.1
143+
}
144+
bias_filler {
145+
type: "constant"
146+
value: 0.2
147+
}
148+
}
149+
}
150+
151+
152+
layer {
153+
name: "ActivationTangH3"
154+
bottom: "Conv3"
155+
top: "ActivationTangH3"
156+
type: "TanH"
157+
}
158+
159+
layer {
160+
name: "ActivationAbs3"
161+
bottom: "ActivationTangH3"
162+
top: "Abs3"
163+
type: "AbsVal"
164+
}
165+
166+
layer {
167+
name: "Pool3"
168+
type: "Pooling"
169+
bottom: "Abs3"
170+
top: "Pool3"
171+
pooling_param {
172+
pool: MAX
173+
kernel_size: 3
174+
stride: 2
175+
}
176+
}
177+
178+
# layer 4
179+
layer {
180+
name: "Conv4"
181+
type: "Convolution"
182+
bottom: "Pool3"
183+
top: "Conv4"
184+
param {
185+
lr_mult: 1
186+
decay_mult: 1
187+
}
188+
param {
189+
lr_mult: 2
190+
decay_mult: 0
191+
}
192+
convolution_param {
193+
num_output: 80
194+
pad: 0
195+
kernel_size: 3
196+
stride: 1
197+
weight_filler {
198+
type: "xavier"
199+
std: 0.1
200+
}
201+
bias_filler {
202+
type: "constant"
203+
value: 0.2
204+
}
205+
}
206+
}
207+
208+
209+
layer {
210+
name: "ActivationTangH4"
211+
bottom: "Conv4"
212+
top: "ActivationTangH4"
213+
type: "TanH"
214+
}
215+
216+
layer {
217+
name: "ActivationAbs4"
218+
bottom: "ActivationTangH4"
219+
top: "Abs4"
220+
type: "AbsVal"
221+
}
222+
223+
224+
########################################
225+
226+
layer {
227+
name: "Dense1"
228+
type: "InnerProduct"
229+
bottom: "Abs4"
230+
top: "Dense1"
231+
param {
232+
lr_mult: 1
233+
decay_mult: 1
234+
}
235+
param {
236+
lr_mult: 2
237+
decay_mult: 0
238+
}
239+
inner_product_param {
240+
num_output: 512
241+
weight_filler {
242+
type: "xavier"
243+
}
244+
bias_filler {
245+
type: "constant"
246+
value: 0
247+
}
248+
}
249+
}
250+
251+
252+
layer {
253+
name: "ActivationTangH5"
254+
bottom: "Dense1"
255+
top: "ActivationTangH5"
256+
type: "TanH"
257+
}
258+
259+
layer {
260+
name: "ActivationAbs5"
261+
bottom: "ActivationTangH5"
262+
top: "Abs5"
263+
type: "AbsVal"
264+
}
265+
266+
267+
layer {
268+
name: "Dense3"
269+
type: "InnerProduct"
270+
bottom: "Abs5"
271+
top: "Dense3"
272+
param {
273+
lr_mult: 1
274+
decay_mult: 1
275+
}
276+
param {
277+
lr_mult: 2
278+
decay_mult: 0
279+
}
280+
inner_product_param {
281+
num_output: 136
282+
weight_filler {
283+
type: "xavier"
284+
}
285+
bias_filler {
286+
type: "constant"
287+
value: 0
288+
}
289+
}
290+
}

Diff for: results/detect_facemarks_by_dnn_vanface.jpg

292 KB
Loading

0 commit comments

Comments
 (0)