|
@@ -41,17 +41,20 @@ public class FaceVerifyServiceImpl implements FaceVerifyService {
|
|
|
throw new IllegalArgumentException("FacePlus暂不支持人脸真实性检测!");
|
|
|
}
|
|
|
|
|
|
- Map<String, String> imageData = this.buildImageParmForBaidu(faceParam.getImages()[0]);
|
|
|
+ // 百度私有化API:未指定参数值时采用配置值;明确指定时则采用参数值
|
|
|
+ boolean usePrivateBaiduApi = faceParam.getApiType() == null ? properties.getDefaultPrivateBaiduApi() :
|
|
|
+ FaceApiType.PRIVATE_BAIDU_API == faceParam.getApiType();
|
|
|
+
|
|
|
+ Map<String, String> imageData = this.buildImageParamForBaidu(faceParam.getImages()[0], usePrivateBaiduApi);
|
|
|
imageData.put("face_field", "spoofing");
|
|
|
List<Map<String, String>> images = new ArrayList<>();
|
|
|
images.add(imageData);
|
|
|
String params = new JsonHelper().toJson(images);
|
|
|
|
|
|
- if (FaceApiType.BAIDU_API == faceParam.getApiType()) {
|
|
|
- return BaiduApiHelper.faceVerify(properties, params);
|
|
|
+ if (usePrivateBaiduApi) {
|
|
|
+ return BaiduApiHelper.faceVerifyUsePrivateApi(properties, params);
|
|
|
}
|
|
|
- // 默认:百度私有化部署API
|
|
|
- return BaiduApiHelper.faceVerifyUseLocalApi(properties, params);
|
|
|
+ return BaiduApiHelper.faceVerify(properties, params);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -65,21 +68,24 @@ public class FaceVerifyServiceImpl implements FaceVerifyService {
|
|
|
|
|
|
if (FaceApiType.FACE_PLUS == faceParam.getApiType()) {
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
- this.buildImageParmForFacePlus(params, faceParam.getImages()[0], null);
|
|
|
+ this.buildImageParamForFacePlus(params, faceParam.getImages()[0], null);
|
|
|
|
|
|
return FacePlusApiHelper.faceDetect(properties, params);
|
|
|
}
|
|
|
|
|
|
- Map<String, String> imageData = this.buildImageParmForBaidu(faceParam.getImages()[0]);
|
|
|
+ // 百度私有化API:未指定参数值时采用配置值;明确指定时则采用参数值
|
|
|
+ boolean usePrivateBaiduApi = faceParam.getApiType() == null ? properties.getDefaultPrivateBaiduApi() :
|
|
|
+ FaceApiType.PRIVATE_BAIDU_API == faceParam.getApiType();
|
|
|
+
|
|
|
+ Map<String, String> imageData = this.buildImageParamForBaidu(faceParam.getImages()[0], usePrivateBaiduApi);
|
|
|
imageData.put("liveness_control", "NORMAL");
|
|
|
imageData.put("max_face_num", "2");
|
|
|
String params = new JsonHelper().toJson(imageData);
|
|
|
|
|
|
- if (FaceApiType.BAIDU_API == faceParam.getApiType()) {
|
|
|
- return BaiduApiHelper.faceDetect(properties, params);
|
|
|
+ if (usePrivateBaiduApi) {
|
|
|
+ return BaiduApiHelper.faceDetectUsePrivateApi(properties, params);
|
|
|
}
|
|
|
- // 默认:百度私有化部署API
|
|
|
- return BaiduApiHelper.faceDetectUseLocalApi(properties, params);
|
|
|
+ return BaiduApiHelper.faceDetect(properties, params);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -93,141 +99,40 @@ public class FaceVerifyServiceImpl implements FaceVerifyService {
|
|
|
|
|
|
if (FaceApiType.FACE_PLUS == faceParam.getApiType()) {
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
- this.buildImageParmForFacePlus(params, faceParam.getImages()[0], "1");
|
|
|
- this.buildImageParmForFacePlus(params, faceParam.getImages()[1], "2");
|
|
|
+ this.buildImageParamForFacePlus(params, faceParam.getImages()[0], "1");
|
|
|
+ this.buildImageParamForFacePlus(params, faceParam.getImages()[1], "2");
|
|
|
|
|
|
return FacePlusApiHelper.faceCompare(properties, params);
|
|
|
}
|
|
|
|
|
|
- List<Map<String, String>> images = new ArrayList<>();
|
|
|
- images.add(this.buildImageParmForBaidu(faceParam.getImages()[0]));
|
|
|
- images.add(this.buildImageParmForBaidu(faceParam.getImages()[1]));
|
|
|
- String params = new JsonHelper().toJson(images);
|
|
|
-
|
|
|
- Double expectFaceCompareScore;
|
|
|
- if (faceParam.getExpectFaceCompareScore() != null) {
|
|
|
- expectFaceCompareScore = faceParam.getExpectFaceCompareScore();
|
|
|
- } else {
|
|
|
- expectFaceCompareScore = properties.getBaiduExpectFaceCompareScore();
|
|
|
- }
|
|
|
-
|
|
|
- if (FaceApiType.BAIDU_API == faceParam.getApiType()) {
|
|
|
- return BaiduApiHelper.faceCompare(properties, params, expectFaceCompareScore);
|
|
|
- }
|
|
|
- // 默认:百度私有化部署API
|
|
|
- return BaiduApiHelper.faceCompareUseLocalApi(properties, params, expectFaceCompareScore);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public FaceResult faceVerifyByBaidu(ImageParm image) {
|
|
|
- Map<String, String> imageData = this.buildImageParmForBaidu(image);
|
|
|
- imageData.put("face_field", "spoofing");
|
|
|
-
|
|
|
- List<Map<String, String>> images = new ArrayList<>();
|
|
|
- images.add(imageData);
|
|
|
-
|
|
|
- String params = new JsonHelper().toJson(images);
|
|
|
- if (properties.getBaiduLocalEnabled()) {
|
|
|
- return BaiduApiHelper.faceVerifyUseLocalApi(properties, params);
|
|
|
- }
|
|
|
- return BaiduApiHelper.faceVerify(properties, params);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public FaceResult faceVerifyByBaidu(ImageParm image, boolean localEnabled) {
|
|
|
- Map<String, String> imageData = this.buildImageParmForBaidu(image);
|
|
|
- imageData.put("face_field", "spoofing");
|
|
|
-
|
|
|
- List<Map<String, String>> images = new ArrayList<>();
|
|
|
- images.add(imageData);
|
|
|
-
|
|
|
- String params = new JsonHelper().toJson(images);
|
|
|
- if (localEnabled) {
|
|
|
- return BaiduApiHelper.faceVerifyUseLocalApi(properties, params);
|
|
|
- }
|
|
|
- return BaiduApiHelper.faceVerify(properties, params);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public FaceResult faceDetectByBaidu(ImageParm image) {
|
|
|
- Map<String, String> imageData = this.buildImageParmForBaidu(image);
|
|
|
- imageData.put("liveness_control", "NORMAL");
|
|
|
- imageData.put("max_face_num", "2");
|
|
|
-
|
|
|
- String params = new JsonHelper().toJson(imageData);
|
|
|
- if (properties.getBaiduLocalEnabled()) {
|
|
|
- return BaiduApiHelper.faceDetectUseLocalApi(properties, params);
|
|
|
- }
|
|
|
- return BaiduApiHelper.faceDetect(properties, params);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public FaceResult faceDetectByBaidu(ImageParm image, boolean localEnabled) {
|
|
|
- Map<String, String> imageData = this.buildImageParmForBaidu(image);
|
|
|
- imageData.put("liveness_control", "NORMAL");
|
|
|
- imageData.put("max_face_num", "2");
|
|
|
-
|
|
|
- String params = new JsonHelper().toJson(imageData);
|
|
|
- if (localEnabled) {
|
|
|
- return BaiduApiHelper.faceDetectUseLocalApi(properties, params);
|
|
|
- }
|
|
|
- return BaiduApiHelper.faceDetect(properties, params);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public FaceResult faceCompareByBaidu(ImageParm image1, ImageParm image2) {
|
|
|
- return this.faceCompareByBaidu(image1, image2, properties.getBaiduExpectFaceCompareScore());
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public FaceResult faceCompareByBaidu(ImageParm image1, ImageParm image2, boolean localEnabled) {
|
|
|
- return this.faceCompareByBaidu(image1, image2, properties.getBaiduExpectFaceCompareScore(), localEnabled);
|
|
|
- }
|
|
|
+ // 百度私有化API:未指定参数值时采用配置值;明确指定时则采用参数值
|
|
|
+ boolean usePrivateBaiduApi = faceParam.getApiType() == null ? properties.getDefaultPrivateBaiduApi() :
|
|
|
+ FaceApiType.PRIVATE_BAIDU_API == faceParam.getApiType();
|
|
|
|
|
|
- @Override
|
|
|
- public FaceResult faceCompareByBaidu(ImageParm image1, ImageParm image2, Double expectFaceCompareScore) {
|
|
|
List<Map<String, String>> images = new ArrayList<>();
|
|
|
- images.add(this.buildImageParmForBaidu(image1));
|
|
|
- images.add(this.buildImageParmForBaidu(image2));
|
|
|
-
|
|
|
- if (expectFaceCompareScore == null) {
|
|
|
- expectFaceCompareScore = properties.getBaiduExpectFaceCompareScore();
|
|
|
- }
|
|
|
-
|
|
|
+ images.add(this.buildImageParamForBaidu(faceParam.getImages()[0], usePrivateBaiduApi));
|
|
|
+ images.add(this.buildImageParamForBaidu(faceParam.getImages()[1], usePrivateBaiduApi));
|
|
|
String params = new JsonHelper().toJson(images);
|
|
|
- if (properties.getBaiduLocalEnabled()) {
|
|
|
- return BaiduApiHelper.faceCompareUseLocalApi(properties, params, expectFaceCompareScore);
|
|
|
- }
|
|
|
- return BaiduApiHelper.faceCompare(properties, params, expectFaceCompareScore);
|
|
|
- }
|
|
|
|
|
|
- @Override
|
|
|
- public FaceResult faceCompareByBaidu(ImageParm image1, ImageParm image2, Double expectFaceCompareScore,
|
|
|
- boolean localEnabled) {
|
|
|
- List<Map<String, String>> images = new ArrayList<>();
|
|
|
- images.add(this.buildImageParmForBaidu(image1));
|
|
|
- images.add(this.buildImageParmForBaidu(image2));
|
|
|
+ // 百度自定义人脸相似度期望通过的阈值
|
|
|
+ Double expectFaceCompareScore = faceParam.getExpectFaceCompareScore() != null ?
|
|
|
+ faceParam.getExpectFaceCompareScore() : properties.getBaiduExpectFaceCompareScore();
|
|
|
|
|
|
- if (expectFaceCompareScore == null) {
|
|
|
- expectFaceCompareScore = properties.getBaiduExpectFaceCompareScore();
|
|
|
- }
|
|
|
-
|
|
|
- String params = new JsonHelper().toJson(images);
|
|
|
- if (localEnabled) {
|
|
|
- return BaiduApiHelper.faceCompareUseLocalApi(properties, params, expectFaceCompareScore);
|
|
|
+ if (usePrivateBaiduApi) {
|
|
|
+ return BaiduApiHelper.faceCompareUsePrivateApi(properties, params, expectFaceCompareScore);
|
|
|
}
|
|
|
return BaiduApiHelper.faceCompare(properties, params, expectFaceCompareScore);
|
|
|
}
|
|
|
|
|
|
- private Map<String, String> buildImageParmForBaidu(ImageParm image) {
|
|
|
+ private Map<String, String> buildImageParamForBaidu(ImageParm image, boolean usePrivateBaiduApi) {
|
|
|
if (image == null || StringUtils.isBlank(image.value())) {
|
|
|
throw new IllegalArgumentException("ImageParm must be not empty.");
|
|
|
}
|
|
|
|
|
|
- // image_type: URL、BASE64、FACE_TOKEN,其中“本地化部署API”不支持 URL 方式
|
|
|
+ // image_type: URL、BASE64、FACE_TOKEN,其中“私有化部署API”不支持 URL 方式
|
|
|
Map<String, String> imageData = new HashMap<>();
|
|
|
if (ImageParmType.FILE_URL == image.type()) {
|
|
|
- if (properties.getBaiduLocalEnabled()) {
|
|
|
+ if (usePrivateBaiduApi) {
|
|
|
String imageBase64 = CommonUtils.toBase64(image.value());
|
|
|
imageData.put("image", imageBase64);
|
|
|
imageData.put("image_type", "BASE64");
|
|
@@ -251,7 +156,7 @@ public class FaceVerifyServiceImpl implements FaceVerifyService {
|
|
|
return imageData;
|
|
|
}
|
|
|
|
|
|
- private void buildImageParmForFacePlus(Map<String, String> params, ImageParm image, String index) {
|
|
|
+ private void buildImageParamForFacePlus(Map<String, String> params, ImageParm image, String index) {
|
|
|
if (image == null || StringUtils.isBlank(image.value())) {
|
|
|
throw new IllegalArgumentException("ImageParm must be not empty.");
|
|
|
}
|
|
@@ -272,23 +177,6 @@ public class FaceVerifyServiceImpl implements FaceVerifyService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public FaceResult faceDetectByFacePlus(ImageParm image) {
|
|
|
- Map<String, String> params = new HashMap<>();
|
|
|
- this.buildImageParmForFacePlus(params, image, null);
|
|
|
-
|
|
|
- return FacePlusApiHelper.faceDetect(properties, params);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public FaceResult faceCompareByFacePlus(ImageParm image1, ImageParm image2) {
|
|
|
- Map<String, String> params = new HashMap<>();
|
|
|
- this.buildImageParmForFacePlus(params, image1, "1");
|
|
|
- this.buildImageParmForFacePlus(params, image2, "2");
|
|
|
-
|
|
|
- return FacePlusApiHelper.faceCompare(properties, params);
|
|
|
- }
|
|
|
-
|
|
|
public void setProperties(FaceVerifyProperties properties) {
|
|
|
this.properties = properties;
|
|
|
}
|