代码拉取完成,页面将自动刷新
同步操作将从 带刀医生/php-opencv 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
<?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);
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。