PHP calls the camera for face recognition: Exploration from basics to application
Abstract: With the development of artificial intelligence technology, face recognition has become an important application. This article will introduce how to use PHP to call the camera for face recognition and provide relevant code examples.
Introduction:
Face recognition is an identity recognition technology based on facial biometrics, which can be widely used in security monitoring, face payment, face access control and other fields. With the popularity of smartphones and smart devices, facial recognition technology has begun to develop rapidly in the mobile field. This article will introduce how to use PHP to call the camera for face recognition and implement some simple applications.
1. Preparation
Installing OpenCV
OpenCV is an open source computer vision library that provides a wealth of image processing and computer vision related functions. To use OpenCV in PHP, you need to install the OpenCV PHP extension first. You can use the following command to install:
sudo apt-get install php7.4-opencv
2. Calling the camera with PHP
Using PHP to call the camera requires the use of the interface function provided by the OpenCV library. The following is a simple PHP code example for calling the camera and displaying live footage.
<?php // 创建一个新的画布 $canvas = imagecreatetruecolor(640, 480); // 创建一个摄像头对象 $camera = cvCreateCameraCapture(0); while (true) { // 从摄像头中读取一帧图像 $frame = cvQueryFrame($camera); // 将图像绘制到画布上 imagejpeg($canvas, 'tmp.jpg'); // 显示图像 echo "<img src="tmp.jpg" />"; // 按下ESC键退出循环 $key = ord(cvWaitKey(1)); if ($key == 27) { break; } } // 释放摄像头和画布资源 cvReleaseCapture($camera); imagedestroy($canvas); ?>
Running the above code will display a web page that displays the footage captured by the camera in real time. Press the ESC key to exit the program.
3. Face recognition
Before introducing face recognition, we also need to install a PHP extension-FaceRecognizer. You can install it using the following command:
sudo apt-get install php7.4-faceRecognizer
The following is a simple sample code for face recognition:
<?php // 加载人脸识别模型 $model = cvLoad("/path/to/model.xml"); // 创建一个人脸分类器 $faceCascade = cvLoadHaarClassifierCascade("/path/to/haarcascade_frontalface_default.xml"); // 从摄像头中读取一帧图像 $frame = cvQueryFrame($camera); // 转换为灰度图像 $gray = cvCreateImage(cvGetSize($frame), IPL_DEPTH_8U, 1); cvCvtColor($frame, $gray, CV_RGB2GRAY); // 检测人脸 $faces = cvHaarDetectObjects($gray, $faceCascade, $model, 1.1, 2, CV_HAAR_SCALE_IMAGE, cvSize(30, 30)); // 绘制人脸区域 foreach ($faces as $face) { cvRectangle($frame, $face->x, $face->y, $face->x + $face->width, $face->y + $face->height, CV_RGB(0, 255, 0), 3); } // 显示图像 imagejpeg($canvas, 'tmp.jpg'); echo "<img src="tmp.jpg" />"; // 释放资源 cvReleaseImage($gray); cvReleaseImage($frame); cvReleaseHaarClassifierCascade($faceCascade); cvReleaseCapture($camera); ?>
The above code will detect the face in the picture captured by the camera and use Green box marks it. Some further processing can be done according to actual needs, such as determining the emotion and gender of the face.
Conclusion:
This article introduces how to use PHP to call the camera for face recognition. Readers can further develop and improve their own face recognition applications based on code examples and specific needs. With the continuous development of artificial intelligence technology, it is believed that face recognition will play an important role in more fields.
The above is the detailed content of PHP calls the camera for face recognition: Exploration from basics to application. For more information, please follow other related articles on the PHP Chinese website!