• 人脸识别


    今天介绍的是通过face++联网API实现人脸识别功能、打开相机及从相册获取图片功能。

    文档请到face++官网查看(https://www.faceplusplus.com.cn/)

    程序的主流程:

    1、创建一个人脸的集合FaceSet,用于存储人脸标识 face_token

    调用face++的url:https://api-cn.faceplusplus.com/facepp/v3/faceset/create

     1 RequestParams params = new RequestParams();
     2 params.put("api_key",Constant.Key);
     3 params.put("api_secret",Constant.Secret);
     4 params.put("display_name","FaceSet");
     5 params.put("outer_id","faceSet");
     6 
     7 HttpUtil.post(Constant.createUrl,params,new AsyncHttpResponseHandler(){
     8     @Override
     9     public void onSuccess(String resultJson) {
    10         super.onSuccess(resultJson);
    11         Log.e("createFaceSet==Success","success");
    12         Log.e("resultJson===",resultJson.toString());
    13     }
    14 
    15     @Override
    16     public void onFailure(Throwable throwable, String resultJson) {
    17         super.onFailure(throwable, resultJson);
    18         Log.e("createFaceSet==Error","error");
    19         Log.e("resultJson===",resultJson.toString());
    20         Toast.makeText(MainActivity.this,"createFaceSet=="+resultJson.toString(),Toast.LENGTH_SHORT).show();
    21     }
    22 });

    2、传入图片进行人脸检测和人脸分析

    调用face++的url:https://api-cn.faceplusplus.com/facepp/v3/detect

     1 Bitmap bitmap = BitmapFactory.decodeFile(urlImg);
     2 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
     3 bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream );
     4 byte[] imagebyte = outputStream.toByteArray();
     5 String image_base64 = Constant.encode(imagebyte);
     6 RequestParams params = new RequestParams();
     7 params.put("api_key", Constant.Key);
     8 params.put("api_secret",Constant.Secret);
     9 params.put("image_base64",image_base64);
    10 params.put("return_attributes","gender,age");
    11 
    12 HttpUtil.post(Constant.detectUrl,params,new AsyncHttpResponseHandler(){
    13     @Override
    14     public void onSuccess(String resultJson) {
    15         super.onSuccess(resultJson);
    16         Log.e("detect==testSuccess","success");
    17         Log.e("resultJson===",resultJson.toString());
    18         result.setText(resultJson.toString());
    19         if(resultJson != null){
    20                 JSONObject object;
    21                 try {
    22                     object = new JSONObject(resultJson);
    23                     JSONArray array = object.getJSONArray("faces");
    24                     String face_token = null;
    25                     for(int i = 0;i<array.length();i++){
    26                         JSONObject oj = (JSONObject) array.get(i);
    27                         face_token = (String) oj.get("face_token");
    28                     }
    29                     if(face_token != null){
    30                         Log.e("face_token",face_token);
    31                         if (state == 1){
    32                             addFace(face_token);
    33                         }else if(state == 2){
    34                             serachFace(face_token);
    35                         }else{
    36                             Toast.makeText(MainActivity.this,"state=="+state,Toast.LENGTH_SHORT).show();
    37                         }
    38                     }else{
    39                         Toast.makeText(MainActivity.this,"上传图片无法获取人脸标识",Toast.LENGTH_SHORT).show();
    40                     }
    41                 } catch (JSONException e) {
    42                     e.printStackTrace();
    43                 }
    44             }
    45 
    46         }
    47 
    48         @Override
    49         public void onFailure(Throwable throwable, String resultJson) {
    50             super.onFailure(throwable, resultJson);
    51             Log.e("detect==testError","error");
    52             Log.e("resultJson===",resultJson.toString());
    53             Toast.makeText(MainActivity.this,"detect=="+resultJson.toString(),Toast.LENGTH_SHORT).show();
    54             result.setText(resultJson.toString());
    55         }
    56     });

    3、为一个已创建的人脸集合FaceSet 添加人脸标识face_token

    调用face++的url: https://api-cn.faceplusplus.com/facepp/v3/faceset/addface

     1 RequestParams params = new RequestParams();
     2 params.put("api_key",Constant.Key);
     3 params.put("api_secret",Constant.Secret);
     4 params.put("faceset_token",faceset_token);
     5 params.put("face_tokens",face_token);
     6 
     7 HttpUtil.post(Constant.addfaceUrl,params,new AsyncHttpResponseHandler(){
     8     @Override
     9     public void onSuccess(String resultJson) {
    10         super.onSuccess(resultJson);
    11         Log.e("addFace==Success","success");
    12         Log.e("resultJson===",resultJson.toString());
    13         JSONObject object;
    14         try {
    15             object = new JSONObject(resultJson);
    16             int face_added = Integer.parseInt(object.getString("face_added"));
    17             if(face_added > 0){
    18                 Toast.makeText(MainActivity.this,"添加人脸标识成功!",Toast.LENGTH_SHORT).show();
    19             }else{
    20                 Toast.makeText(MainActivity.this,"添加人脸标识失败!",Toast.LENGTH_SHORT).show();
    21             }
    22         } catch (JSONException e) {
    23             e.printStackTrace();
    24         }
    25     }
    26 
    27     @Override
    28     public void onFailure(Throwable throwable, String resultJson) {
    29         super.onFailure(throwable, resultJson);
    30         Log.e("addFace==Error","error");
    31         Log.e("resultJson===",resultJson.toString());
    32         Toast.makeText(MainActivity.this,"addFace=="+resultJson.toString(),Toast.LENGTH_SHORT).show();
    33     }
    34 });

    4、在一个已有的人脸集合FaceSet 中找出与目标人脸最相似的一张或多张人脸,返回置信度和不同误识率下的阈值

    调用face++的url:https://api-cn.faceplusplus.com/facepp/v3/search

     1 RequestParams params = new RequestParams();
     2 params.put("api_key",Constant.Key);
     3 params.put("api_secret",Constant.Secret);
     4 params.put("face_token",face_token);
     5 params.put("faceset_token",faceset_token);
     6 /**
     7  * 控制返回比对置信度最高的结果的数量。合法值为一个范围 [1,5] 的整数。默认值为 1
     8  */
     9 
    10 HttpUtil.post(Constant.searchUrl,params,new AsyncHttpResponseHandler(){
    11     @Override
    12     public void onSuccess(String resultJson) {
    13         super.onSuccess(resultJson);
    14         Log.e("serachFace==Success","success");
    15         Log.e("resultJson===",resultJson.toString());
    16         JSONObject object;
    17         try {
    18             object = new JSONObject(resultJson);
    19             JSONArray array = object.getJSONArray("results");
    20             //比对结果置信度
    21             int confidence = 0;
    22             for(int i = 0;i<array.length();i++){
    23                 JSONObject oj = (JSONObject) array.get(i);
    24                 confidence = oj.getInt("confidence");
    25             }
    26             if(confidence != 0){
    27                 Log.e("confidence",confidence+"");
    28                 if(confidence >= 80){
    29                     AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    30                     builder.setTitle("比对结果");
    31                     builder.setMessage("通过");
    32                     byte[] requestData;
    33                     requestData = CommandGenerate.generateLockCmd(CommandGenerate.CMD.UNLOCK,4);
    34                     sendDataToSerial(requestData);
    35                     builder.show();
    36                 }else{
    37                     AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    38                     builder.setTitle("比对结果");
    39                     builder.setMessage("未通过");
    40                     builder.show();
    41                     //Toast.makeText(MainActivity.this,"比对结果置信度低于80=="+confidence,Toast.LENGTH_SHORT).show();
    42                 }
    43             }else {
    44                 Toast.makeText(MainActivity.this,"FaceSet中未搜索到相吻合的信息",Toast.LENGTH_SHORT).show();
    45             }
    46         } catch (JSONException e) {
    47             e.printStackTrace();
    48         }
    49     }
    50     @Override
    51     public void onFailure(Throwable throwable, String resultJson) {
    52         super.onFailure(throwable, resultJson);
    53         Log.e("serachFace==Error","error");
    54         Log.e("resultJson===",resultJson.toString());
    55         Toast.makeText(MainActivity.this,"serachFace=="+resultJson.toString(),Toast.LENGTH_SHORT).show();
    56     }
    57 });

    项目源码:http://download.csdn.net/download/daxudada/10229284

    希望对大家有用,如有问题请多多指教。关注我哟

  • 相关阅读:
    深入理解ThreadLocal
    synchronized与Lock的区别与使用
    1亿个数中找出最小的100个数--最小堆
    B+/-Tree原理(mysql索引数据结构)
    深入理解token
    shiro(java安全框架)
    第一次项目上Linux服务器(四:CentOS6下Mysql数据库的安装与配置(转))
    第一次项目上Linux服务器(三:安装Tomcat及相关命令)
    第一次项目上Linux服务器(二:——安装jdk)
    第一次项目上Linux服务器(一:远程连接服务器)
  • 原文地址:https://www.cnblogs.com/creazyda/p/9149295.html
Copyright © 2020-2023  润新知