|
| 1 | +<?php |
| 2 | +use CV\Mat; |
| 3 | +use CV\Face\FaceRecognizer; |
| 4 | +use CV\Face\LBPHFaceRecognizer; |
| 5 | +use CV\Face\BaseFaceRecognizer; |
| 6 | +use CV\Size; |
| 7 | +use CV\Scalar; |
| 8 | +use CV\Point; |
| 9 | +use CV\CascadeClassifier; |
| 10 | +use CV\VideoCapture; |
| 11 | +use function CV\{ |
| 12 | + normalize, imread, cvtColor, equalizeHist, rectangleByRect, imshow, waitKey, putText, imwrite, resize |
| 13 | +}; |
| 14 | +use const CV\{ |
| 15 | + NORM_MINMAX, CV_8UC1, CV_8UC3, IMREAD_GRAYSCALE, COLOR_BGR2GRAY, CV_HAAR_SCALE_IMAGE |
| 16 | +}; |
| 17 | + |
| 18 | +// 创建和返回一个归一化后的图像矩阵 |
| 19 | +function norm_0_255(Mat $src) |
| 20 | +{ |
| 21 | + $dst = null; |
| 22 | + switch ($src->channels()) { |
| 23 | + case 1: |
| 24 | + normalize($src, $dst, 0, 255, NORM_MINMAX, CV_8UC1); |
| 25 | + break; |
| 26 | + case 3: |
| 27 | + normalize($src, $dst, 0, 255, NORM_MINMAX, CV_8UC3); |
| 28 | + break; |
| 29 | + default: |
| 30 | + $src->copyTo($dst); |
| 31 | + break; |
| 32 | + } |
| 33 | + return $dst; |
| 34 | +} |
| 35 | + |
| 36 | +function read_csv($filename, $separator = ';') |
| 37 | +{ |
| 38 | + $images = []; |
| 39 | + $labels = []; |
| 40 | + $file = fopen($filename, "r"); |
| 41 | + while (!feof($file)) { |
| 42 | + $str = fgets($file);//fgets()函数从文件指针中读取一行 |
| 43 | + if ($str) { |
| 44 | + $array = explode($separator, $str); |
| 45 | + $images[] = imread($array[0], IMREAD_GRAYSCALE); |
| 46 | + $labels[] = intval($array[1]); |
| 47 | + } |
| 48 | + } |
| 49 | + fclose($file); |
| 50 | + return [$images, $labels]; |
| 51 | +} |
| 52 | + |
| 53 | +function run() |
| 54 | +{ |
| 55 | + $cvsPath = 'at.txt'; |
| 56 | + list($images, $labels) = read_csv($cvsPath); |
| 57 | + if (count($images) < 2) { |
| 58 | + die('至少需要两张图片'); |
| 59 | + } |
| 60 | + $faceRecognizer = LBPHFaceRecognizer::create(); |
| 61 | + $faceRecognizer->train($images, $labels);//识别器训练 |
| 62 | + $cascadeClassifier = new CascadeClassifier(); |
| 63 | + $cascadeClassifier->load('./haarcascade_frontalface_alt2.xml');//加载人脸识别分类器 |
| 64 | + $videoCapture = new VideoCapture(0);//打开默认摄像头 |
| 65 | + if (!$videoCapture->isOpened()) { |
| 66 | + die('摄像头开启失败。'); |
| 67 | + } |
| 68 | + |
| 69 | + $isStop = false; |
| 70 | + while (!$isStop) { |
| 71 | + $frame = null; |
| 72 | + $videoCapture->read($frame);//读取图像 |
| 73 | + $gray = cvtColor($frame, COLOR_BGR2GRAY);//转为灰度图 |
| 74 | + equalizeHist($gray, $gray); |
| 75 | + $faces = null; |
| 76 | + $cascadeClassifier->detectMultiScale($gray, $faces, 1.1, 2, CV_HAAR_SCALE_IMAGE, new Size(50, 50)); |
| 77 | + $face = null; |
| 78 | + $textLb = null; |
| 79 | + for ($i = 0; $i < count($faces); $i++) { |
| 80 | + if ($faces[$i]->height > 0 && $faces[$i]->width > 0) { |
| 81 | + $face = $gray->getImageROI($faces[$i]); |
| 82 | + $textLb = new Point($faces[$i]->x, $faces[$i]->y - 10); |
| 83 | + rectangleByRect($frame, $faces[$i], new Scalar(255, 0, 0), 1, 8, 0); |
| 84 | + $faceLabel = $faceRecognizer->predict($face); |
| 85 | + if ($faceLabel == 41) { |
| 86 | + $name = "hiho"; |
| 87 | + putText($frame, $name, $textLb, 3, 1, new Scalar(0, 0, 255)); |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + } |
| 92 | + imshow('frame', $frame); |
| 93 | + if (waitKey(50) >= 0) { |
| 94 | + $isStop = true; |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +run(); |
0 commit comments