php使用face++實現一個簡單的Face Service系統

來源:互聯網
上載者:User
流程 流程可以分為兩部分,一部分是訓練,一部分是測試。 關於如何使用face++提供的API可以看 http://blog.csdn.net/jianjian1992/article/details/46640483 代碼可在 http://download.csdn.net/detail/jianjian1992/8866839免費下載。
起始介面 介面 如下所示,將使用說明進行了介紹,因為是初次使用,很多不太會,所以介面弄得比較簡單,以能使用為第一目標。
實現細節 index.html是歡迎介面,記錄一下幾個小問題 中文的使用 在head部分加上字元集使用utf-8就好啦。
<head> <meta http-equiv = "Content-Type" content = "text/html; charset = utf-8" /></head>
文字置中與背景設定 在html最前面加了個style,設定body的顯示效果。 文字顯示設定為置中,使用height,width加上margin-top,margin-left便可以設定body部分在頁面中顯示的地方啦。 背景則設定了圖片url,以及置中顯示。
<style>body{text-align:left; height:500px;width:600px;top:50%;margin-top:130px;margin-left:550px;background-image:url(./imgs/back.jpg);background-position:center;background-repeat:repeat-y;}</style>

按鈕的頁面跳轉 onClick裡面用location記錄將要跳轉的介面。
<input type = "button" value = "點擊進入測試" onClick = "location='test.php' "/>

訓練部分 包括 兩部分: startTrain.php用於輸入訓練目錄, train.php用於將訓練資料傳到伺服器,並將訓練成功標記輸出 startTrain.php
這一部分做得蠻挫的,查了下php選擇檔案夾,沒有找到該怎麼做,所以就用了個<input type = "text">來輸入目錄。真是弱爆了啊 用了一個表單form來記錄輸入目錄,提交表單之後則跳到train.php中進行真正的訓練啦。
<form action="train.php" method="get"><input type="text" name="trainDir"><br><input type="submit" name="submit" value="提交"><input type="reset" name="reset" value = "重設"> </form>




train.php 這一部分實現了兩個功能: 根據輸入目錄尋找所有訓練圖片,並記錄所有圖片路徑 將所有訓練圖片一一檢測人臉,並將人臉加入到訓練模型中


尋找目錄 php中尋找目錄中所有檔案倒是挺方便的啊,listDir函數在dir.php中。 這個函數根據$dir目錄名,將得到的所有檔案名稱存入$names,所有檔案路徑名則存入$img_urls中。 注意的一點是,如果是中文檔案名稱,則要加上

$file = iconv("gb2312","UTF-8",$file);
這一句才會得到結果的,要不就是空哦 php中字串的尋找可以使用strstr函數, 比如下面的$file_name = strstr($file,'.',true)便是尋找$file中‘.’的前面部分,如果是strstr($file,'.',false)則是'.'的後面部分。 動態數組的添加使用array_push就ok啦。 哦,不要忘記了關閉資源啊。有opendir($dir)那麼就要有對應的closedir($dir)。
function listDir($dir, &$names, &$img_urls){if(is_dir($dir))   {     if ($dh = opendir($dir)) {        while (($file = readdir($dh)) !== false){     if((is_dir($dir."/".$file)) && $file!="." && $file!=".."){     //echo "<b><font color='red'>檔案名稱:</font></b>",$file,"<br><hr>";     listDir($dir."/".$file."/");     }else{$file = iconv("gb2312","UTF-8",$file);                 if($file!="." && $file!=".."){         //var_dump($file);$file_name = strstr($file, '.', true);//echo $file_name."<br>";array_push($names, $file_name);array_push($img_urls, $dir."/".$file);      }     }        }        closedir($dh);     }   }}


在train.php中使用如下代碼便得到了訓練用的所有圖片的url啦。
$img_url = array();$person_name = array();$trainDir = $_GET["trainDir"];listDir($trainDir, $person_name, $img_url);echo "從目錄中我們得到了 ".sizeof($img_url)." 張圖片".<br>;
接著我們建立一個訓練組oldpeople_qiaoxi。
$response = $facepp->execute('/group/delete', array('group_name' => 'oldpeople_qiaoxi'));$response = $facepp->execute('/group/create', array('group_name' => 'oldpeople_qiaoxi'));
接著做迴圈,對每張圖片檢測人臉
$params['img']          = $img;$params['attribute']    = 'gender,age,race,smiling,glass,pose';$response               = $facepp->execute('/detection/detect',$params);
從傳回值$response得到face_id之後,建立一個person並將檢測到的人臉加入這個類別person中。
$response = $facepp->execute('/person/delete', array('person_name' => $person_name[$i],'group_name' => 'oldpeople_qiaoxi'));$response = $facepp->execute('/person/create', array('person_name' => $person_name[$i],'group_name' => 'oldpeople_qiaoxi'));$response = $facepp->execute('/person/add_face', array('person_name' => $person_name[$i], 'face_id' => $face_id, 'group_name' => 'oldpeople_qiaoxi'));
將所有圖片都處理好之後,便可以訓練模型了。
$response = $facepp->execute('/train/identify', array('group_name' => 'oldpeople_qiaoxi'));
訓練成功的話,結果如下:

測試部分 這個部分包括2個部分: test.php負責選擇測試圖片 recognition.php負責將測試圖片傳到伺服器並給出結果 test.php部分 顯示如下: 選擇檔案部分用的是<input type = "file">來做的,之後表單提交到recognition.php就好了。


recognition.php部分 首先得到圖片的url。
$img_url = $_GET["testImgPath"];
之後執行identify得到身份結果。
$response = $facepp->execute('/recognition/identify', array('group_name' => 'oldpeople_qiaoxi', 'img' => $img_url));
這裡採用了一個數組來進行中文名與英文名的對應,因為face++不支援中文名的圖片,所以傳給它的圖片都是英文命名的,但是顯示需要中文名,所以在這裡進行映射。
$person_name = array("ami" => "艾米", "dongjian" => "張東健", "xiaowang" => "小王");







$img_url = array();$person_name = array();$trainDir = $_GET["trainDir"];listDir($trainDir, $person_name, $img_url);echo "從目錄中我們得到了 ".sizeof($img_url)." 張圖片".<br>;
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.