您的位置:首页 > 移动开发 > 微信开发

微信日志开发之人脸识别开发

2015-04-05 22:16 176 查看
这节我们将简单介绍在微信日志中如何实现人脸识别功能

人脸识别我们将会使用Face++提供的api。

需要使用的id以及AppKey请到官网首页注册获取。

(一)实现步骤

1.将用户发送信息获取并提交到服务器

2.服务器端处理信息并返回处理结果

3.将信息返还给用户

(二)实现代码

1.信息提取

在微信日志的主函数中我们有用户输入信息获取的函数,人脸识别的相关信息也可以在其中实现

public function __construct() {
global $wpdb;

if(isset($_GET['echostr']))
$this->valid();//第一次验证

$postStr = (isset($GLOBALS["HTTP_RAW_POST_DATA"]))?$GLOBALS["HTTP_RAW_POST_DATA"]:'';
if($_POST["test"]){
$postStr = @$_POST["HTTP_RAW_POST_DATA"];
}
//$xml = file_get_contents('php://input');
//$postStr = unicode_encode($postStr);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
if($RX_TYPE == 'image')//该处为我们人脸识别函数的添加部分
{
$wpdb->postStr = $postStr;
$wpdb->postArray = wxlog_xml_to_array($postStr);
$this->wxlog_log_id = $this->insert_wxlog_log($wpdb->postArray,$wpdb->postStr);
$result = $this->receiveImage($postObj);
exit($result);
}


2.信息提交到服务器并处理

$faceObj = new FacePlusPlus();
$detect = $faceObj->face_detect($url);
$numbers = isset($detect->face)? count($detect->face):0;
if($numbers == 1)
{

}
if (($detect->face[0]->attribute->gender->value != $detect->face[1]->attribute->gender->value) && $numbers == 2){
$compare = $faceObj->recognition_compare($detect->face[0]->face_id,$detect->face[1]->face_id);
$result = getCoupleComment($compare->component_similarity->eye, $compare->component_similarity->mouth, $compare->component_similarity->nose, $compare->component_similarity->eyebrow, $compare->similarity);
return $result;
}
else{
return "请发送一男一女的图片";
}


该过程中需要使用到face++的相关api(具体使用方法请参考官网或私信联系)

//人脸检测
public function face_detect($urls = null)
{
return $this->call("detection/detect", array("url"=>$urls));
}

//人脸比较
public function recognition_compare($face_id1, $face_id2)
{
return $this->call("recognition/compare", array("face_id1"=>$face_id1, "face_id2"=>$face_id2));
}


(三)信息返回给用户

由于我们使用了微信日志作为平台,所以输出函数我们也不用写,直接使用微信日志的相关函数就可以

如果希望自己完成也很简单

private function receiveImage($object)
{
require_once('faceplusplus.php');
$content = getImageInfo((string)$object->PicUrl);
$result = $this->transmitText($object, $content);
return $result;
}

private function transmitText($object, $content)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
return $result;
}


通过以上相关函数就可以完成函数的返回。

(四)实现效果



大家可以通过扫描一下二维码体验上述效果,如果对于实现方法有什么不清楚的地方请加微信(dinghinh154)讨论。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: