前提条件: 首先,要有一个百度云账号并获取到自己的App_Id、API_KEY、Secret_Key和创建好的人脸库名
1. 下载SDK
composer require baidu/aip-sdk
2. 直接上代码? 挂件地方已注释
<?php
namespace App\Http\Controllers\Face;
use App\Http\Controllers\Controller;
use Dcat\Admin\Grid\Filter\Group;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class FaceController extends Controller
{
const APP_ID = ''; //百度智能云获取
const API_KEY = ''; //百度智能云获取
const SECRET_KEY = ''; //百度智能云获取
const IMAGE_Type = 'BASE64'; // 设置图片类型 必须大写
const OPSTION = ['max_face_num'=>5,'face_field'=>'age,beauty,gender,expression']; // 最大人脸数及返回 可选参数
const GROUP = ''; // 组名
private $client;
public function __construct()
{
//实例化对象
$this->client = new \AipFace(self::APP_ID, self::API_KEY, self::SECRET_KEY);
}
/**
* 人脸识别
* @param $image string 人脸图片
* @param $type int 方式
* */
public function face(request $request){
$image = $request['image'];
$type = $request['type'];
$img = $this->image($image,$type);
$result = $this->client->detect($img,self::IMAGE_Type,self::OPSTION);
return $result;
}
/**
* 人脸搜索
* @param $image string 人脸图片
* @param $type int 方式
* */
public function faceSearch(request $request){
$image = $request['image'];
$type = $request['type'];
$img = $this->image($image,$type);
$result = $this->client->search($img,self::IMAGE_Type,self::GROUP);
return $result;
}
/**
* 人脸注册
* @param $image string 人脸图片
* @param $userId int 用户Id
* */
public function faceRegister(request $request){
$image = $request['image'];
$type = $request['type'];
$userId = $request['userId'];
if(empty($userId)) return showMsg(400,'参数不全');
$img = $this->image($image,$type);
$result = $this->client->addUser($img,self::IMAGE_Type,self::GROUP,$userId,self::OPSTION);
return $result;
}
/**
* 人脸删除
* @param $faceToken string 删除的人脸图片token 注册成功时返回
* @param $userId int 用户ID
* */
public function faceDel(request $request){
$faceToken = $request['faceToken'];
$userId = $request['userId'];
if(empty($faceToken) || empty($userId)) return showMsg(400,'参数不全');
$result = $this->client->faceDelete($userId,self::GROUP,$faceToken);
return $result;
}
/**
* 人脸更新
* @param $image string 人脸图片
* @param $userId int 用户ID
* */
public function faceUpd(request $request){
$userId = $request['userId'];
$image = $request['image'];
$type = $request['type'];
if(empty($userId)) return showMsg(400,'参数不全');
$img = $this->image($image,$type);
$result = $this->client->updateUser($img,self::IMAGE_Type,self::GROUP,$userId);
return $result;
}
/**
* @param $image string 人脸图
* @param $type int 类型 5为文件上传图 7为现拍图
* */
public function image($image,$type){
if(empty($image) || empty($type)) return showMsg(400,'参数不全');
$result = '参数不对';
if($type == 5){
$result = base64_encode(file_get_contents($image));
}
if($type == 7){
$result = str_replace('data:image/png;base64,', '', $image);
}
return $result;
}
/**
* 将图片保存到本地
* 系统现拍 转码 base64编码字符串转换成图片
* @param $image string base64串
* */
public function baseDecode($image){
$imageName = "25220_".date("His",time())."_".rand(1111,9999).'.png';
if (strstr($image,",")){
$image = explode(',',$image);
$image = $image[1];
}
$path = "./".date("Ymd",time());
if (!is_dir($path)){ //判断目录是否存在 不存在就创建
mkdir($path,0777,true);
}
$imageSrc= $path."/". $imageName; //图片名字
$r = file_put_contents($imageSrc, base64_decode($image));//返回的是字节数
if (!$r) {
return showMsg(400,'图片生成失败');
}else{
$url = 'http://hyzf.cn/'.$imageSrc;
return $url;
}
}
}
另附:调用本地摄像头现拍代码(注:只有HTTPS域名和127.0.0.1 才可调用本地摄像头)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>H5 canvas 调用摄像头进行绘制</title>
<style>
html,body{
width:100%;
height:100%;
padding: 0px;
margin: 0px;
overflow: auto;
}
#canvas{
width:500px;
height:300px;
}
#video{
width:500px;
height:300px;
}
.btn{
display:inline-block;
text-align: center;
background-color: #333;
color:#eee;
font-size:14px;
padding:5px 15px;
border-radius: 5px;
cursor:pointer;
}
</style>
</head>
<body>
<p style="text-align:center;font-size:12px;color:#555;">本DEMO不会存储任何有关视频图像信息,可以查看源码,都是在本地操作,没有任何服务器上传代码。</p>
<video id="video" autoplay="true" style="background-color:#ccc;display:none;"></video>
<div style="width:500px;height:300px;margin:30px auto;">
<canvas id="canvas" width="500px" height="300px">您的浏览器不支持H5 ,请更换或升级!</canvas>
<span class="btn" filter="screenshot">视频截图</span>
<span class="btn" filter="close">暂停本地摄像头</span>
<span class="btn" filter="open">打开本地摄像头</span>
</div>
<div style="width:500px;height:300px;margin:40px auto;" id="show"></div>
</body>
</html>
<script>
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
var events = {
open : function(){
LV.open();
},
close : function(){
console.log(LV.timer);
clearInterval(LV.timer);
},
screenshot : function(){
//获得当前帧的图像并拿到数据
var image = canvas.toDataURL('jpeg');
document.getElementById('show').innerHTML = '<img src="'+image+'" style="width:500px;height:300px;" />'
}
};
var LV = {
video : document.getElementById('video'),
canvas : document.getElementById('canvas'),
timer : null,
media : null,
open :function(){
if(!LV.timer){
navigator.getUserMedia({
video : {width:500,height:300}
},function(stream){
LV.video.srcObject = stream;
LV.video.onloadedmetadata = function(e) {
LV.video.play();
};
},function(err){
alert(err);//弹窗报错
})
}
if(LV.timer){
clearInterval(LV.timer);
}
//将画面绘制到canvas中
LV.timer = setInterval(function(){
LV.ctx.drawImage(LV.video,0,0,500,300);
},15);
},
init : function(){
LV.ctx = LV.canvas.getContext('2d');
//绑定事件
document.querySelectorAll('[filter]').forEach(function(item){
item.onclick = function(ev){
var type = this.getAttribute('filter');
events[type].call(this,ev);
}
});
return LV;
}
};
LV.init();
</script>
3. 官方文档链接
人脸识别_人脸检测_人脸对比_人脸搜索_活体检测_百度智能云
|