Using PHP to Access a Webcam and Perform Object (Face) Detection with OpenCV
This tutorial explains how to install OpenCV and php-facedetect, write PHP code to capture images from a webcam, perform face detection, and display the results, providing step‑by‑step commands and a complete example script.
Cameras have become ubiquitous in modern life, and one of their useful applications is object detection. This article introduces how to use the PHP language to call a webcam and perform object detection.
Before starting, ensure that PHP is installed and that a webcam is available. The following steps describe how to achieve object detection with PHP.
1. Install Required Libraries
To use PHP for object detection we first need to install some necessary libraries. In this guide we use OpenCV and the php-facedetect library. The commands below install these dependencies:
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-master2. Write PHP Code
Next we write PHP code that captures an image from the webcam and runs object (face) detection. The example below assumes we want to detect faces:
<?php
// Call the webcam
exec('fswebcam -d /dev/video0 -r 1280x720 --no-banner image.jpg');
// Load the image
$image = imagecreatefromjpeg('image.jpg');
// Set parameters
$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 the image
$croppedImage = imagecrop($image, ['x' => $x, 'y' => $y, 'width' => $size, 'height' => $size]);
// Save the cropped image
imagejpeg($croppedImage, 'cropped.jpg');
// Run the pico face‑detection library
exec('pico/picornt cropped.jpg face.txt');
// Parse detection results
$faceTxt = file_get_contents('face.txt');
$faceTxt = explode("\n", $faceTxt);
$faceCount = count($faceTxt);
// Draw rectangles around detected faces
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 code first captures an image from the webcam and saves it as image.jpg . It then crops the central region using OpenCV, saves it as cropped.jpg , runs the pico face‑detection program, stores results in face.txt , parses those results, draws green rectangles around detected faces on the original image, and finally displays the annotated image.
3. Run the Code
Save the above script as detection.php and execute it in a terminal with:
php detection.phpIf everything works, you will see the webcam capture with green boxes around any detected faces.
Summary
Using PHP to call a webcam for object detection is an interesting capability. This article demonstrated how to achieve face detection by leveraging OpenCV and the php‑facedetect library, providing a complete example script. The code can be adapted to detect other objects as needed.
Note that this example is simplified for demonstration purposes; real‑world projects will require additional logic and more sophisticated algorithms for accurate detection. We hope this guide inspires you and wish you success!
PHP8 Video Tutorial
Scan the QR code to receive free learning materials
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.