1.基于上一节,我们已经把百度云上的人脸库,建好了,然后我们可以从,百度云官网拿到,
离线人脸识别的SDK.
SDK下载_文字识别SDK_语音识别SDK-百度AI开放平台 这里可以点进去下载
2.然后我们就可以基于给的官网示例去,进行人脸识别了.
人脸识别有两种方式:
第一种:基于示例代码,获取摄像头的,视频流中的图片,然后,把图片发送给百度云人脸识别API,
利用人脸搜索的API,来进行,在人脸库中搜索该人脸,如果搜索到了,并且人脸分值符合
标准,就认为找到该人脸了.这种方式只利用百度云人脸识别的在线API就可以实现.
a.这种方式,首先获取摄像头视频流中的图片是在:
这里加上这句,就可以获取到视频流中的图片,然后把图片,可以再调用下面的featureSearch方法的时候把,bitmap传递进去.
然后在featureSearch方法中,根据:
featureCheckMode自己写个分支.
然后,可以根据传过来的图片,把图片传递给百度云的,在线人脸库搜索的API,根据API返回的搜索到的user_id
然后去自己的数据库中查询这个用户,根据用户信息,如果能查到,说明人脸登录成功.
第二种:基于示例代码,每次识别人脸之前,先去,百度云人脸库,把人脸库图片,同步到本地的,平板
电脑上,然后再利用百度人脸识别,离线SDK去识别人脸.这种方式是利用百度云离线SDK
百度云在线API实现的.这种方式是,在打开,人脸识别摄像头页面之前,先去同步一下百度云的人脸库,到
本地.也就是在调用下面的代码,之前,先去请求一下,百度人脸库的获取API,获取人脸到本地库中.
下面是同步人脸库的做法,之前人脸库,我们是自己做了一个人脸库,在本地做的,请求的是我们自己的服务器,人脸入库,sdk,中本来就有
对应的代码,可以去看一下:实现就可以
参考代码:
//4.同步人脸库操作
private void faceAsyncLogin() {
// Toast.makeText(HomeActivity.this, "本机人脸数据开始同步",
// Toast.LENGTH_SHORT).show();
//人脸数据正在加载。。。。。。
//ToastUtils.toast(HomeActivity.this, "正在加载人脸,请稍等...");
//1.这里去同步照片到人脸库
//String faceUrl="http://172.19.128.73:8060/api/faceDevice/listByIp";
String server_ip = GetConfigValue.getConfigProperties("server_ip");
//String faceUrl="http://172.19.128.64:8061/api/faceDevice/listByIp";
String faceUrl= server_ip "/api/faceDevice/listByIp";
OkHttpClient faceHttpClient = new OkHttpClient();
Request faceRequest = new Request.Builder()
.get()
.url(faceUrl)
.build();
//构造Call对象
Call faceCall=faceHttpClient.newCall(faceRequest);
faceCall.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
ToastUtils.toast(HomeActivity.this, "网络连通失败");
ToastUtils.toast(HomeActivity.this, "人脸加载失败,请稍后再试");
}
@Override
public void onResponse(Call call, okhttp3.Response response) throws IOException {
//1.将base64,转换为图片
try {
reentrantLock.lock();
String jsonStr =response.body().string();
respFaceArray=null; //清空一下
JSONObject jsonObj=new JSONObject(jsonStr);
respFaceArray =jsonObj.getJSONArray("data");
linkedQueue.enQueue(respFaceArray);
uiHandler.sendEmptyMessage(141);
response.body().close();
reentrantLock.unlock();
} catch (JSONException e) {
ToastUtils.toast(HomeActivity.this, "服务器异常");
ToastUtils.toast(HomeActivity.this, "人脸加载失败,请稍后再试");
e.printStackTrace();
response.body().close();
}
}
});
}
public Handler uiHandler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 110) { //同步登陆二维码
// ImageView codeView = (ImageView)findViewById(R.id.codebutton);
// codeView.setImageBitmap(qrCode);
// ToastUtils.toast(HomeActivity.this, "登录二维码获取成功");
}else if(msg.what==115){//同步用户信息
Log.e("ets","ssss");
}else if (msg.what==131){
reentrantLock.lock();
//改成使用linkedqueue自定义的queue来操作
while (linkedQueue.isQueueEmpty() == false){
JSONArray jsonArray = (JSONArray) linkedQueue.deQueue();
faceServerHttp(jsonArray);
}
reentrantLock.unlock();
}
else if (msg.what==141){
reentrantLock.lock();
//改成使用linkedqueue自定义的queue来操作
while (linkedQueue.isQueueEmpty() == false){
JSONArray jsonArray = (JSONArray) linkedQueue.deQueue();
//if(jsonArray!=null && jsonArray.length()>0){
faceServerHttpLogin(jsonArray);
// }else{
// //ToastUtils.toast(HomeActivity.this,"人脸加载成功!");
// ToastUtils.toast(HomeActivity.this, "人脸加载中,请稍等...");
// Timer timer = new Timer();
// TimerTask timerTask = new TimerTask() {
// @Override
// public void run() {
// ToastUtils.toast(HomeActivity.this, "人脸数据加载成功");
// uiHandler.sendEmptyMessage(151);
// }
// };
// timer.schedule(timerTask,4000);
// }
}
reentrantLock.unlock();
}
else if (msg.what==151){
//timer人脸同步定时器
Timer faceTimer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
mLiveType = com.baidu.idl.main.facesdk.model.SingleBaseConfig.getBaseConfig().getType();
judgeLiveType(mLiveType,
FaceRGBGateActivity.class,
FaceNIRGateActivriy.class,
FaceDepthGateActivity.class,
FaceRgbNirDepthGataActivity.class);
}
};
faceTimer.schedule(timerTask,2300);
}
else {
ToastUtils.toast(HomeActivity.this, "下载文件失败,请检查网络连接");
// Toast.makeText(HomeActivity.this, "下载文件失败,请检查网络连接",
// Toast.LENGTH_SHORT).show();
}
}
};
private synchronized void faceServerHttpLogin(JSONArray respFaceArray) {
//同步人脸数据
mFinishCount = 0; // 已完成的图片数量
mSuccessCount = 0; // 已导入成功的图片数量
mFailCount = 0; // 已导入失败的图片数量
try {
boolean success = false; // 判断成功状态
List<String> faceIds =new ArrayList<String>();
//System.out.println("人脸库同步中,请稍等");
//ToastUtils.toast(HomeActivity.this, "人脸库同步中,请稍等");
for (int i = 0; i < respFaceArray.length(); i ) {
JSONObject faceJson = respFaceArray.getJSONObject(i);
//1.去判断人脸库中是否已经有该人脸
String faceDataStr = faceJson.getString("faceImage");
Log.e("TAG01","人脸照片---001:" faceDataStr);
//2.开始获取照片
if (StringUtils.isNotNUll(faceDataStr) && StringUtils.isNotEmpty(faceDataStr)) {
//1.获取要导入的人脸照片
Bitmap faceImg = ImageUtils.stringToBitmap(faceDataStr); //这个默认就是JPG的图片,不用检测
//2.获取要导入照片的ID
int faceId = faceJson.getInt("faceId");
final int userId = faceJson.getInt("userId");
//Log.e("TAG01","人脸ID:" faceId);
//Log.e("TAG01","用户ID:" userId);
//3.获取用户的年龄
int age =faceJson.getInt("age");
//Log.e("TAG01","用户年龄:" age);
//4.获取用户的生日
String birthday = faceJson.getString("birthday");
//Log.e("TAG01","用户生日:" birthday);
//5.获取身高
int height = faceJson.getInt("height");
//Log.e("TAG01","用户身高:" height);
//6.获取性别
String sex = faceJson.getString("sex");
//Log.e("TAG01","用户性别:" sex);
String faceName = faceJson.getString("userName");
//7.用户信息绑定字符串
String bind_user_str = "";
//bind_user_str = "{" "faceId" ":" faceId "," "userId" ":" userId "," "age" ":" age "," "birthday" ":" birthday "," "height" ":" height "," "sex" ":" sex "," "userName" ":" faceName "}";
bind_user_str = "{" "faceId" ":" """ faceId """ "," "userId" ":" """ userId """ "," "age" ":" """ age """ "," "birthday" ":" """ birthday """ "," "height" ":" """ height """ "," "sex" ":" """ sex """ "," "userName" ":" """ faceName """ "}";
Log.e("TAG01","用户内容:" bind_user_str);
//faceName="张三" i ; //------暂时没有传入faceName写入临时名字
//3.根据人脸ID,查询数据库与文件中对应的姓名是否相等,如果相等,则直接过滤
//User user = FaceApi.getInstance().getUserListById(faceId);
List<User> users = FaceApi.getInstance().getAllUserList();
User user = null;
//从已经存储的用户列表中去查询该用户是否已经入库
try{
if(users!=null){
for (User curUser : users) {
Log.e("TAG--","" curUser.getUserId());
if(curUser.getUserId().equals(String.valueOf(faceId))){
user =curUser;
break;
}
}
}
}catch (Exception e){
e.printStackTrace();
}
if (user != null) {
//4.该人脸已经存在不需要进行同步了
Log.e("TAG01","该人脸已经存在了---》:" faceId "_Ok");
faceIds.add(faceId "_Ok");
continue;
} else {
Log.e("TAG01","人脸入库开始---》");
//5.去把该人脸入库
if (faceImg == null) {
//6.不是一张bitmap图片,过滤
faceIds.add(faceId "_Fail");
//ToastUtils.toast(HomeActivity.this, "不是合法的图片");
continue;
}
//6.图片缩放
if (faceImg.getWidth() * faceImg.getHeight() > 3000 * 2000) {
if (faceImg.getWidth() > faceImg.getHeight()) {
float scale = 1 / (faceImg.getWidth() * 1.0f / 1000.0f);
faceImg = BitmapUtils.scale(faceImg, scale);
} else {
float scale = 1 / (faceImg.getHeight() * 1.0f / 1000.0f);
faceImg = BitmapUtils.scale(faceImg, scale);
}
}
//7.走人脸SDK接口,通过人脸检测、特征提取拿到人脸特征值
byte[] bytes = new byte[512];
ImportFeatureResult result;
//8.走人脸SDK接口,通过人脸检测、特征提取拿到人脸特征值
result = FaceApi.getInstance().getFeature(faceImg, bytes,
BDFaceSDKCommon.FeatureType.BDFACE_FEATURE_TYPE_LIVE_PHOTO);
Log.e("HomeActivity", "-------------------------------::::::::HttpServerHttpServer" result.getResult());
System.out.println("-------------------------------:::::::HttpServerHttpServer:" result.getResult());
while (result.getResult() == -11){
//System.out.println("百度人脸API");
//Log.e("HomeActivity", faceName ":百度人脸API加载中");
Log.e("TAG01","百度人脸API加载中---》");
}
//9.判断是否提取成功:128为成功,-1为参数为空,-2表示未检测到人脸
if (result.getResult() == -1) {
Log.e("HomeActivity", faceName ":bitmap参数为空");
System.out.println("bitmap参数为空------01");
// Toast.makeText(HomeActivity.this, faceId "-->" faceName " bitmap人脸图片参数为空",
// Toast.LENGTH_SHORT).show();
//ToastUtils.toast(HomeActivity.this, faceId "-->" faceName " bitmap人脸图片参数为空");
} else if (result.getResult() == -2) {
Log.e("HomeActivity", faceName ":未检测到人脸");
System.out.println("未检测到人脸------02");
// Toast.makeText(HomeActivity.this, faceId "-->" faceName " 未检测到人脸",
// Toast.LENGTH_SHORT).show();
//ToastUtils.toast(HomeActivity.this, faceId "-->" faceName " 未检测到人脸");
} else if (result.getResult() == -3) {
Log.e("HomeActivity", faceName ":抠图失败");
System.out.println("抠图失败------03");
// Toast.makeText(HomeActivity.this, faceId "-->" faceName " 人脸抠图失败",
// Toast.LENGTH_SHORT).show();
//ToastUtils.toast(HomeActivity.this, faceId "-->" faceName " 人脸抠图失败");
}else if(result.getResult() == -11){
}else if (result.getResult() == 128) {
//10.图片特征提取成功,存入数据库
// 将用户信息保存到数据库中
Log.e("TAG01","保存人脸中---》");
boolean importDBSuccess = FaceApi.getInstance().registerUserIntoDBSelf(null,String.valueOf(faceId),
faceName, faceName, bind_user_str, bytes);
//System.out.println("------04:" importDBSuccess);
//11.保存数据库成功
if (importDBSuccess) {
//12.保存图片到新目录中
File facePicDir = FileUtils.getBatchImportSuccessDirectory();
if (facePicDir != null) {
File savePicPath = new File(facePicDir, faceName);
if (FileUtils.saveBitmap(savePicPath, result.getBitmap())) {
Log.i("HomeActivity", "图片保存成功");
System.out.println("图片保存成功------05:");
// Toast.makeText(HomeActivity.this, faceId "-->" faceName " 人脸图片保存失败",
// Toast.LENGTH_SHORT).show();
success = true;
} else {
System.out.println("图片保存失败------06:");
Log.i("HomeActivity", "图片保存失败");
// Toast.makeText(HomeActivity.this, faceId "-->" faceName " 人脸图片保存失败",
// Toast.LENGTH_SHORT).show();
//ToastUtils.toast(HomeActivity.this, faceId "-->" faceName " 人脸图片保存失败");
}
}
} else {
Log.e("HomeActivity", faceName ":保存到数据库失败");
System.out.println("保存到数据库失败------07:");
// Toast.makeText(HomeActivity.this, faceId "-->" faceName " 保存到数据库失败",
// Toast.LENGTH_SHORT).show();
//ToastUtils.toast(HomeActivity.this, faceId "-->" faceName " 保存到数据库失败");
}
}else {
Log.e("HomeActivity", faceName ":未检测到人脸");
System.out.println(faceName ":未检测到人脸----08");
// Toast.makeText(HomeActivity.this, faceId "-->" faceName " 未检测到人脸",
// Toast.LENGTH_SHORT).show();
//ToastUtils.toast(HomeActivity.this, faceId "-->" faceName " 未检测到人脸");
}
//12.图片回收
if (!faceImg.isRecycled()) {
faceImg.recycle();
}
//13.判断成功与否
if (success) {
mSuccessCount ;
//15.数据保存成功以后,这里告诉服务器已经同步好了图片-----
faceIds.add(faceId "_Ok");
Log.e("TAG01","人脸保存成功---》" faceId "_Ok");
} else {
mFailCount ;
//16.人脸没有入库成功,那么这里需要-----
faceIds.add(faceId "_Fail");
System.out.println(faceName ":失败图片----09");
Log.e("HomeActivity", "失败图片:" faceName);
Log.e("TAG01","人脸保存失败---》");
// Toast.makeText(HomeActivity.this, faceId "-->" faceName " 同步失败",
// Toast.LENGTH_SHORT).show();
//ToastUtils.toast(HomeActivity.this, faceId "-->" faceName " 同步失败");
}
//14.已经处理了多少张人脸了,做个统计
mFinishCount ;
}
}
} //这里for循环结束,执行完以后,图片导入完成
//17.for循环完成所有人脸的导入,获取导入成功人脸的IDS
StringBuilder idsStr = new StringBuilder();
if(faceIds.size()>0){
for (String faceId : faceIds) {
idsStr.append(faceId).append(",");
}
if(idsStr.charAt(idsStr.length()-1) ==','){
idsStr.deleteCharAt(idsStr.length()-1);
}
//18.发送入库成功人脸Id,给服务器
Log.e("TAG01","人脸保存情况---idsStr》" idsStr);
sendSuccessFaceIdsLogin(idsStr);
}else{
// Toast.makeText(HomeActivity.this, "人脸数据,未同步成功或不需要同步",
// Toast.LENGTH_SHORT).show();
ToastUtils.toast(HomeActivity.this, "人脸加载中,请稍等....");
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
uiHandler.sendEmptyMessage(151);
ToastUtils.toast(HomeActivity.this, "人脸数据加载成功");
}
};
timer.schedule(timerTask,2000);
//ToastUtils.toast(HomeActivity.this, "人脸数据加载完成");
//ToastUtils.toast(HomeActivity.this, "本次没有同步成功的人脸或不需要同步");
//服务器发送过来的信息是空的时候会显示这个提升------
//System.out.println("本次没有同步成功的人脸或不需要同步");
}
//ToastUtils.toast(HomeActivity.this, "人脸同步完成");
//初始化人脸库成功.
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e){
//HttpServer ---人脸同步出错了..
//Log.e("onResponse","1233");
//ToastUtils.toast(HomeActivity.this, "人脸数据同步异常");
e.printStackTrace();
throw e;
}
}
,