zl程序教程

您现在的位置是:首页 >  后端

当前栏目

基于OpenCV的PHP图像人脸识别技术

OpencvPHP技术 基于 图像 人脸识别
2023-06-13 09:14:13 时间
openCV是一个开源的用C/C++开发的计算机图形图像库,非常强大,研究资料很齐全。本文重点是介绍如何使用php来调用其中的局部的功能。人脸侦查技术只是openCV一个应用分支。
1.安装
从源代码编译成一个动态的so文件。
1.1.安装OpenCV(OpenCV1.0.0)
下载地址:http://sourceforge.net/project/showfiles.php?group_id=22870&package_id=16948
#tarxvzfOpenCV-1.0.0.tar.gz
#cdopencv-1.0.0
#./configure
#make
#makeinstall
#makecheck(检查是否安装全部正确)
提示:不要指定安装路径,否则后面编译facedetect会找不到OpenCV的路径。
1.2安装facedetect
下载地址http://www.xarg.org/download/facedetect-1.0.0.tar.gz
#tarxzvffacedetect-1.0.0.tar.gz
#cdfacedetect-1.0.0
#phpize&&./configure&&make&&makeinstall
编译完之后会提示facedetect.so文件所在的位置。
最后确认在php.ini加入
extension=facedetect.so,重启apache.
2.函数使用
在phpinfo()里检查是否有facedetect这个模块。
从openCV源代码/data/haarcascades/里头取出所有xml文件放在php的执行目录下
//检查有多少个脸型
var_dump(face_count(‘party.jpeg",haarcascade_frontalface_alt.xml"));
//返回脸型在图片中的位置参数,多个则返回数组
$arr=face_detect(‘party.jpeg",haarcascade_frontalface_alt2.xml");
print_r($arr);
3.应用
结合imagick可以将图片做一下应用。因为face_detect只返回一个矩形参数,包含x,y坐标和w,h长宽参数。下面是我的一个应用demo
复制代码代码如下:

<?php
if($_FILES){
$img=$_FILES["pic"]["tmp_name"];
$arr=face_detect($img,‘haarcascade_frontalface_alt2.xml");
//$arr1=face_detect($img,"haarcascade_frontalface_alt_tree.xml");
if(is_array($arr1))$all=array_merge($arr,$arr1);
else$all=$arr;
$im=newImagick($img);
//$draw=newImagickDraw();
//$borderColor=newImagickPixel("red");
//$draw->setFillAlpha(0.0);
//$draw->setStrokeColor($borderColor);
//$draw->setStrokeWidth(1);
if(is_array($all)){
foreach($allas$v){
$im_cl=$im->clone();
$im_cl->cropImage($v["w"],$v["h"],$v["x"],$v["y"]);
$im_cl->swirlImage(60);
$im->compositeImage($im_cl,Imagick::COMPOSITE_OVER,$v["x"],$v["y"]);
//$draw->rectangle($v["x"],$v["y"],$v["x"]+$v["w"],$v["y"]+$v["h"]);
//$im->drawimage($draw);
}
}
header(“Content-Type:image/png”);
echo$im;
}else{
?>
<metahttp-equiv=“Content-Type”content=“text/html;charset=utf-8″/>
<formmethod=“POST”enctype=“multipart/form-data”>
人脸识别试验:只支持jpg,png<br>
上传一张图片<inputtype=“file”name=“pic”>
<inputtype=“submit”value=“upload”>
</form>
<?
}
?>

参考资料:
http://www.xarg.org/2008/07/face-detection-with-php/
http://www.opencv.org.cn/index.php/首页
http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/index.html