-
Notifications
You must be signed in to change notification settings - Fork 93
/
recognize_face_by_facerecognizersf.php
54 lines (39 loc) · 1.57 KB
/
recognize_face_by_facerecognizersf.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
<?php
use CV\Scalar;
use function CV\{imread, imwrite, rectangle};
$src = imread("images/faces.jpg");
$scalar = new Scalar(0, 0, 255);
$netDet = CV\FaceDetectorYN::create('models/opencv_zoo/face_detection_yunet_2022mar.onnx', '', $src->size());
$netRecogn = CV\FaceRecognizerSF::create('models/opencv_zoo/face_recognition_sface_2021dec.onnx', '');
$r = $netDet->detect($src);
$features = [];
for ($i = 0; $i < $r->rows; $i++) {
$confidence = $r->atIdx([$i,14]);
var_export($confidence);echo "\n";
if ($confidence > 0.9) {
$startX = $r->atIdx([$i,0]);
$startY = $r->atIdx([$i,1]);
$w = $r->atIdx([$i,2]);
$h = $r->atIdx([$i,3]);
var_export([$startX, $startY, $startX + $w, $startY + $h]);echo "\n";
//rectangle($src, $startX, $startY, $startX + $w, $startY + $h, $scalar, 3);
//$faces[] = $src->getImageROI(new \CV\Rect($startX, $startY, $w, $h));
$faceBox = $r->row($i);
//$face = $netRecogn->alignCrop($r, $faceBox); // todo: doesn't work. needs fix
//imwrite("results/_recognize_face_by_facerecognizersf$i.jpg", $face);
$face = $src->getImageROI(new \CV\Rect($startX, $startY, $w, $h));
$feature = $netRecogn->feature($face);
var_export($feature);
var_export($feature->data());
$features[] = $feature;
}
}
foreach ($features as $i => $feature) {
foreach ($features as $j => $feature2) {
if ($i == $j) {
continue;
}
$similarity = $netRecogn->match($feature, $feature2);
var_export($similarity);echo "\n";
}
}