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:异质人脸识别进展的资讯
高新波教授团队异质人脸图像识别研究取得新突破,有望大大降低刑侦过程人力耗费并提高办案效率 近日,西安电子科技大学高新波教授带领的研究团队,在异质人脸图像识别研究领域取得重要进展,其对香 ...
随机推荐
- 「零秒思考」是个神话,不过这款笔记术你值得拥有zz
今天读完了赤羽雄二的<零秒思考>,作者是一位在麦肯锡公司工作了 14 年的资深顾问.依照作者的说法,「零秒思考」指的是: 瞬间便能认清现状, 瞬间便能整理问题, 瞬间便能考虑出解决办法, ...
- NumPy事例练习
因为排版问题直接把jupyter里的截图过来了:暂时就写了这么点小例子,建议在ipython notebook中做测试
- git学习(4)---工作流
一.目的 前三章介绍了git工具本身的操作,主要包含本地仓库操作和远程库操作两部分内容.接下来,我们将介绍怎样使用git进行项目开发,也叫做git工作流. git工作流分为三种模式:共享远程库模式.独 ...
- commons io上传文件
习惯了是用框架后,上传功能MVC框架基本都提供了.如struts2,springmvc! 可是假设项目中没有使用框架.而是单纯的使用jsp或servlet作为action,这时我们就能够使用commo ...
- 【转】IDA远程调试 The debugger could not attach to the selected process. irs_recv 等待的操作过时
IDA连接android_server 选中进程点ok之后 连接不上报错 The debugger could not attach to the selected process. This can ...
- 【BZOJ3218】a + b Problem 可持久化线段树优化建图
[BZOJ3218]a + b Problem 题解:思路很简单,直接最小割.S->i,容量为Bi:i->T,容量为Wi:所有符合条件的j->new,容量inf:new->i, ...
- EasyIPCamera实现的桌面采集直播用于课堂、会议、展销同屏等应用
本文转自博客:http://blog.csdn.net/jinlong0603/article/details/56664233 Android同屏直播 在Android上除了获取摄像头数据为Easy ...
- Detours3.0 文档翻译
http://blog.csdn.net/buck84/article/details/8289991 拦截二进制函数 Detours库能够在执行过程中动态拦截函数调用.detours将目标函数前几个 ...
- 【WordSearch】Word Search
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- machine learning for hacker记录(3) 贝叶斯分类器
本章主要介绍了分类算法里面的一种最基本的分类器:朴素贝叶斯算法(NB),算法性能正如英文缩写的一样,很NB,尤其在垃圾邮件检测领域,关于贝叶斯的网上资料也很多,这里推荐那篇刘未鹏写的http://mi ...