sudo apt-get update sudo apt-get install build-essential cmake sudo apt-get install libgtk2.0-dev libjpeg-dev libtiff5-dev libjasper-dev libopenexr-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libatlas-base-dev gfortran python2.7-dev python3.6-dev
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.2.0.zip unzip opencv.zip
mkdir buildcd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j4 sudo make install sudo ldconfig
sudo apt-get install php7.4-dev
sudo apt-get install php-pear
sudo pecl install opencv
php.ini
文件中添加OpenCV扩展:extension=opencv.so
sudo service apache2 restart
index.php
)。use \OpenCV\{ CvImage, CvVideoCapture, CvWindow, CvFont, CvScalar};
function detectFace($inputImage){ // 加载人脸识别器和级联分类器 $faceCascade = new \OpenCV\CascadeClassifier(); $faceCascade->load('/path/to/haarcascade_frontalface_default.xml'); // 加载输入图像 $image = new CvImage(); $image->load($inputImage); // 将图像转换为灰度图像 $grayImage = $image->clone(); $grayImage->toGray(); // 检测人脸 $faces = $faceCascade->detectMultiScale($grayImage); // 在图像中绘制人脸矩形框 $rectColor = new CvScalar(0, 255, 0); foreach ($faces as $face) { $image->rectangle( $face->getX(), $face->getY(), $face->getX() + $face->getWidth(), $face->getY() + $face->getHeight(), $rectColor, 2 ); } // 在新窗口中显示图像 $window = new CvWindow('Face Detection'); $window->showImage($image); $window->waitKey(); }
detectFace
函数并传入要检测的图像路径:detectFace('/path/to/input/image.jpg');
function trainModel($trainingDataPath){ // 加载训练数据集 $images = []; $labels = []; $labelsMap = []; foreach (glob($trainingDataPath . '/*') as $dir) { $label = basename($dir); $labelsMap[] = $label; foreach (glob($dir . '/*.jpg') as $file) { $images[] = CvImage::load($file); $labels[] = count($labelsMap) - 1; } } // 创建人脸识别模型 $model = new \OpenCV\Face\FisherFaceRecognizer(); $model->train($images, $labels); // 保存模型 $model->save('/path/to/fisher_model.yml'); // 保存标签映射 file_put_contents('/path/to/labels.txt', implode(PHP_EOL, $labelsMap)); }
function recognizeFace($inputImage){ // 加载人脸识别模型 $model = new \OpenCV\Face\FisherFaceRecognizer(); $model->load('/path/to/fisher_model.yml'); // 加载标签映射 $labelsMap = file('/path/to/labels.txt', FILE_IGNORE_NEW_LINES); // 加载输入图像 $image = CvImage::load($inputImage); $grayImage = $image->clone(); $grayImage->toGray(); // 检测人脸 $faceCascade = new \OpenCV\CascadeClassifier(); $faceCascade->load('/path/to/haarcascade_frontalface_default.xml'); $faces = $faceCascade->detectMultiScale($grayImage); // 遍历所有检测到的人脸 foreach ($faces as $face) { // 提取人脸区域 $faceImage = $grayImage->getROI($face->getX(), $face->getY(), $face->getWidth(), $face->getHeight()); // 识别人脸 $predictedLabel = $model->predict($faceImage); // 在图像中绘制识别结果 $font = new CvFont(); $font->scale(1); $font->color(new CvScalar(0, 255, 0)); $image->putText( $labelsMap[$predictedLabel], new CvPoint($face->getX(), $face->getY() - 10), $font ); $image->rectangle( $face->getX(), $face->getY(), $face->getX() + $face->getWidth(), $face->getY() + $face->getHeight(), new CvScalar(0, 255, 0), 2 ); } // 在新窗口中显示图像 $window = new CvWindow('Face Recognition'); $window->showImage($image); $window->waitKey(); }
recognizeFace
函数并传入要识别的图像路径:recognizeFace('/path/to/input/image.jpg');
以上是PHP和OpenCV库:如何实现人脸识别的详细内容。更多信息请关注PHP中文网其他相关文章!