Real-Time Face Recognition with PHP and OpenCV
This article demonstrates how to set up a PHP environment with OpenCV, control a camera to capture images, and implement real-time face detection and recognition using Haar cascades and LBPH algorithms, providing code examples for building a security-oriented facial recognition system.
Abstract: With advances in technology, facial recognition is increasingly applied in security. This article explains how to use PHP to control a camera for real‑time face recognition, leveraging the OpenCV library for detection and recognition.
Introduction: Ensuring public safety is crucial, and facial recognition offers efficient, accurate solutions across industries. The focus here is on using PHP to manage a camera and perform live face recognition to enhance security.
1. Environment Setup: Ensure PHP and the OpenCV extension are installed. Verify PHP extensions with php -m , then download and install OpenCV so that its libraries can be referenced in PHP scripts.
2. Controlling the Camera with PHP: Camera control is achieved by invoking system commands via PHP. Example code:
<?php
function captureImage($filename) {
exec("raspistill -o $filename");
}
function showImage($filename) {
echo "<img src='$filename' alt='captured image'>";
}
$filename = "captured.jpg";
captureImage($filename);
showImage($filename);
?>The script uses exec to run the raspistill command, captures an image, and displays it on a web page.
3. Face Detection with OpenCV: OpenCV provides powerful computer‑vision functions, including Haar‑cascade face detection. Example code:
<?php
$faceCascade = new CvCascade();
$faceCascade->load("haarcascade_frontalface_default.xml");
$camera = new CvCapture();
$frame = $camera->queryFrame();
$gray = $frame->convertColor(CV_BGR2GRAY);
$faces = $faceCascade->detectMultiScale($gray);
foreach ($faces as $face) {
$frame->rectangle($face->x, $face->y, $face->x + $face->width, $face->y + $face->height);
}
$frame->showImage();
?>This code loads a Haar cascade, captures a frame, converts it to grayscale, detects faces, draws rectangles around them, and displays the result.
4. Integrating Face Recognition (LBPH): Before real‑time recognition, a model must be trained. The LBPH algorithm is used for training and prediction. Example code:
<?php
$images = glob("train_images/*.jpg");
$labels = [0, 0, 1, 1]; // training labels
$lbph = new CvLBPHFaceRecognizer();
$lbph->train($images, $labels);
$faceCascade = new CvCascade();
$faceCascade->load("haarcascade_frontalface_default.xml");
$camera = new CvCapture();
$frame = $camera->queryFrame();
$gray = $frame->convertColor(CV_BGR2GRAY);
$faces = $faceCascade->detectMultiScale($gray);
foreach ($faces as $face) {
$recognizedLabel = $lbph->predict($gray);
if ($recognizedLabel == 0) {
$label = "Tom";
} else {
$label = "Jane";
}
$frame->rectangle($face->x, $face->y, $face->x + $face->width, $face->y + $face->height);
$frame->putText($label, new CvPoint($face->x, $face->y - 20), new CvFont(CV_FONT_HERSHEY_SIMPLEX, 1, 1));
}
$frame->showImage();
?>The script trains an LBPH recognizer with labeled images, detects faces in each captured frame, predicts the label, annotates the frame with the identified name, and displays the annotated image.
Conclusion: By combining PHP‑based camera control with OpenCV’s detection and LBPH recognition, a functional real‑time facial recognition system can be built, suitable for access control, surveillance, and other security applications. Ongoing refinement and testing can improve stability and accuracy.
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.