Artificial Intelligence 6 min read

Real-Time Face Recognition with PHP and OpenCV

This article explains how to set up a PHP environment, control a camera, and use the OpenCV library to perform real-time face detection and recognition with code examples, enabling security applications such as access control and monitoring systems.

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

Abstract

With advances in technology, facial recognition is increasingly used in security; this article demonstrates how to control a camera with PHP and apply OpenCV for real‑time face detection and recognition, providing code samples.

Introduction

Ensuring public safety is critical, and facial recognition offers efficient, accurate solutions across industries. The focus here is on using PHP to manage a camera and integrate OpenCV for live face recognition to enhance security.

1. Environment Setup

Before starting, ensure PHP and the OpenCV library are correctly installed. Verify PHP extensions with php -m , then download and install OpenCV so it can be referenced by the system.

2. Controlling the Camera with PHP

PHP can invoke system commands to operate a camera. The following example captures an image and displays it on a web page:

<?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 code uses exec to run the raspistill command, captures a photo, and outputs it as an HTML image.

3. Face Detection with OpenCV

OpenCV provides powerful computer‑vision functions, including Haar‑cascade face detection. After installing the OpenCV PHP extension, the following script detects faces in a video frame and draws rectangles around them:

<?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 script loads the Haar cascade, captures a frame, converts it to grayscale, detects faces, and marks each detected face with a rectangle.

4. Integrating Face Recognition

Before real‑time recognition, a model must be trained. Using OpenCV’s LBPH algorithm, the following code trains a recognizer with sample images and then identifies faces in the live stream, labeling them as "Tom" or "Jane":

<?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 recognizer predicts a label for each detected face, draws a rectangle, and overlays the corresponding name.

Conclusion

By combining PHP‑controlled camera access 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. Ongoing learning and refinement will improve stability and accuracy.

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.