程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 基於OpenCV的PHP圖像人臉識別技術

基於OpenCV的PHP圖像人臉識別技術

編輯:關於PHP編程

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved