Using PHP and OpenCV for Camera‑Based Object Detection
This tutorial explains how to install required libraries, write PHP code that captures images from a webcam, uses OpenCV and php‑facedetect to detect faces, and displays the results with annotated bounding boxes, providing a foundation for further object detection projects.
Cameras have become very common in modern life. We can use cameras for various operations, one of which is object detection. This article introduces how to use PHP to access a camera and perform object detection.
Before starting, ensure PHP is installed and the camera is accessible. Below are the steps to perform object detection with PHP:
1. Install related libraries
To use PHP for object detection, we first need to install some necessary libraries. Here we will use OpenCV and the php‑facedetect library. They can be installed with the following commands:
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 can write PHP code to call the camera and perform object detection. The following is a sample code assuming we want to detect faces:
<?php
// Call the camera
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');
// Call pico face detection library
exec('pico/picornt cropped.jpg face.txt');
// Parse face detection results
$faceTxt = file_get_contents('face.txt');
$faceTxt = explode("\n", $faceTxt);
$faceCount = count($faceTxt);
// Draw detected face boxes on the original image
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 camera and saves it as image.jpg . It then uses OpenCV to crop the central region, saving it as cropped.jpg . Next, it runs the pico face detection program from the php‑facedetect library, feeding the processed image and storing results in face.txt . Finally, it parses the results, draws green rectangles around detected faces on the original image, and displays the image.
3. Run the code
Save the above code as detection.php , then execute the following command in a terminal: php detection.php . If everything runs correctly, you will see the webcam image with green boxes around detected faces.
Summary
Using PHP to call a camera for object detection is an interesting capability. This article showed how to use OpenCV and the php‑facedetect library to achieve face detection, providing sample code that can be adapted to detect other objects.
Please note that this sample code is for demonstration only and is not production‑ready. Real projects may require additional logic and more sophisticated algorithms for accurate detection. We hope this guide inspires you and wish you success!
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.