fixed recognize example and phpdoc
Esse commit está contido em:
@@ -7,7 +7,6 @@ use const CV\{COLOR_BGR2GRAY};
|
||||
|
||||
$src = imread("images/faces.jpg");
|
||||
$gray = cvtColor($src, COLOR_BGR2GRAY);
|
||||
equalizeHist($gray, $gray);
|
||||
|
||||
// face by lbpcascade_frontalface
|
||||
$faceClassifier = new CascadeClassifier();
|
||||
@@ -40,4 +39,4 @@ if ($eyes) {
|
||||
}*/
|
||||
|
||||
|
||||
$src = imwrite("results/_detect_face_by_cascade_classifier.jpg", $src);
|
||||
imwrite("results/_detect_face_by_cascade_classifier.jpg", $src);
|
||||
|
||||
Arquivo binário não exibido.
|
Depois Largura: | Altura: | Tamanho: 190 KiB |
Arquivo binário não exibido.
|
Depois Largura: | Altura: | Tamanho: 344 KiB |
+27
-27
@@ -150,7 +150,7 @@ class Mat {
|
||||
|
||||
/**
|
||||
* @param array $rect
|
||||
* @return Mat $mat
|
||||
* @return Mat $image
|
||||
*/
|
||||
public function getImageROI(Rect $rect) {
|
||||
|
||||
@@ -412,7 +412,7 @@ class CascadeClassifier {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Mat $mat
|
||||
* @param Mat $image
|
||||
* @param array $objects
|
||||
* @param float $scale_factor
|
||||
* @param int $min_neighbors
|
||||
@@ -421,7 +421,7 @@ class CascadeClassifier {
|
||||
* @param Size|null $maxSize
|
||||
* @return null
|
||||
*/
|
||||
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) {
|
||||
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) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -437,24 +437,24 @@ function imread(string $filename, $flags = null) {
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
* @param Mat $mat
|
||||
* @param Mat $image
|
||||
* @return true|null
|
||||
*/
|
||||
function imwrite(string $filename, Mat $mat) {
|
||||
function imwrite(string $filename, Mat $image) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Mat $mat
|
||||
* @param Mat $image
|
||||
* @param $code
|
||||
* @return Mat|null
|
||||
*/
|
||||
function cvtColor(Mat $mat, $code, $dstCn = null) {
|
||||
function cvtColor(Mat $image, $code, $dstCn = null) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Mat $mat
|
||||
* @param Mat $image
|
||||
* @param Point $startPoint
|
||||
* @param Point $endPoint
|
||||
* @param Scalar $color
|
||||
@@ -463,12 +463,12 @@ function cvtColor(Mat $mat, $code, $dstCn = null) {
|
||||
* @param int $shift
|
||||
* @return null
|
||||
*/
|
||||
function line(Mat $mat, Point $startPoint, Point $endPoint, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
|
||||
function line(Mat $image, Point $startPoint, Point $endPoint, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Mat $mat
|
||||
* @param Mat $image
|
||||
* @param Point $point
|
||||
* @param int $radius
|
||||
* @param Scalar $color
|
||||
@@ -477,12 +477,12 @@ function line(Mat $mat, Point $startPoint, Point $endPoint, Scalar $color, int $
|
||||
* @param int $shift
|
||||
* @return null
|
||||
*/
|
||||
function circle(Mat $mat, Point $point, int $radius, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
|
||||
function circle(Mat $image, Point $point, int $radius, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Mat $mat
|
||||
* @param Mat $image
|
||||
* @param Point $point
|
||||
* @param Size $size
|
||||
* @param int $angle
|
||||
@@ -494,12 +494,12 @@ function circle(Mat $mat, Point $point, int $radius, Scalar $color, int $thickne
|
||||
* @param int $shift
|
||||
* @return null
|
||||
*/
|
||||
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) {
|
||||
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) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Mat $mat
|
||||
* @param Mat $image
|
||||
* @param int $startX
|
||||
* @param int $startY
|
||||
* @param int $endX
|
||||
@@ -510,12 +510,12 @@ function ellipse(Mat $mat, Point $point, Size $size, int $angle, int $startAngle
|
||||
* @param int $shift
|
||||
* @return null
|
||||
*/
|
||||
function rectangle(Mat $mat, int $startX, int $startY, int $endX, int $endY, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
|
||||
function rectangle(Mat $image, int $startX, int $startY, int $endX, int $endY, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Mat $mat
|
||||
* @param Mat $image
|
||||
* @param Point $startPoint
|
||||
* @param Point $endPoint
|
||||
* @param Scalar $color
|
||||
@@ -524,12 +524,12 @@ function rectangle(Mat $mat, int $startX, int $startY, int $endX, int $endY, Sca
|
||||
* @param int $shift
|
||||
* @return null
|
||||
*/
|
||||
function rectangleByPoint(Mat $mat, Point $startPoint, Point $endPoint, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
|
||||
function rectangleByPoint(Mat $image, Point $startPoint, Point $endPoint, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Mat $mat
|
||||
* @param Mat $image
|
||||
* @param Rect $rect
|
||||
* @param Scalar $color
|
||||
* @param int $thickness
|
||||
@@ -537,12 +537,12 @@ function rectangleByPoint(Mat $mat, Point $startPoint, Point $endPoint, Scalar $
|
||||
* @param int $shift
|
||||
* @return null
|
||||
*/
|
||||
function rectangleByRect(Mat $mat, Rect $rect, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
|
||||
function rectangleByRect(Mat $image, Rect $rect, Scalar $color, int $thickness = 1, int $lineType = null, int $shift = 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Mat $mat
|
||||
* @param Mat $image
|
||||
* @param string $text
|
||||
* @param Point $point
|
||||
* @param int $fontFace
|
||||
@@ -553,21 +553,21 @@ function rectangleByRect(Mat $mat, Rect $rect, Scalar $color, int $thickness = 1
|
||||
* @param bool $bottomLeftOrigin
|
||||
* @return null
|
||||
*/
|
||||
function putText(Mat $mat, string $text, Point $point, int $fontFace, float $fontScale, Scalar $color, int $thickness = 1, int $lineType = null, bool $bottomLeftOrigin = false) {
|
||||
function putText(Mat $image, string $text, Point $point, int $fontFace, float $fontScale, Scalar $color, int $thickness = 1, int $lineType = null, bool $bottomLeftOrigin = false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Mat $mat
|
||||
* @param Mat $image
|
||||
* @param Mat $dst
|
||||
* @return null
|
||||
*/
|
||||
function equalizeHist(Mat $mat, Mat &$dst) {
|
||||
function equalizeHist(Mat $image, Mat &$dst) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Mat $mat
|
||||
* @param Mat $image
|
||||
* @param Mat $dst
|
||||
* @param Size $size
|
||||
* @param float $fx
|
||||
@@ -575,7 +575,7 @@ function equalizeHist(Mat $mat, Mat &$dst) {
|
||||
* @param int $interpolation
|
||||
* @return null
|
||||
*/
|
||||
function resize(Mat $mat, Mat &$dst, Size $size, float $fx = 0, float $fy = 0, int $interpolation = 1) {
|
||||
function resize(Mat $image, Mat &$dst, Size $size, float $fx = 0, float $fy = 0, int $interpolation = 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1000,12 +1000,12 @@ class FacemarkLBF {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Mat $mat
|
||||
* @param Mat $image
|
||||
* @param array $faces
|
||||
* @param array $landmarks
|
||||
* @return null
|
||||
*/
|
||||
public function fit(Mat $mat, array $faces, array &$landmarks) {
|
||||
public function fit(Mat $image, array $faces, array &$landmarks) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +1,67 @@
|
||||
<?php
|
||||
|
||||
use CV\Face\LBPHFaceRecognizer, CV\CascadeClassifier;
|
||||
use CV\Face\LBPHFaceRecognizer, CV\CascadeClassifier, CV\Scalar, CV\Point;
|
||||
use function CV\{imread, cvtColor, equalizeHist};
|
||||
use const CV\{COLOR_BGR2GRAY};
|
||||
|
||||
$src = imread("images/faces.jpg");
|
||||
$gray = cvtColor($src, COLOR_BGR2GRAY);
|
||||
equalizeHist($gray, $gray);
|
||||
|
||||
// face by lbpcascade_frontalface
|
||||
$faceClassifier = new CascadeClassifier();
|
||||
$faceClassifier->load('models/lbpcascades/lbpcascade_frontalface.xml');
|
||||
$faces = null;
|
||||
|
||||
$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);
|
||||
|
||||
if ($faces) {
|
||||
$faceRecognizer = LBPHFaceRecognizer::create();
|
||||
// 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);
|
||||
|
||||
//train
|
||||
foreach ($faces as $k => $face) {
|
||||
$mat = $gray->getImageROI($face);
|
||||
$faceRecognizer->update([$mat], [$k+1]);
|
||||
}
|
||||
//$faceRecognizer->write('results/lbph_model.xml');
|
||||
//$faceRecognizer->read('results/lbph_model.xml');
|
||||
|
||||
//$faceRecognizer->write('results/lbph_model.xml');
|
||||
//$faceRecognizer->read('results/lbph_model.xml');
|
||||
|
||||
// test image
|
||||
$src = imread("images/2.jpg");
|
||||
$gray = cvtColor($src, COLOR_BGR2GRAY);
|
||||
equalizeHist($gray, $gray);
|
||||
$faceClassifier->detectMultiScale($gray, $faces);
|
||||
$mat = $gray->getImageROI($faces[0]);
|
||||
// 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($gray, $faceConfidence);
|
||||
$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);
|
||||
|
||||
Referência em uma Nova Issue
Bloquear um usuário