Skip to content

Commit d83fc59

Browse files
committed
fixed recognize example and phpdoc
1 parent 47ee9a2 commit d83fc59

5 files changed

+78
-52
lines changed

Diff for: detect_face_by_cascade_classifier.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
$src = imread("images/faces.jpg");
99
$gray = cvtColor($src, COLOR_BGR2GRAY);
10-
equalizeHist($gray, $gray);
1110

1211
// face by lbpcascade_frontalface
1312
$faceClassifier = new CascadeClassifier();
@@ -40,4 +39,4 @@
4039
}*/
4140

4241

43-
$src = imwrite("results/_detect_face_by_cascade_classifier.jpg", $src);
42+
imwrite("results/_detect_face_by_cascade_classifier.jpg", $src);

Diff for: images/angelina_and_me.png

190 KB
Loading

Diff for: images/angelina_faces.png

344 KB
Loading

Diff for: phpdoc.php

+27-27
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function col($x) {
150150

151151
/**
152152
* @param array $rect
153-
* @return Mat $mat
153+
* @return Mat $image
154154
*/
155155
public function getImageROI(Rect $rect) {
156156

@@ -412,7 +412,7 @@ public function load(string $filename) {
412412
}
413413

414414
/**
415-
* @param Mat $mat
415+
* @param Mat $image
416416
* @param array $objects
417417
* @param float $scale_factor
418418
* @param int $min_neighbors
@@ -421,7 +421,7 @@ public function load(string $filename) {
421421
* @param Size|null $maxSize
422422
* @return null
423423
*/
424-
public function detectMultiScale(Mat $mat, array &$rects, float $scale_factor = 1.1, int $min_neighbors = 3, int $flags = 0, Size $minSize = null, Size $maxSize = null) {
424+
public function detectMultiScale(Mat $image, array &$rects, float $scale_factor = 1.1, int $min_neighbors = 3, int $flags = 0, Size $minSize = null, Size $maxSize = null) {
425425
return null;
426426
}
427427
}
@@ -437,24 +437,24 @@ function imread(string $filename, $flags = null) {
437437

438438
/**
439439
* @param string $filename
440-
* @param Mat $mat
440+
* @param Mat $image
441441
* @return true|null
442442
*/
443-
function imwrite(string $filename, Mat $mat) {
443+
function imwrite(string $filename, Mat $image) {
444444

445445
}
446446

447447
/**
448-
* @param Mat $mat
448+
* @param Mat $image
449449
* @param $code
450450
* @return Mat|null
451451
*/
452-
function cvtColor(Mat $mat, $code, $dstCn = null) {
452+
function cvtColor(Mat $image, $code, $dstCn = null) {
453453

454454
}
455455

456456
/**
457-
* @param Mat $mat
457+
* @param Mat $image
458458
* @param Point $startPoint
459459
* @param Point $endPoint
460460
* @param Scalar $color
@@ -463,12 +463,12 @@ function cvtColor(Mat $mat, $code, $dstCn = null) {
463463
* @param int $shift
464464
* @return null
465465
*/
466-
function line(Mat $mat, Point $startPoint, Point $endPoint, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
466+
function line(Mat $image, Point $startPoint, Point $endPoint, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
467467
return null;
468468
}
469469

470470
/**
471-
* @param Mat $mat
471+
* @param Mat $image
472472
* @param Point $point
473473
* @param int $radius
474474
* @param Scalar $color
@@ -477,12 +477,12 @@ function line(Mat $mat, Point $startPoint, Point $endPoint, Scalar $color, int $
477477
* @param int $shift
478478
* @return null
479479
*/
480-
function circle(Mat $mat, Point $point, int $radius, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
480+
function circle(Mat $image, Point $point, int $radius, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
481481
return null;
482482
}
483483

484484
/**
485-
* @param Mat $mat
485+
* @param Mat $image
486486
* @param Point $point
487487
* @param Size $size
488488
* @param int $angle
@@ -494,12 +494,12 @@ function circle(Mat $mat, Point $point, int $radius, Scalar $color, int $thickne
494494
* @param int $shift
495495
* @return null
496496
*/
497-
function ellipse(Mat $mat, Point $point, Size $size, int $angle, int $startAngle, int $endAngle, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
497+
function ellipse(Mat $image, Point $point, Size $size, int $angle, int $startAngle, int $endAngle, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
498498
return null;
499499
}
500500

501501
/**
502-
* @param Mat $mat
502+
* @param Mat $image
503503
* @param int $startX
504504
* @param int $startY
505505
* @param int $endX
@@ -510,12 +510,12 @@ function ellipse(Mat $mat, Point $point, Size $size, int $angle, int $startAngle
510510
* @param int $shift
511511
* @return null
512512
*/
513-
function rectangle(Mat $mat, int $startX, int $startY, int $endX, int $endY, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
513+
function rectangle(Mat $image, int $startX, int $startY, int $endX, int $endY, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
514514
return null;
515515
}
516516

517517
/**
518-
* @param Mat $mat
518+
* @param Mat $image
519519
* @param Point $startPoint
520520
* @param Point $endPoint
521521
* @param Scalar $color
@@ -524,25 +524,25 @@ function rectangle(Mat $mat, int $startX, int $startY, int $endX, int $endY, Sca
524524
* @param int $shift
525525
* @return null
526526
*/
527-
function rectangleByPoint(Mat $mat, Point $startPoint, Point $endPoint, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
527+
function rectangleByPoint(Mat $image, Point $startPoint, Point $endPoint, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
528528
return null;
529529
}
530530

531531
/**
532-
* @param Mat $mat
532+
* @param Mat $image
533533
* @param Rect $rect
534534
* @param Scalar $color
535535
* @param int $thickness
536536
* @param int|null $lineType
537537
* @param int $shift
538538
* @return null
539539
*/
540-
function rectangleByRect(Mat $mat, Rect $rect, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
540+
function rectangleByRect(Mat $image, Rect $rect, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
541541
return null;
542542
}
543543

544544
/**
545-
* @param Mat $mat
545+
* @param Mat $image
546546
* @param string $text
547547
* @param Point $point
548548
* @param int $fontFace
@@ -553,29 +553,29 @@ function rectangleByRect(Mat $mat, Rect $rect, Scalar $color, int $thickness = 1
553553
* @param bool $bottomLeftOrigin
554554
* @return null
555555
*/
556-
function putText(Mat $mat, string $text, Point $point, int $fontFace, float $fontScale, Scalar $color, int $thickness = 1, int $lineType = null, bool $bottomLeftOrigin = false) {
556+
function putText(Mat $image, string $text, Point $point, int $fontFace, float $fontScale, Scalar $color, int $thickness = 1, int $lineType = null, bool $bottomLeftOrigin = false) {
557557
return null;
558558
}
559559

560560
/**
561-
* @param Mat $mat
561+
* @param Mat $image
562562
* @param Mat $dst
563563
* @return null
564564
*/
565-
function equalizeHist(Mat $mat, Mat &$dst) {
565+
function equalizeHist(Mat $image, Mat &$dst) {
566566
return null;
567567
}
568568

569569
/**
570-
* @param Mat $mat
570+
* @param Mat $image
571571
* @param Mat $dst
572572
* @param Size $size
573573
* @param float $fx
574574
* @param float $fy
575575
* @param int $interpolation
576576
* @return null
577577
*/
578-
function resize(Mat $mat, Mat &$dst, Size $size, float $fx = 0, float $fy = 0, int $interpolation = 1) {
578+
function resize(Mat $image, Mat &$dst, Size $size, float $fx = 0, float $fy = 0, int $interpolation = 1) {
579579
return null;
580580
}
581581

@@ -1000,12 +1000,12 @@ public function loadModel(string $filename) {
10001000
}
10011001

10021002
/**
1003-
* @param Mat $mat
1003+
* @param Mat $image
10041004
* @param array $faces
10051005
* @param array $landmarks
10061006
* @return null
10071007
*/
1008-
public function fit(Mat $mat, array $faces, array &$landmarks) {
1008+
public function fit(Mat $image, array $faces, array &$landmarks) {
10091009
return null;
10101010
}
10111011
}

Diff for: recognize_face_by_lbph.php

+50-23
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,67 @@
11
<?php
22

3-
use CV\Face\LBPHFaceRecognizer, CV\CascadeClassifier;
3+
use CV\Face\LBPHFaceRecognizer, CV\CascadeClassifier, CV\Scalar, CV\Point;
44
use function CV\{imread, cvtColor, equalizeHist};
55
use const CV\{COLOR_BGR2GRAY};
66

7-
$src = imread("images/faces.jpg");
8-
$gray = cvtColor($src, COLOR_BGR2GRAY);
9-
equalizeHist($gray, $gray);
10-
117
// face by lbpcascade_frontalface
128
$faceClassifier = new CascadeClassifier();
139
$faceClassifier->load('models/lbpcascades/lbpcascade_frontalface.xml');
14-
$faces = null;
10+
11+
$faceRecognizer = LBPHFaceRecognizer::create();
12+
13+
$labels = ['unknown', 'me', 'angelina'];
14+
15+
// me
16+
$src = imread("images/faces.jpg");
17+
$gray = cvtColor($src, COLOR_BGR2GRAY);
1518
$faceClassifier->detectMultiScale($gray, $faces);
1619
//var_export($faces);
20+
equalizeHist($gray, $gray);
21+
$faceImages = $faceLabels = [];
22+
foreach ($faces as $k => $face) {
23+
$faceImages[] = $gray->getImageROI($face); // face coordinates to image
24+
$faceLabels[] = 1; // me
25+
//cv\imwrite("results/recognize_face_by_lbph_me$k.jpg", $gray->getImageROI($face));
26+
}
27+
$faceRecognizer->train($faceImages, $faceLabels);
1728

18-
if ($faces) {
19-
$faceRecognizer = LBPHFaceRecognizer::create();
20-
21-
//train
22-
foreach ($faces as $k => $face) {
23-
$mat = $gray->getImageROI($face);
24-
$faceRecognizer->update([$mat], [$k+1]);
25-
}
29+
// angelina
30+
$src = imread("images/angelina_faces.png");
31+
$gray = cvtColor($src, COLOR_BGR2GRAY);
32+
$faceClassifier->detectMultiScale($gray, $faces);
33+
//var_export($faces);
34+
equalizeHist($gray, $gray);
35+
$faceImages = $faceLabels = [];
36+
foreach ($faces as $k => $face) {
37+
$faceImages[] = $gray->getImageROI($face); // face coordinates to image
38+
$faceLabels[] = 2; // Angelina
39+
//cv\imwrite("results/recognize_face_by_lbph_angelina$k.jpg", $gray->getImageROI($face));
40+
}
41+
$faceRecognizer->update($faceImages, $faceLabels);
2642

27-
//$faceRecognizer->write('results/lbph_model.xml');
28-
//$faceRecognizer->read('results/lbph_model.xml');
43+
//$faceRecognizer->write('results/lbph_model.xml');
44+
//$faceRecognizer->read('results/lbph_model.xml');
2945

30-
// test image
31-
$src = imread("images/2.jpg");
32-
$gray = cvtColor($src, COLOR_BGR2GRAY);
33-
equalizeHist($gray, $gray);
34-
$faceClassifier->detectMultiScale($gray, $faces);
35-
$mat = $gray->getImageROI($faces[0]);
46+
// test image
47+
$src = imread("images/angelina_and_me.png");
48+
$gray = cvtColor($src, COLOR_BGR2GRAY);
49+
$faceClassifier->detectMultiScale($gray, $faces);
50+
//var_export($faces);
51+
equalizeHist($gray, $gray);
52+
foreach ($faces as $face) {
53+
$faceImage = $gray->getImageROI($face);
3654

3755
//predict
38-
$faceLabel = $faceRecognizer->predict($gray, $faceConfidence);
56+
$faceLabel = $faceRecognizer->predict($faceImage, $faceConfidence);
3957
echo "{$faceLabel}, {$faceConfidence}\n";
58+
59+
$scalar = new \CV\Scalar(0, 0, 255);
60+
\CV\rectangleByRect($src, $face, $scalar, 2);
61+
62+
$text = $labels[$faceLabel];
63+
\CV\rectangle($src, $face->x, $face->y, $face->x + ($faceLabel == 1 ? 50 : 130), $face->y - 30, new Scalar(255,255,255), -2);
64+
\CV\putText($src, "$text", new Point($face->x, $face->y - 2), 0, 1.5, new Scalar(), 2);
4065
}
66+
67+
cv\imwrite("results/_recognize_face_by_lbph.jpg", $src);

0 commit comments

Comments
 (0)