-
Notifications
You must be signed in to change notification settings - Fork 93
/
recognize_face_by_dnn_openface.php
103 lines (77 loc) · 2.9 KB
/
recognize_face_by_dnn_openface.php
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
<?php
use CV\Scalar, CV\Size;
use function CV\{imread, imwrite, rectangle};
$netDet = \CV\DNN\readNetFromCaffe('models/ssd/res10_300x300_ssd_deploy.prototxt', 'models/ssd/res10_300x300_ssd_iter_140000.caffemodel');
$netRecogn = \CV\DNN\readNetFromTorch('models/openface/openface.nn4.small2.v1.t7');
$image2faces = function ($src) use ($netDet) {
$size = $src->size(); // 2000x500
$minSide = min($size->width, $size->height);
$divider = $minSide / 300;
\CV\resize($src, $resized, new Size($size->width / $divider, $size->height / $divider)); // 1200x300
//var_export($resized);
$blob = \CV\DNN\blobFromImage($resized, 1, new Size(), new Scalar(104, 177, 123), true, false);
$netDet->setInput($blob);
$r = $netDet->forward();
//var_export($r->shape);
$faces = [];
for ($i = 0; $i < $r->shape[2]; $i++) {
$confidence = $r->atIdx([0,0,$i,2]);
if ($confidence > 0.9) {
//var_export($confidence);echo "\n";
$startX = $r->atIdx([0,0,$i,3]) * $src->cols;
$startY = $r->atIdx([0,0,$i,4]) * $src->rows;
$endX = $r->atIdx([0,0,$i,5]) * $src->cols;
$endY = $r->atIdx([0,0,$i,6]) * $src->rows;
$faces[] = $src->getImageROI(new \CV\Rect($startX, $startY, $endX - $startX, $endY - $startY));
}
}
return $faces;
};
$face2vec = function ($face) use ($netRecogn) {
$blob = \CV\DNN\blobFromImage($face, 1.0 / 255, new Size(96, 96), new Scalar(), true, false);
$netRecogn->setInput($blob);
return $netRecogn->forward();
};
function faceDistance($face1, $face2) {
$distance = 0;
foreach ($face1 as $i => $v) {
$distance += ($face1[$i] - $face2[$i])**2;
}
return sqrt($distance);
}
$src = imread("images/faces.jpg");
$faces = $image2faces($src);
$faceVectors = [];
foreach ($faces as $i => $face) {
$vec = $face2vec($face);
//imwrite("results/_face.jpg", $face);
//$vec->print();
//var_export($vec->data());
$faceVectors["me$i"] = $vec->data();
}
$src = imread("images/angelina_faces.png");
$faces = $image2faces($src);
foreach ($faces as $i => $face) {
$vec = $face2vec($face);
//imwrite("results/_face.jpg", $face);
//$vec->print();
//var_export($vec->data());
$faceVectors["angelina$i"] = $vec->data();
}
//var_export($faceVectors);
$src = imread("images/angelina_and_me.png");
$faces = $image2faces($src);
foreach ($faces as $i => $face) {
$vec = $face2vec($face);
$minDistance = 0;
$faceLabel = '';
foreach ($faceVectors as $label => $faceVector) {
$distance = faceDistance($vec->data(), $faceVector);
if (!$minDistance || $distance < $minDistance) {
$minDistance = $distance;
$faceLabel = $label;
}
}
$similarity = intval((max(sqrt(2), $minDistance) - $minDistance) / sqrt(2) * 100);
echo "face$i $faceLabel distance: $minDistance, similarity: $similarity%\n";
}