基于OpenCV的PHP圖像人臉識別技術(shù)
更新時間:2009年10月11日 15:14:39 作者:
本文所介紹的技術(shù)不是原創(chuàng),而是從一個叫Robert Eisele的德國人那里學(xué)習(xí)來的。他寫了一個PHP擴展openCV,只封裝了兩個函數(shù),叫face_detect和face_count。
openCV是一個開源的用C/C++開發(fā)的計算機圖形圖像庫,非常強大,研究資料很齊全。本文重點是介紹如何使用php來調(diào)用其中的局部的功能。人臉偵查技術(shù)只是openCV一個應(yīng)用分支。
1.安裝
從源代碼編譯成一個動態(tài)的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.函數(shù)使用
在phpinfo()里檢查是否有facedetect這個模塊。
從openCV源代碼/data/haarcascades/里頭取出所有xml文件放在php的執(zhí)行目錄下
//檢查有多少個臉型
var_dump(face_count(‘party.jpeg', haarcascade_frontalface_alt.xml'));
//返回臉型在圖片中的位置參數(shù),多個則返回數(shù)組
$arr = face_detect(‘party.jpeg', haarcascade_frontalface_alt2.xml');
print_r($arr);
3.應(yīng)用
結(jié)合imagick可以將圖片做一下應(yīng)用。因為 face_detect只返回一個矩形參數(shù),包含x,y坐標(biāo)和w,h長寬參數(shù)。下面是我的一個應(yīng)用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>
<?
}
?>
1.安裝
從源代碼編譯成一個動態(tài)的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.函數(shù)使用
在phpinfo()里檢查是否有facedetect這個模塊。
從openCV源代碼/data/haarcascades/里頭取出所有xml文件放在php的執(zhí)行目錄下
//檢查有多少個臉型
var_dump(face_count(‘party.jpeg', haarcascade_frontalface_alt.xml'));
//返回臉型在圖片中的位置參數(shù),多個則返回數(shù)組
$arr = face_detect(‘party.jpeg', haarcascade_frontalface_alt2.xml');
print_r($arr);
3.應(yīng)用
結(jié)合imagick可以將圖片做一下應(yīng)用。因為 face_detect只返回一個矩形參數(shù),包含x,y坐標(biāo)和w,h長寬參數(shù)。下面是我的一個應(yīng)用demo
復(fù)制代碼 代碼如下:
<?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
相關(guān)文章
Zend?Framework框架等常用php框架中存在的問題
這篇文章主要介紹了Zend?Framework框架等常用php框架中存在的問題2008-01-01Apache實現(xiàn)Web Server負載均衡詳解(不考慮Session版)
本篇文章是對使用Apache實現(xiàn)Web Server負載均衡的方法進行了詳細的分析介紹,需要的朋友參考下(不考慮Session版)2013-07-07php獲取服務(wù)器端mac和客戶端mac的地址支持WIN/LINUX
這篇文章主要介紹了php獲取服務(wù)器端mac和客戶端mac地址的方法,需要的朋友可以參考下2014-05-05PHP iconv 解決utf-8和gb2312編碼轉(zhuǎn)換問題
就一個很簡單的函數(shù)iconv();但是就是這個函數(shù)在網(wǎng)上找了很多例子,都無法成功轉(zhuǎn)換,這是為什么呢?2010-04-04