|
@@ -4,17 +4,20 @@ import cn.com.qmth.examcloud.starters.face.verify.FaceVerifyProperties;
|
|
import cn.com.qmth.examcloud.starters.face.verify.common.CommonUtils;
|
|
import cn.com.qmth.examcloud.starters.face.verify.common.CommonUtils;
|
|
import cn.com.qmth.examcloud.starters.face.verify.common.FaceResult;
|
|
import cn.com.qmth.examcloud.starters.face.verify.common.FaceResult;
|
|
import cn.com.qmth.examcloud.starters.face.verify.common.JsonHelper;
|
|
import cn.com.qmth.examcloud.starters.face.verify.common.JsonHelper;
|
|
|
|
+import cn.com.qmth.examcloud.starters.face.verify.common.param.ImageParm;
|
|
|
|
+import cn.com.qmth.examcloud.starters.face.verify.common.param.ImageParmType;
|
|
import cn.com.qmth.examcloud.starters.face.verify.service.FaceVerifyService;
|
|
import cn.com.qmth.examcloud.starters.face.verify.service.FaceVerifyService;
|
|
import cn.com.qmth.examcloud.starters.face.verify.service.baidu.BaiduApiHelper;
|
|
import cn.com.qmth.examcloud.starters.face.verify.service.baidu.BaiduApiHelper;
|
|
import cn.com.qmth.examcloud.starters.face.verify.service.faceplus.FacePlusApiHelper;
|
|
import cn.com.qmth.examcloud.starters.face.verify.service.faceplus.FacePlusApiHelper;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
|
+import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
-import java.util.TreeMap;
|
|
|
|
|
|
|
|
public class FaceVerifyServiceImpl implements FaceVerifyService {
|
|
public class FaceVerifyServiceImpl implements FaceVerifyService {
|
|
|
|
|
|
@@ -23,26 +26,9 @@ public class FaceVerifyServiceImpl implements FaceVerifyService {
|
|
private FaceVerifyProperties properties;
|
|
private FaceVerifyProperties properties;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public FaceResult faceVerifyByBaidu(String imageUrl) {
|
|
|
|
|
|
+ public FaceResult faceVerifyByBaidu(ImageParm image) {
|
|
List<Map<String, String>> images = new ArrayList<>();
|
|
List<Map<String, String>> images = new ArrayList<>();
|
|
- Map<String, String> imageData = new TreeMap<>();
|
|
|
|
- imageData.put("image_type", "URL");
|
|
|
|
- imageData.put("image", imageUrl);
|
|
|
|
- images.add(imageData);
|
|
|
|
-
|
|
|
|
- String params = new JsonHelper().toJson(images);
|
|
|
|
- return BaiduApiHelper.faceVerify(properties, params);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public FaceResult faceVerifyByBaidu(File imageFile) {
|
|
|
|
- String imageBase64 = CommonUtils.toBase64(imageFile);
|
|
|
|
-
|
|
|
|
- List<Map<String, String>> images = new ArrayList<>();
|
|
|
|
- Map<String, String> imageData = new TreeMap<>();
|
|
|
|
- imageData.put("image_type", "BASE64");
|
|
|
|
- imageData.put("image", imageBase64);
|
|
|
|
- images.add(imageData);
|
|
|
|
|
|
+ images.add(this.buildImageParmForBaidu(image));
|
|
|
|
|
|
String params = new JsonHelper().toJson(images);
|
|
String params = new JsonHelper().toJson(images);
|
|
if (properties.getBaiduLocalEnabled()) {
|
|
if (properties.getBaiduLocalEnabled()) {
|
|
@@ -52,23 +38,8 @@ public class FaceVerifyServiceImpl implements FaceVerifyService {
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public FaceResult faceDetectByBaidu(String imageUrl) {
|
|
|
|
- // image_type: URL、BASE64、FACE_TOKEN,其中“本地化部署”不支持 URL 方式
|
|
|
|
- Map<String, String> imageData = new TreeMap<>();
|
|
|
|
- imageData.put("image_type", "URL");
|
|
|
|
- imageData.put("image", imageUrl);
|
|
|
|
-
|
|
|
|
- String params = new JsonHelper().toJson(imageData);
|
|
|
|
- return BaiduApiHelper.faceDetect(properties, params);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public FaceResult faceDetectByBaidu(File imageFile) {
|
|
|
|
- String imageBase64 = CommonUtils.toBase64(imageFile);
|
|
|
|
-
|
|
|
|
- Map<String, String> imageData = new TreeMap<>();
|
|
|
|
- imageData.put("image_type", "BASE64");
|
|
|
|
- imageData.put("image", imageBase64);
|
|
|
|
|
|
+ public FaceResult faceDetectByBaidu(ImageParm image) {
|
|
|
|
+ Map<String, String> imageData = this.buildImageParmForBaidu(image);
|
|
|
|
|
|
String params = new JsonHelper().toJson(imageData);
|
|
String params = new JsonHelper().toJson(imageData);
|
|
if (properties.getBaiduLocalEnabled()) {
|
|
if (properties.getBaiduLocalEnabled()) {
|
|
@@ -78,37 +49,10 @@ public class FaceVerifyServiceImpl implements FaceVerifyService {
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public FaceResult faceCompareByBaidu(String imageUrl, String imageUrl2) {
|
|
|
|
|
|
+ public FaceResult faceCompareByBaidu(ImageParm image1, ImageParm image2) {
|
|
List<Map<String, String>> images = new ArrayList<>();
|
|
List<Map<String, String>> images = new ArrayList<>();
|
|
- Map<String, String> imageData = new TreeMap<>();
|
|
|
|
- imageData.put("image_type", "URL");
|
|
|
|
- imageData.put("image", imageUrl);
|
|
|
|
- images.add(imageData);
|
|
|
|
-
|
|
|
|
- Map<String, String> imageData2 = new TreeMap<>();
|
|
|
|
- imageData2.put("image_type", "URL");
|
|
|
|
- imageData2.put("image", imageUrl2);
|
|
|
|
- images.add(imageData2);
|
|
|
|
-
|
|
|
|
- String params = new JsonHelper().toJson(images);
|
|
|
|
- return BaiduApiHelper.faceCompare(properties, params);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public FaceResult faceCompareByBaidu(File imageFile, File imageFile2) {
|
|
|
|
- String imageBase64 = CommonUtils.toBase64(imageFile);
|
|
|
|
- String imageBase64_2 = CommonUtils.toBase64(imageFile2);
|
|
|
|
-
|
|
|
|
- List<Map<String, String>> images = new ArrayList<>();
|
|
|
|
- Map<String, String> imageData = new TreeMap<>();
|
|
|
|
- imageData.put("image_type", "BASE64");
|
|
|
|
- imageData.put("image", imageBase64);
|
|
|
|
- images.add(imageData);
|
|
|
|
-
|
|
|
|
- Map<String, String> imageData2 = new TreeMap<>();
|
|
|
|
- imageData2.put("image_type", "BASE64");
|
|
|
|
- imageData2.put("image", imageBase64_2);
|
|
|
|
- images.add(imageData2);
|
|
|
|
|
|
+ images.add(this.buildImageParmForBaidu(image1));
|
|
|
|
+ images.add(this.buildImageParmForBaidu(image2));
|
|
|
|
|
|
String params = new JsonHelper().toJson(images);
|
|
String params = new JsonHelper().toJson(images);
|
|
if (properties.getBaiduLocalEnabled()) {
|
|
if (properties.getBaiduLocalEnabled()) {
|
|
@@ -117,43 +61,66 @@ public class FaceVerifyServiceImpl implements FaceVerifyService {
|
|
return BaiduApiHelper.faceCompare(properties, params);
|
|
return BaiduApiHelper.faceCompare(properties, params);
|
|
}
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
|
- public FaceResult faceDetectByFacePlus(String imageUrl) {
|
|
|
|
- // 参数:image_url、image_base64、image_file
|
|
|
|
- Map<String, String> params = new TreeMap<>();
|
|
|
|
- params.put("image_url", imageUrl);
|
|
|
|
|
|
+ private Map<String, String> buildImageParmForBaidu(ImageParm image) {
|
|
|
|
+ if (image == null || StringUtils.isBlank(image.value())) {
|
|
|
|
+ throw new IllegalArgumentException("ImageParm must be not empty.");
|
|
|
|
+ }
|
|
|
|
|
|
- return FacePlusApiHelper.faceDetect(properties, params);
|
|
|
|
|
|
+ // image_type: URL、BASE64、FACE_TOKEN,其中“本地化部署API”不支持 URL 方式
|
|
|
|
+ Map<String, String> imageData = new HashMap<>();
|
|
|
|
+ if (ImageParmType.FILE_URL == image.type()) {
|
|
|
|
+ imageData.put("image_type", "URL");
|
|
|
|
+ imageData.put("image", image.value());
|
|
|
|
+ } else if (ImageParmType.FACE_TOKEN == image.type()) {
|
|
|
|
+ imageData.put("image_type", "FACE_TOKEN");
|
|
|
|
+ imageData.put("image", image.value());
|
|
|
|
+ } else {
|
|
|
|
+ imageData.put("image_type", "BASE64");
|
|
|
|
+ if (ImageParmType.FILE_PATH == image.type()) {
|
|
|
|
+ File imageFile = new File(image.value());
|
|
|
|
+ String imageBase64 = CommonUtils.toBase64(imageFile);
|
|
|
|
+ imageData.put("image", imageBase64);
|
|
|
|
+ } else {
|
|
|
|
+ imageData.put("image", image.value());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return imageData;
|
|
}
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
|
- public FaceResult faceDetectByFacePlus(File imageFile) {
|
|
|
|
- String imageBase64 = CommonUtils.toBase64(imageFile);
|
|
|
|
-
|
|
|
|
- Map<String, String> params = new TreeMap<>();
|
|
|
|
- params.put("image_base64", imageBase64);
|
|
|
|
|
|
+ private void buildImageParmForFacePlus(Map<String, String> params, ImageParm image, String index) {
|
|
|
|
+ if (image == null || StringUtils.isBlank(image.value())) {
|
|
|
|
+ throw new IllegalArgumentException("ImageParm must be not empty.");
|
|
|
|
+ }
|
|
|
|
|
|
- return FacePlusApiHelper.faceDetect(properties, params);
|
|
|
|
|
|
+ // 参数:image_url、image_file、image_base64、face_token
|
|
|
|
+ if (ImageParmType.FILE_URL == image.type()) {
|
|
|
|
+ params.put("image_url" + (index != null ? index : ""), image.value());
|
|
|
|
+ } else if (ImageParmType.FACE_TOKEN == image.type()) {
|
|
|
|
+ params.put("face_token" + (index != null ? index : ""), image.value());
|
|
|
|
+ } else {
|
|
|
|
+ if (ImageParmType.FILE_PATH == image.type()) {
|
|
|
|
+ File imageFile = new File(image.value());
|
|
|
|
+ String imageBase64 = CommonUtils.toBase64(imageFile);
|
|
|
|
+ params.put("image_base64" + (index != null ? "_" + index : ""), imageBase64);
|
|
|
|
+ } else {
|
|
|
|
+ params.put("image_base64" + (index != null ? "_" + index : ""), image.value());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public FaceResult faceCompareByFacePlus(String imageUrl, String imageUrl2) {
|
|
|
|
- // 参数:image_url、image_base64、image_file、face_token
|
|
|
|
- Map<String, String> params = new TreeMap<>();
|
|
|
|
- params.put("image_url1", imageUrl);
|
|
|
|
- params.put("image_url2", imageUrl2);
|
|
|
|
|
|
+ public FaceResult faceDetectByFacePlus(ImageParm image) {
|
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
|
+ this.buildImageParmForFacePlus(params, image, null);
|
|
|
|
|
|
- return FacePlusApiHelper.faceCompare(properties, params);
|
|
|
|
|
|
+ return FacePlusApiHelper.faceDetect(properties, params);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public FaceResult faceCompareByFacePlus(File imageFile, File imageFile2) {
|
|
|
|
- String imageBase64 = CommonUtils.toBase64(imageFile);
|
|
|
|
- String imageBase64_2 = CommonUtils.toBase64(imageFile2);
|
|
|
|
-
|
|
|
|
- Map<String, String> params = new TreeMap<>();
|
|
|
|
- params.put("image_base64_1", imageBase64);
|
|
|
|
- params.put("image_base64_2", imageBase64_2);
|
|
|
|
|
|
+ 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);
|
|
return FacePlusApiHelper.faceCompare(properties, params);
|
|
}
|
|
}
|