Android 调用百度人脸采集

 

1.访问集成文档 http://ai.baidu.com/docs#/FaceSDK-Collect-WithLiveness-Android/top 申请license,下载自动授权的sdk

Android 调用百度人脸采集_第1张图片

2. 添加license

Android 调用百度人脸采集_第2张图片

3.添加sdk文件

Android 调用百度人脸采集_第3张图片

 

4.修改config文件

   

package com.baidu.aip.fl;
public class Config {

    // 为了apiKey,secretKey为您调用百度人脸在线接口的,如注册,识别等。
    // 为了的安全,建议放在您的服务端,端把人脸传给服务器,在服务端端进行人脸注册、识别放在示例里面是为了您快速看到效果
    public static String apiKey = "WGV*************m5m";
    public static String secretKey =  "a4cK**********cs";
    public static String licenseID = "bp-*********-android";
    public static String licenseFileName = "idl********android";
    /**
     * groupId,标识一组用户(由数字、字母、下划线组成),长度限制128B,可以自行定义,只要注册和识别都是同一个组。
     * 详情见 http://ai.baidu.com/docs#/Face-API/top
     * 

* 人脸识别 接口 https://aip.baidubce.com/rest/2.0/face/v2/identify * 人脸注册 接口 https://aip.baidubce.com/rest/2.0/face/v2/faceset/user/add */ public static String groupID = "*******"; }

5.添加类和权限

  





6.在继承Application类里添加

 FaceSDKManager.getInstance().initialize(this, Config.licenseID, Config.licenseFileName);
  FaceConfig config = FaceSDKManager.getInstance().getFaceConfig();
  // SDK初始化已经设置完默认参数(推荐参数),您也根据实际需求进行数值调整
  // 设置活体动作,通过设置list LivenessTypeEnum.Eye,LivenessTypeEnum.Mouth,LivenessTypeEnum.HeadUp,
  // LivenessTypeEnum.HeadDown,LivenessTypeEnum.HeadLeft, LivenessTypeEnum.HeadRight,
  // LivenessTypeEnum.HeadLeftOrRight
  List livenessList = new ArrayList<>();
  livenessList.add(LivenessTypeEnum.Mouth);
  livenessList.add(LivenessTypeEnum.Eye);
  livenessList.add(LivenessTypeEnum.HeadUp);
  livenessList.add(LivenessTypeEnum.HeadDown);
  livenessList.add(LivenessTypeEnum.HeadLeft);
  livenessList.add(LivenessTypeEnum.HeadRight);
  config.setLivenessTypeList(livenessList);
  // 设置 活体动作是否随机 boolean
  config.setLivenessRandom(true);
  config.setLivenessRandomCount(2);
  // 模糊度范围 (0-1) 推荐小于0.7
  config.setBlurnessValue(VALUE_BLURNESS);
  // 光照范围 (0-1) 推荐大于40
  config.setBrightnessValue(VALUE_BRIGHTNESS);
  // 裁剪人脸大小
  config.setCropFaceValue(VALUE_CROP_FACE_SIZE);
  // 人脸yaw,pitch,row 角度,范围(-45,45),推荐-15-15
  config.setHeadPitchValue(VALUE_HEAD_PITCH);
  config.setHeadRollValue(VALUE_HEAD_ROLL);
  config.setHeadYawValue(VALUE_HEAD_YAW);
  // 最小检测人脸(在图片人脸能够被检测到最小值)80-200, 越小越耗性能,推荐120-200
  config.setMinFaceSize(VALUE_MIN_FACE_SIZE);
  // 人脸置信度(0-1)推荐大于0.6
  config.setNotFaceValue(VALUE_NOT_FACE_THRESHOLD);
  // 人脸遮挡范围 (0-1) 推荐小于0.5
  config.setOcclusionValue(VALUE_OCCLUSION);
  // 是否进行质量检测
//  config.setCheckFaceQuality(true);
  config.setCheckFaceQuality(false);
  // 人脸检测使用线程数
  config.setFaceDecodeNumberOfThreads(2);
  // 是否开启提示音
  config.setSound(true);

  FaceSDKManager.getInstance().setFaceConfig(config);


  APIService.getInstance().init(this);
  APIService.getInstance().setGroupId(Config.groupID);
  // 用ak,sk获取token, 调用在线api,如:注册、识别等。为了ak、sk安全,建议放您的服务器,
  APIService.getInstance().initAccessTokenWithAkSk(new OnResultListener() {
      @Override
      public void onResult(AccessToken result) {
          Log.i("wtf", "AccessToken->" + result.getAccessToken());

         /* handler.post(new Runnable() {
              @Override
              public void run() {
                  Toast.makeText(Myapplicaition.this, "启动成功", Toast.LENGTH_LONG).show();
              }
          });*/
      }

      @Override
      public void onError(FaceError error) {
          Log.e("xx", "AccessTokenError:" + error);
          error.printStackTrace();

      }
  }, this, Config.apiKey, Config.secretKey);

//sdk里人脸检测类

public class FaceDetectActivity extends Activity {

//识别出结果的方法

faceDetectManager.setOnFaceDetectListener(new FaceDetectManager.OnFaceDetectListener() {
    @Override
    public void onDetectFace(final int retCode, FaceInfo[] infos, ImageFrame frame) {

     
        Log.d("retCoderetCode","retCode=="+retCode);
        //  Log.d("DetectLoginActivity", "retCode is:" + retCode);
        String str = "";
        if (retCode == 0) {
            if (infos != null && infos[0] != null) {
                FaceInfo info = infos[0];
                boolean distance = false;
                if (info != null && frame != null) {
                    if (info.mWidth >= (0.9 * frame.getWidth())) {
                        distance = false;
                        str = getResources().getString(R.string.detect_zoom_out);
                    } else if (info.mWidth <= 0.4 * frame.getWidth()) {
                        distance = false;
                        str = getResources().getString(R.string.detect_zoom_in);
                    } else {
                        distance = true;
                    }
                }
                boolean headUpDown;
                if (info != null) {
                    if (info.headPose[0] >= ANGLE) {
                        headUpDown = false;
                        str = getResources().getString(R.string.detect_head_up);
                    } else if (info.headPose[0] <= -ANGLE) {
                        headUpDown = false;
                        str = getResources().getString(R.string.detect_head_down);
                    } else {
                        headUpDown = true;
                    }

                    boolean headLeftRight;
                    if (info.headPose[1] >= ANGLE) {
                        headLeftRight = false;
                        str = getResources().getString(R.string.detect_head_left);
                    } else if (info.headPose[1] <= -ANGLE) {
                        headLeftRight = false;
                        str = getResources().getString(R.string.detect_head_right);
                    } else {
                        headLeftRight = true;
                    }

                    if (distance && headUpDown && headLeftRight) {
                        mGoodDetect = true;
                    } else {
                        mGoodDetect = false;
                    }

                }
            }
}

//保存图片到本地

faceDetectManager.setOnTrackListener(new FaceFilter.OnTrackListener() {
    @Override
    public void onTrack(FaceFilter.TrackedModel trackedModel) {
        if (trackedModel.meetCriteria() && mGoodDetect) {
            // upload(trackedModel);
            mGoodDetect = false;
            saveFaceBmp(trackedModel);
    
        }
    }
});

你可能感兴趣的:(Android 调用百度人脸采集)