Implementing Face Recognition with PHP and OpenCV
This article provides a step‑by‑step tutorial on installing OpenCV and the PHP OpenCV extension on Ubuntu, then demonstrates how to write PHP code for face detection and recognition using OpenCV's cascade classifier and FisherFaceRecognizer, complete with example scripts and usage instructions.
Face recognition uses computer‑vision techniques to automatically identify and verify faces. This guide shows how to implement face recognition using PHP and the OpenCV library, covering installation of required software, configuration of the PHP extension, and detailed coding steps.
Preparation : Install necessary dependencies, download and build OpenCV, and install the PHP OpenCV extension.
Install OpenCV (Ubuntu):
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 build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j4
sudo make install
sudo ldconfigInstall PHP extension :
sudo apt-get install php7.4-dev
sudo apt-get install php-pear
sudo pecl install opencv
# Add to php.ini
extension=opencv.so
sudo service apache2 restartFace detection :
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('/path/to/input/image.jpg');Face recognition involves creating a labeled training dataset, training a FisherFaceRecognizer model, and then using it to predict faces.
Training model example:
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));
}Recognition function example:
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('/path/to/input/image.jpg');Conclusion : By installing OpenCV and the PHP extension, then using the provided detection and recognition functions, developers can quickly build face‑recognition applications in PHP. The article includes full code snippets and installation steps to help readers get started.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.