Real-Time Face Recognition with PHP and OpenCV

This article demonstrates how to set up a PHP environment, control a camera, and integrate OpenCV for real-time face detection and recognition, providing code examples and a complete workflow to enhance security applications.

php Courses
php Courses
php Courses
Real-Time Face Recognition with PHP and OpenCV

With the advancement of technology, facial recognition is increasingly used in security domains; this article explains how to use PHP to control a camera for real-time face recognition by leveraging the OpenCV library.

The introduction outlines the importance of safety and the efficiency of facial recognition, emphasizing the goal of improving security through a PHP‑based solution.

Environment setup: ensure PHP and the OpenCV extension are installed, verify loaded extensions with php -m, and download the OpenCV library so it can be referenced in the project.

Controlling the camera with PHP is achieved by invoking system commands. 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);
?>

This script uses exec to call raspistill for capturing an image and then displays it on a web page.

Face detection with OpenCV involves loading the Haar cascade classifier and processing frames from the camera. 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 detects faces in each captured frame and draws rectangles around them.

Combining detection with recognition uses the LBPH algorithm to train a model and predict identities. 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, detects faces, predicts the label, and annotates the video stream with the identified name.

Conclusion: By integrating PHP 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 scenarios, with room for further optimization and stability improvements.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Computer Visionface recognitionPHP
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

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.