利用OpenCV进行PHP人脸识别

时间:2022-01-18 08:56:40

1.opencv下载安装

下载地址:http://opencv.org/downloads.html,注意:2.4.4版本采用的是cmake,所以需要事先安装cmake。

shell:

wget http://softlayer-dal.dl.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.4/OpenCV-2.4.4a.tar.bz2
tar xvf OpenCV-2.4.4a.tar.bz2 
cd opencv-2.4.4/ 
cmake ./
make
make install

2.安装facedetect

http://www.xarg.org/project/php-facedetect/
cd PHP-Facedetect-master
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
编译完之后会提示facedetect.so 文件所在的位置。
最后确认在php.ini加入
extension=facedetect.so,reload php-fpm.

3.安装imagemagick

http://www.imagemagick.org/script/install-source.php

wget http://www.imagemagick.org/download/ImageMagick.tar.gz

tar xvf ImageMagick.tar.gz 
cd ImageMagick-6.8.6-10/
./configure
make && make install
ldconfig /usr/local/lib

4.安装php的imagick扩展

http://pecl.php.net/package/imagick
wget http://pecl.php.net/get/imagick-3.1.1.tgz
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
Installing header files:          /usr/local/php/include/php/
最后确认在php.ini加入

extension=imagick.so,reload php-fpm.


5.php实例代码

从openCV源代码/data/haarcascades/里头取出所有xml文件放在php的执行目录下,此处我是放在data目录下:
<?php
if ($_FILES) {
	$imgName = $_FILES['pic']['tmp_name'];
	//返回多维数组,每个人脸的位置。
	$all = face_detect($imgName, 'data/haarcascade_frontalface_alt2.xml');
	// print_r($all);exit;
	$image = new Imagick($imgName);
	if (is_array($all)) {
		foreach ($all as $v) {
			$image->cropImage($v['w'], $v['h'], $v['x'], $v['y']);
		}
	}
	ob_clean();
	header("Content-Type: image/jpeg");
	echo $image;
	$image->destroy();
} 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="提交">
</form>
<?php
}
?>

参考网址:
http://blog.csdn.net/zhongmao/article/details/3753377