forked from php-opencv/php-opencv-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recognize_face_by_lbph.php
67 lines (55 loc) · 2.28 KB
/
recognize_face_by_lbph.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
<?php
use CV\Face\LBPHFaceRecognizer, CV\CascadeClassifier, CV\Scalar, CV\Point;
use function CV\{imread, cvtColor, equalizeHist};
use const CV\{COLOR_BGR2GRAY};
// face by lbpcascade_frontalface
$faceClassifier = new CascadeClassifier();
$faceClassifier->load('models/lbpcascades/lbpcascade_frontalface.xml');
$faceRecognizer = LBPHFaceRecognizer::create();
$labels = ['unknown', 'me', 'angelina'];
// me
$src = imread("images/faces.jpg");
$gray = cvtColor($src, COLOR_BGR2GRAY);
$faceClassifier->detectMultiScale($gray, $faces);
//var_export($faces);
equalizeHist($gray, $gray);
$faceImages = $faceLabels = [];
foreach ($faces as $k => $face) {
$faceImages[] = $gray->getImageROI($face); // face coordinates to image
$faceLabels[] = 1; // me
//cv\imwrite("results/recognize_face_by_lbph_me$k.jpg", $gray->getImageROI($face));
}
$faceRecognizer->train($faceImages, $faceLabels);
// angelina
$src = imread("images/angelina_faces.png");
$gray = cvtColor($src, COLOR_BGR2GRAY);
$faceClassifier->detectMultiScale($gray, $faces);
//var_export($faces);
equalizeHist($gray, $gray);
$faceImages = $faceLabels = [];
foreach ($faces as $k => $face) {
$faceImages[] = $gray->getImageROI($face); // face coordinates to image
$faceLabels[] = 2; // Angelina
//cv\imwrite("results/recognize_face_by_lbph_angelina$k.jpg", $gray->getImageROI($face));
}
$faceRecognizer->update($faceImages, $faceLabels);
//$faceRecognizer->write('results/lbph_model.xml');
//$faceRecognizer->read('results/lbph_model.xml');
// test image
$src = imread("images/angelina_and_me.png");
$gray = cvtColor($src, COLOR_BGR2GRAY);
$faceClassifier->detectMultiScale($gray, $faces);
//var_export($faces);
equalizeHist($gray, $gray);
foreach ($faces as $face) {
$faceImage = $gray->getImageROI($face);
//predict
$faceLabel = $faceRecognizer->predict($faceImage, $faceConfidence);
echo "{$faceLabel}, {$faceConfidence}\n";
$scalar = new \CV\Scalar(0, 0, 255);
\CV\rectangleByRect($src, $face, $scalar, 2);
$text = $labels[$faceLabel];
\CV\rectangle($src, $face->x, $face->y, $face->x + ($faceLabel == 1 ? 50 : 130), $face->y - 30, new Scalar(255,255,255), -2);
\CV\putText($src, "$text", new Point($face->x, $face->y - 2), 0, 1.5, new Scalar(), 2);
}
cv\imwrite("results/_recognize_face_by_lbph.jpg", $src);