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="' + evt.target.result + '" />';
}
reader.readAsDataURL(file.files[0]);
}else{
prevDiv.innerHTML = '<div class="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=\'' + 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实现人脸识别技术的更多相关文章
- face ++ 人脸识别技术初步
网站地址: https://console.faceplusplus.com.cn/documents/5671791主要有 1 人脸识别技术 2 人体识别技术 ...
- 旷视科技 -- Face++ 世界最大的人脸识别技术平台
旷视科技 -- Face++ 世界最大的人脸识别技术平台: https://www.megvii.com/
- 人脸识别技术大总结(1):Face Detection & Alignment
http://blog.jobbole.com/85783/ 首页 最新文章 IT 职场 前端 后端 移动端 数据库 运维 其他技术 - 导航条 - 首页 最新文章 IT 职场 前端 - Ja ...
- 基于 HTML5 的人脸识别技术
基于 HTML5 的人脸识别技术 https://github.com/auduno/headtrackr/
- 3D动态人脸识别技术分析——世纪晟人脸识别实现三维人脸建模
- 目录 - 国内3D动态人脸识别现状概况 - 新形势下人脸识别技术发展潜力 - 基于深度学习的3D动态人脸识别技术分析 1. 非线性数据建模方法 2. 基于3D变形模型的人脸建模 - 案例结合——世 ...
- 人脸识别技术大总结1——Face Detection & Alignment
搞了一年人脸识别,寻思着记录点什么,于是想写这么个系列,介绍人脸识别的四大块:Face detection, alignment, verification and identification(re ...
- 基于百度AI人脸识别技术的Demo
编写demo之前首先浏览官方API:http://ai.baidu.com/docs#/Face-API/top 下面是源码: package com.examsafety.test; import ...
- ios OpenCv的配置和人脸识别技术
作为一个好奇心非常重的人,面对未知的世界都想去一探到底. 于是做了个人脸识别的demo. 眼下国内的关于opencv技术文章非常少.都是互相抄袭.关键是抄个一小部分还不全.时间又是非常久之前的了,和如 ...
- paper 97:异质人脸识别进展的资讯
高新波教授团队异质人脸图像识别研究取得新突破,有望大大降低刑侦过程人力耗费并提高办案效率 近日,西安电子科技大学高新波教授带领的研究团队,在异质人脸图像识别研究领域取得重要进展,其对香 ...
随机推荐
- 用redis实现跨服务器session(转)
这个月我们新开发了一个项目,由于使用到了4台机器做web,使用dns做负载均衡, 上面图上用户通过DNS的调度(一个域名对应多个ip)分别访问到VM2-VM5上,四台机器都访问VM1上的redis,两 ...
- iOS应用数据存储的经常使用方式
ios程序中数据数据存储有下列5种方式 XML属性列表(plist)归档 Preference(偏好设置) NSKeyedArchiver归档(NSCoding) SQLite3 Core Data ...
- Flash文字效果
flash中增加文本.使用了消除锯齿:可读性消除锯齿.发现不嵌入字体的无法动态改动里面的文字,但嵌入字体的话会造成swf文件过大. 终于还是选择了使用设备字体,并选择了黑体.出了一个问题.文字没有加粗 ...
- Android 中View仅仅能接收到ACTION_DOWN无法接收ACTION_MOVE和ACTION_UP解决的方法
昨天晚上调试了一晚上,在LinearLayout上接收屏幕动作,可是出现了问题, 以下的代码是本人调的代码 </pre><pre name="code" clas ...
- LeetCode215:Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- 一套Tomcat处理多个域名请求 - Virtual Host
最近和Tomcat较上劲了... 作为Tomcat的系列之一,来尝试下如何用一套Tomcat来处理多个域名请求. 场景:基于成本考虑,多个department共用一台服务器,然后该服务器上就一套Tom ...
- mysql 较为高效的分页
直接上代码 DaoImpl: /** * 开发转让页面展示 ,查询搜索数据,而且分页展示 * @param zrdp 搜索条件封装对象 * @return */ @SuppressWarnings(& ...
- EasyDarwin自动停止推流
原文转自:http://blog.csdn.net/ss00_2012/article/details/51441753 我们使用EasyDarwin的推流转发来进行媒体直播的时候,有时会有这样一个需 ...
- Spring mvc接受集合类型参数的方法
public String xxxxx(String xxxx, String xxxxx, @RequestParam("parameterList[]") List<St ...
- 基于struts2的学生报道管理系统(附github源码地址)
本项目参考了<java web轻量级开发全体验>,加入了对mysql的支持. 一.基本业务功能 通过struts2框架,结合mysql数据库构建一个学生报到管理系统,来模拟学生报到登记的过 ...