Using PHP and OpenCV for Real-Time Camera Image Processing
This tutorial explains how PHP developers can install OpenCV and the php‑opencv extension, capture video from a webcam, display live frames in a browser, and perform basic real‑time image processing such as face detection using Haar cascades.
1. Install required software and drivers
To call a camera from PHP you first need OpenCV and the php‑opencv extension. On Windows you can download the latest OpenCV binaries from the official site and compile/install the php‑opencv plugin from its GitHub repository following the documentation.
1.1 Install OpenCV
Download the appropriate OpenCV package for your OS from https://opencv.org/ and complete the installation.
1.2 Install php‑opencv plugin
The php‑opencv extension provides PHP bindings for OpenCV. Clone the source from https://github.com/opencv/opencv_contrib, compile and install it according to the official guide.
2. Capture camera and display real‑time image
After the software is installed, you can write PHP code that opens the default camera, reads frames, encodes them as BMP, and outputs them as base64‑encoded tags so the browser shows the live video.
<?php
$video = new VideoCapture(0); // Open default camera
while (true) {
$frame = $video->read(); // Read a frame
if ($frame !== null) {
$image = cvimencode(".bmp", $frame); // Encode frame
echo "<img src=\"data:image/bmp;base64," . base64_encode($image) . "\"/>"; // Display image
}
if (waitKey(1) >= 0) { // Exit on any key press
break;
}
}
$video->release(); // Release camera resource
?>The script uses VideoCapture, reads frames in a loop, encodes each frame, echoes an tag, and exits when any key is pressed, releasing the camera resource.
3. Real‑time image processing
Beyond simply displaying frames you can process them, for example performing face detection with OpenCV’s Haar cascade. The code converts each frame to grayscale, applies histogram equalization, detects faces, draws rectangles around them, and then displays the processed image.
<?php
$video = new VideoCapture(0); // Open default camera
$cascade = new CascadeClassifier('haarcascade_frontalface_default.xml'); // Load face model
while (true) {
$frame = $video->read(); // Read a frame
if ($frame !== null) {
$gray = cvcvtColor($frame, cvCOLOR_BGR2GRAY); // Convert to gray
cvequalizeHist($gray, $gray); // Histogram equalization
$faces = $cascade->detectMultiScale($gray); // Detect faces
foreach ($faces as $face) {
cvectangle($frame, $face, new Scalar(0, 255, 0)); // Draw rectangle
}
$image = cvimencode(".bmp", $frame); // Encode frame
echo "<img src=\"data:image/bmp;base64," . base64_encode($image) . "\"/>"; // Display image
}
if (waitKey(1) >= 0) { // Exit on any key press
break;
}
}
$video->release(); // Release camera resource
?>This tutorial provides a basic, entry‑level example of using PHP with OpenCV for real‑time camera access and image processing, which can be extended with more advanced algorithms.
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.