How to Use PHP and OpenCV for Real-Time Face Detection with a Webcam

This guide walks through installing OpenCV and php-facedetect, writing PHP code to capture webcam images, crop them, run Pico face detection, and overlay bounding boxes, providing a complete example for object detection using PHP.

php Courses
php Courses
php Courses
How to Use PHP and OpenCV for Real-Time Face Detection with a Webcam

Cameras are ubiquitous, and PHP can be used to perform object detection by leveraging OpenCV and the php-facedetect library. The tutorial assumes PHP is installed and the webcam is accessible.

1. Install Required Libraries

Install OpenCV and unzip, then download the pico face detection repository and the php-facedetect library:

sudo apt-get install python-opencv
sudo apt-get install unzip
wget https://github.com/nenadmarkus/pico
cd pico
unzip master.zip
cd /path/to/php-facedetect-master

2. Write the PHP Script

The script captures an image from the webcam, crops the central region, runs Pico face detection, parses the results, and draws green rectangles around detected faces.

<?php
// Capture image from webcam
exec('fswebcam -d /dev/video0 -r 1280x720 --no-banner image.jpg');

// Load image
$image = imagecreatefromjpeg('image.jpg');

// Set scaling factor
$scale = 4;
$w = imagesx($image) / $scale;
$h = imagesy($image) / $scale;
$size = min($w, $h);
$x = (imagesx($image) - $size) / 2;
$y = (imagesy($image) - $size) / 2;

// Crop image
$croppedImage = imagecrop($image, ['x'=>$x,'y'=>$y,'width'=>$size,'height'=>$size]);
imagejpeg($croppedImage, 'cropped.jpg');

// Run Pico face detection
exec('pico/picornt cropped.jpg face.txt');

// Parse detection results
$faceTxt = file_get_contents('face.txt');
$faceTxt = explode("
", $faceTxt);
$faceCount = count($faceTxt);

for ($i = 0; $i < $faceCount - 1; $i++) {
    $faceData = explode(' ', $faceTxt[$i]);
    $x = $faceData[0] * $scale;
    $y = $faceData[1] * $scale;
    $width = $faceData[2] * $scale;
    $height = $faceData[3] * $scale;
    imagerectangle($image, $x, $y, $x + $width, $y + $height, imagecolorallocate($image, 0, 255, 0));
}

// Output the image
header('Content-Type: image/jpeg');
imagejpeg($image);

// Clean up temporary files
unlink('image.jpg');
unlink('cropped.jpg');
unlink('face.txt');
?>

The script first captures an image (image.jpg), crops the center to reduce processing size, and saves it as cropped.jpg. It then calls the Pico face detection binary, which writes face coordinates to face.txt. The PHP code reads these coordinates, scales them back, and draws rectangles on the original image.

3. Run the Script

Save the code as detection.php and execute: php detection.php If everything works, the browser will display the webcam image with green boxes around detected faces.

Conclusion

Using PHP with OpenCV and php-facedetect enables simple webcam-based object detection. While the example focuses on face detection, the approach can be adapted to other objects by swapping the detection model. Note that this demo is minimal; production use may require more robust error handling and optimized algorithms.

object detectionOpenCVFace DetectionWebcamphp-facedetect
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.