讀古今文學網 > 微信公眾平台開發:從零基礎到ThinkPHP5高性能框架實踐 > 24.2.3 代碼實現 >

24.2.3 代碼實現

在介紹基礎接口時,我們曾講到,當用戶發送圖片給公眾號時,微信公眾號將收到一個圖片消息,該消息中包含PicUrl和MediaId兩項參數,分別表示圖片鏈接和圖片消息媒體ID。

這裡將直接把圖片鏈接地址提交給Face++來處理。

Face++的類定義如下。


 1 class FacePlusPlus
 2 {
 3     private $api_server_url;
 4     private $auth_params;
 5 
 6     public function __construct
 7     {
 8         $this->api_server_url = "http:// apicn.faceplusplus.com/";
 9         $this->auth_params = array;
10            $this->auth_params['api_key'] = "";
11            $this->auth_params['api_secret'] = "";
12     }
13 
14     // 人臉檢測
15     public function face_detect($urls = null)
16     {
17         return $this->call("detection/detect", array("url"=>$urls));
18     }
19 
20     // 人臉比較
21     public function recognition_compare($face_id1, $face_id2)
22     {
23         return $this->call("recognition/compare", array("face_id1"=>$face_id1, "face_
           id2"=>$face_id2));
24     }
25 
26     protected function call($method, $params = array)
27     {
28         $url = $this->api_server_url."$method?".http_build_query(array_merge($this->
           auth_params, $params));
29         $ch = curl_init;
30         curl_setopt($ch, CURLOPT_URL, $url);
31         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
32          $data = curl_exec($ch);
33         curl_close($ch);
34         $result = json_decode($data);
35         return $result;
36     }
37 }
  

上述代碼定義了FacePlusPlus類,在類中定義了兩個成員變量$api_server_url和$auth_params,以及3個方法face_detect、recognition_compare、call,前兩個方法分別定義了人臉檢測接口和人臉比較接口的實現。

第3~4行:定義了兩個成員變量$api_server_url和$auth_params。

第6~12行:類的構造函數,在構造函數中給成員變量定義了值,包括接口服務器的URL、API Key和API Secret。

第14~18行:定義人臉檢測接口的方法。

第20~24行:定義人臉比較接口的方法。

第26~36行:定義接口調用函數,內部使用curl實現。

獲取圖片識別結果的代碼如下。


 1 function getImageInfo($url)
 2 {
 3     $faceObj = new FacePlusPlus;
 4     $detect = $faceObj->face_detect($url);
 5     $numbers = isset($detect->face)? count($detect->face):0;
 6     if (($detect->face[0]->attribute->gender->value != $detect->face[1]->attribute->
       gender->value) && $numbers == 2){
 7         $compare = $faceObj->recognition_compare($detect->face[0]->face_id,$detect->
           face[1]->face_id);
 8         $result = getCoupleComment($compare->component_similarity->eye, $compare->
           component_similarity->mouth, $compare->component_similarity->nose, $compare
           ->component_similarity->eyebrow, $compare->similarity);
 9         return $result;
10     }else{
11         return "似乎不是一男一女,無法測試夫妻相";
12     }
13 }
  

在上述代碼中,先使用人臉檢測接口獲得圖片的識別結果,再判斷結果中是否得到兩個性別不同的人。如果返回結果不是兩張人臉且性別不同,則提示無法測試夫妻相;否則比較結果中的face_id,獲得相似度的結果。

獲得結果後,需要對結果進行加工,讓該功能更貼近實際情況,相應的代碼如下。


 1 function getCoupleComment($eye, $mouth, $nose, $eyebrow, $similarity)
 2 {
 3     $index = round(($eye + $mouth + $nose + $eyebrow) / 4);
 4     if ($index < 40){
 5         $comment = "花好月圓";
 6     }else if ($index < 50){
 7         $comment = "相濡以沫";
 8     }else if ($index < 60){
 9         $comment = "情真意切";
10     }else if ($index < 70){
11         $comment = "郎才女貌";
12     }else if ($index < 80){
13         $comment  = "心心相印";
14     }else if ($index < 90){
15         $comment  = "濃情蜜意";
16     }else{
17         $comment = "山盟海誓";
18     }
19     return "【夫妻相指數】\n得分:".$index."\n評語:".$comment;
20 }
  

在上述代碼中,將獲取眼睛、嘴巴、眉毛、鼻子4個部分相似值的平均值作為最終相似指數,並且根據指數大小添加表現夫妻關係的評語。

至此,借助強大的Face++人臉識別接口,夫妻相功能就實現了。其運行效果如圖24-5所示。需要注意的是,圖中的人臉經過了處理。

圖24-5 夫妻相測試