Skip to content

Commit 18aa931

Browse files
committed
faceRecognition
1 parent e3ffa59 commit 18aa931

File tree

7 files changed

+20951
-1
lines changed

7 files changed

+20951
-1
lines changed

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
.idea
1+
.idea
2+
faceRecognition/at.txt
3+
faceRecognition/att_faces
4+
faceRecognition/myFaces
5+
faceRecognition/myPics
6+
7+
!.gitkeep

faceRecognition/att_faces.zip

3.59 MB
Binary file not shown.

faceRecognition/faceRecognition.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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();

faceRecognition/generateCSV.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
function searchDir($path, &$data)
4+
{
5+
//判断是否未文件夹
6+
if (is_dir($path)) {
7+
$dp = dir($path);//
8+
while ($file = $dp->read()) {
9+
if ($file != '.' && $file != '..') {
10+
searchDir($path . '/' . $file, $data);
11+
}
12+
}
13+
$dp->close();
14+
}
15+
//判断是否未文件
16+
if (is_file($path)) {
17+
$data[] = $path;//加到data数组中
18+
}
19+
}
20+
21+
function getDir($dir)
22+
{
23+
$data = array();
24+
searchDir($dir, $data);
25+
return $data;
26+
}
27+
28+
29+
$paths = getDir('./att_faces');
30+
//var_dump($paths);
31+
$fp = fopen("at.txt", "w");
32+
$pwdPath = realpath('./att_faces');
33+
$strStartLen = strlen($pwdPath . "/s");
34+
sort($paths, SORT_STRING);
35+
$i = 0;
36+
$oldNum = -1;
37+
foreach ($paths as $key => $path) {
38+
$realpath = realpath($path);
39+
$info = explode("/", $realpath);
40+
$num = count($info) - 1;
41+
$filename = $info[$num];
42+
if (strpos($filename, ".pgm") || strpos($filename, ".jpg")) {
43+
$endLen = strpos($realpath, '/' . $filename);
44+
$num = substr($realpath, $strStartLen, $endLen - $strStartLen);
45+
$flag = fwrite($fp, $realpath . ';' . $num . "\r\n");
46+
}
47+
}
48+
fclose($fp);

0 commit comments

Comments
 (0)