openCV是一个开源的用C/C++开发的计算机图形图像库,非常强大,研究资料很齐全。本文重点是介绍如何使用php来调用其中的局部的功能。人脸侦查技术只是openCV一个应用分支。
1.安装
从源代码编译成一个动态的so文件。
1.1.安装 OpenCV (OpenCV 1.0.0)
下载地址:http://sourceforge.net/project/showfiles.php?group_id=22870&package_id=16948
#tar xvzf OpenCV-1.0.0.tar.gz
#cd opencv-1.0.0
#./configure
#make
#make install
#make check (检查是否安装全部正确)
提示: 不要指定安装路径,否则后面编译facedetect会找不到OpenCV的路径。
1.2 安装facedetect
下载地址http://www.xarg.org/download/facedetect-1.0.0.tar.gz
#tar xzvf facedetect-1.0.0.tar.gz
#cd facedetect-1.0.0
#phpize && ./configure && make && make install
编译完之后会提示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 = new Imagick($img);
//$draw =new ImagickDraw();
//$borderColor = new ImagickPixel('red');
//$draw->setFillAlpha(0.0);
//$draw->setStrokeColor ($borderColor);
//$draw->setStrokeWidth (1);
if(is_array($all)){
foreach ($all as $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{
?>
<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8″ />
<form method=“POST” enctype=“multipart/form-data”>
人脸识别试验:只支持jpg,png<br>
上传一张图片 <input type=“file” name=“pic”>
<input type=“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

以上就是【基于OpenCV的PHP图像人脸识别技术】的全部内容了,欢迎留言评论进行交流!

赞(0) 踩(0)
发表我的评论

最新评论

  1. 暂无评论