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, demonstrating a practical security solution for applications such as access control and surveillance.

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

Abstract

With advances in technology, facial recognition is increasingly applied in security; this article demonstrates how to control a camera with PHP for real‑time face recognition using the OpenCV library, including code examples for detection and recognition.

Introduction

Ensuring public safety is a critical task, and facial recognition offers efficient, accurate solutions across industries. This article focuses on using PHP to control a camera for real‑time facial 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 that its functions can be referenced from PHP.

2. Controlling the Camera with PHP

PHP can control the camera by executing system commands. The following example captures an image with raspistill and displays it on a web page.

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

The script uses exec to call raspistill , saves the image, and outputs an <img> tag.

3. Face Detection with OpenCV

OpenCV provides powerful computer‑vision functions, including Haar‑cascade face detection. After installing the OpenCV PHP extension, the code below loads the cascade, captures a frame, converts it to grayscale, detects faces, 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 code demonstrates loading haarcascade_frontalface_default.xml , detecting faces, and marking them on the frame.

4. Integrating a Face Recognition Algorithm

Before real‑time recognition, a face model must be trained. Using OpenCV’s LBPH (Local Binary Patterns Histograms) algorithm, the script trains on a set of images, then predicts labels for detected faces, annotating the frame with the identified name.

<?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 example trains the recognizer, captures frames, predicts labels, and overlays the name on each detected face.

Conclusion

By combining PHP camera control with OpenCV for detection and LBPH for recognition, a real‑time facial recognition system can be built to improve security in scenarios such as access control and surveillance, with further refinements possible for stability and accuracy.

real-timecomputer visionsecurityface 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.