Artificial Intelligence 5 min read

Real-Time Face Recognition Using PHP and OpenCV

This article explains how to set up a PHP environment with OpenCV, control a camera to capture images, perform real-time face detection using Haar cascades, train and apply an LBPH face recognizer, and integrate the results into a security system.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Real-Time Face Recognition Using PHP and OpenCV

Abstract: With advances in technology, face recognition is increasingly used in security; this article introduces how to use PHP to control a camera for real-time face recognition using the OpenCV library.

Introduction: Emphasizes the importance of safety and the efficiency of face recognition, focusing on implementing the solution with PHP.

Environment Setup: Ensure that PHP and the OpenCV library are properly installed, verify required PHP extensions (e.g., via php -m ), and download and configure OpenCV so it can be referenced from PHP.

Controlling the Camera with PHP: Demonstrates using the exec function to call the system raspistill command, capture an image, and display it on a web page.

<?php
function captureImage($filename) {
    exec("raspistill -o $filename");
}
function showImage($filename) {
    echo "
";
}
$filename = "captured.jpg";
captureImage($filename);
showImage($filename);
?>

Face Detection with OpenCV: Shows how to load a Haar‑cascade classifier, capture frames from the camera, convert them to grayscale, detect faces, and draw rectangles around detected regions.

<?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();
?>

Integrating Face Recognition: Describes training an LBPH recognizer with labeled images, using the trained model to predict identities for detected faces, annotating frames with the recognized names, and displaying the annotated video stream.

<?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();
?>

Conclusion: By combining PHP‑based camera control with OpenCV’s detection and LBPH recognition capabilities, a functional real‑time face recognition system can be built for security applications such as access control and monitoring, with room for further optimization and stability improvements.

real-timecomputer visionface recognitionPHPopencv
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.