首页 > 代码库 > PHP实现人脸识别技术
PHP实现人脸识别技术
这次人脸识别技术,是实现在微信端的,也就是说利用公众微信平台,调用第三的API来实现人脸识别这项技术的。
实现的思路:
首先呢,将收集的照片,建立一个照片库,然后利用在微信平台发送的照片,去到照片库进行匹配,那么怎么匹配呢?
这就要利用第三方的API了。
这个是收集信息,然后存储到信息库(包括图谱库)
部分代码:
<html> <head> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0"> <title>上传到人脸信息库</title> <link rel="stylesheet" href="./weui/dist/style/weui.min.css"/> <style> #preview, .img, img { width:79px; height:79px; } </style> </head> <body> <div class="hd" style="text-align:center;"> <h1 class="weui_cell_primary">上传到人脸信息库</h1> </div> <form action="up.php" method="post" enctype="multipart/form-data"> <div class="weui_cell"> <div class="weui_cell_hd"><label class="weui_label">姓名</label></div> <div class="weui_cell_bd weui_cell_primary"> <input class="weui_input" type="text" name="username" placeholder="请输入联系人姓名"/> </div> </div> <div class="weui_cell"> <div class="weui_cell_hd"><label class="weui_label">电话</label></div> <div class="weui_cell_bd weui_cell_primary"> <input class="weui_input" type="text" name="number" pattern="[0-9]*" placeholder="请输入联系人电话号码"/> </div> </div> <div class="weui_cell"> <div class="weui_cell_hd"><label class="weui_label">微信</label></div> <div class="weui_cell_bd weui_cell_primary"> <input class="weui_input" type="text" name="weixin" placeholder="请输入联系人微信号"/> </div> </div> <div class="weui_cell"> <div class="weui_cell_bd weui_cell_primary"> <div class="weui_uploader"> <div class="weui_uploader_hd weui_cell"> <div class="weui_cell_bd weui_cell_primary">图片上传</div> </div> <div class="weui_uploader_bd"> <ul class="weui_uploader_files"> <li class="weui_uploader_file" id="preview"></li> </ul> <div class="weui_uploader_input_wrp"> <input class="weui_uploader_input" type="file" name="pic" onchange="preview(this)" /> </div> </div> </div> </div> </div> <input type="submit" value="提交" class="weui_btn weui_btn_primary" style="width:40%;"/> </form> </body><script type="text/javascript"> function preview(file){ var prevDiv = document.getElementById(‘preview‘); if(file.files && file.files[0]){ var reader = new FileReader(); reader.onload = function(evt){ prevDiv.innerHTML = ‘<img src="http://www.mamicode.com/‘ + evt.target.result + ‘" />‘; } reader.readAsDataURL(file.files[0]); }else{ prevDiv.innerHTML = ‘<div class="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=http://www.mamicode.com/‘‘ + file.value + ‘\‘"></div>‘; } } </script> </html>
分析照片
<?php if(isset($_FILES[‘pic‘])&&$_FILES[‘pic‘][‘error‘] == 0){ $name = mt_rand(100000,999999); $ext = explode(".",$_FILES[‘pic‘][‘name‘]); $ext = end($ext); $full = $name.".".$ext; $rs = move_uploaded_file($_FILES[‘pic‘][‘tmp_name‘], ‘./upload/‘.$full); if(!$rs){ exit(‘上传图片出错‘); } //上传成功后,获取图片地址 $pic = ‘http://yxhwxtest.applinzi.com/wx/upload/‘.$full; //$pic = ‘http://yxhwxtest.applinzi.com/wx/upload/718204.jpg‘; //人脸检测与分析 $api = ‘http://apicn.faceplusplus.com/v2/detection/detect?api_key=3f2fd9843ac889c84f43de513bca05f2&api_secret=sDUH2FP2b3L4tSKQNHCCk17sUYsHW8HU&url=‘.$pic.‘&attribute=glass,pose,gender,age,race,smiling‘; //将json转换为数组 $rs = json_decode(file_get_contents($api),true); if(count($rs[‘face‘]) == 0){ exit(‘没有检测出人脸‘); } $face_id = $rs[‘face‘][0][‘face_id‘]; //创建脸集 //$api=‘https://apicn.faceplusplus.com/v2/faceset/create?api_key=3f2fd9843ac889c84f43de513bca05f2&api_secret=sDUH2FP2b3L4tSKQNHCCk17sUYsHW8HU&faceset_name=renlianshibie&face_id=‘.$face_id; //将人脸加入到脸集中 $api = ‘https://apicn.faceplusplus.com/v2/faceset/add_face?api_secret=sDUH2FP2b3L4tSKQNHCCk17sUYsHW8HU&face_id=‘.$face_id.‘&api_key=3f2fd9843ac889c84f43de513bca05f2&faceset_name=renlianshibie‘; $rs = json_decode(file_get_contents($api),true); if($rs[‘success‘] == 0){ exit(‘加入脸集失败‘); }else{ //训练脸集 $api = ‘https://apicn.faceplusplus.com/v2/train/search?api_key=3f2fd9843ac889c84f43de513bca05f2&api_secret=sDUH2FP2b3L4tSKQNHCCk17sUYsHW8HU&faceset_name=renlianshibie‘; $rs = file_get_contents($api); //p($rs); exit(‘加入脸集成功‘); } } //调试输出函数 function p($var){ if(is_bool($var)){ var_dump($var); }else if(is_null($var)){ var_dump(NULL); }else{ echo "<pre style=‘position:relative;z-index:1000;padding:10px;border-radius:5px;background:#F5F5F5;border:1px solid #aaa;font-size:14px;line-height:18px;opacity:0.9;‘>".print_r($var,true)."</pre>"; } }?>
收集信息完成后
在微信平台上回复 你要找的人的照片
这是服务器接收到图片进行响应的代码:
//接受图片进行回复 if(strtolower($postObj->MsgType==‘image‘)){ $pic = $postObj->PicUrl; //检测人脸和分析 $api = ‘http://apicn.faceplusplus.com/v2/detection/detect?api_key=3f2fd9843ac889c84f43de513bca05f2&api_secret=sDUH2FP2b3L4tSKQNHCCk17sUYsHW8HU&url=‘.$pic.‘&attribute=glass,pose,gender,age,race,smiling‘; $rs = json_decode(file_get_contents($api),true); if(count($rs[‘face‘])==0){ $cont = "没有检测到人脸"; } $face_id = $rs[‘face‘][0][‘face_id‘]; //得到人脸,到脸集中去找相似的脸 $api = ‘https://apicn.faceplusplus.com/v2/recognition/search?api_key=3f2fd9843ac889c84f43de513bca05f2&api_secret=sDUH2FP2b3L4tSKQNHCCk17sUYsHW8HU&faceset_name=renlianshibie&key_face_id=‘.$face_id; $rs = json_decode(file_get_contents($api),true); //$ps=[]; foreach($rs[‘candidate‘] as $v) { if( intval($v[‘similarity‘]) > 60) { $ps[] = $v[‘face_id‘]; } } if(empty($ps)){ $cont = "找到相似的人"; }else{ $fids = implode(‘,‘, $ps); $api = ‘https://apicn.faceplusplus.com/v2/info/get_face?api_key=3f2fd9843ac889c84f43de513bca05f2&api_secret=sDUH2FP2b3L4tSKQNHCCk17sUYsHW8HU&face_id=‘.$fids; $rs = json_decode(file_get_contents($api),true); if(count($rs[‘face_info‘])==0){ $cont = ‘没有找到相识的人‘; }else{ $cont = ‘找到相似的人‘."\n"; foreach ($rs[‘face_info‘] as $v) { $cont .= $v[‘url‘]."\n"; } } } //回复用户消息 $toUser = $postObj->FromUserName; $fromUser = $postObj->ToUserName; $time = time(); $content = $cont; $msgType = ‘text‘; $template = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $info = sprintf($template,$toUser,$fromUser,$time,$msgType,$content); echo $info; }
然后,服务器就会返回,相应匹配到的信息。
返回的相应照片
然后就是实现了,人脸匹配的功能。
调用API的第三方平台:http://www.faceplusplus.com.cn/demo-detect/
代码下载:http://download.csdn.net/detail/yxhbk/9629866
PHP实现人脸识别技术
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。