2. 引入依赖
<dependency>
<groupId>com.arcsoft.face</groupId>
<artifactId>arcsoft-sdk-face</artifactId>
<version>3.0.0.0</version>
</dependency>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cbaddb018543f597fe848b1300b80998.png)
3. 直接贴出完整代码
@Service
public class FaceEngineService {
private static final Logger LOGGER = LoggerFactory.getLogger(FaceEngineService.class);
private FaceEngine faceEngine;
private FunctionConfiguration functionConfiguration;
@Autowired
private ApiConfig apiConfig;
private boolean checkIsPortrait(ImageInfo imageInfo) {
List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
this.faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList);
return !faceInfoList.isEmpty();
}
public boolean checkIsPortrait(byte[] imageData) {
return this.checkIsPortrait(ImageFactory.getRGBData(imageData));
}
public boolean checkIsPortrait(File file) {
return this.checkIsPortrait(ImageFactory.getRGBData(file));
}
public FaceFeature extractFaceFeature(byte[] imageData) {
List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
ImageInfo imageInfo = ImageFactory.getRGBData(imageData);
this.faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList);
FaceFeature faceFeature = new FaceFeature();
this.faceEngine.extractFaceFeature(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList.get(0), faceFeature);
LOGGER.info("特征值大小:" + faceFeature.getFeatureData().length);
return faceFeature;
}
public List<FaceInfo> detectFaces(byte[] imageData) {
List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
ImageInfo imageInfo = ImageFactory.getRGBData(imageData);
this.faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList);
return faceInfoList;
}
public List<FaceInfo> detectFaces(ImageInfo imageInfo) {
List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
this.faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList);
return faceInfoList;
}
public float faceSimilarScore(byte[] target, byte[] source) {
FaceFeature targetFaceFeature = this.extractFaceFeature(target);
FaceFeature sourceFaceFeature = this.extractFaceFeature(source);
FaceSimilar faceSimilar = new FaceSimilar();
this.faceEngine.compareFaceFeature(targetFaceFeature, sourceFaceFeature, faceSimilar);
LOGGER.info("相似度:" + faceSimilar.getScore());
return faceSimilar.getScore();
}
public float faceSimilarScore(FaceFeature targetFaceFeature, FaceFeature sourceFaceFeature) {
FaceSimilar faceSimilar = new FaceSimilar();
this.faceEngine.compareFaceFeature(targetFaceFeature, sourceFaceFeature, faceSimilar);
LOGGER.info("相似度:" + faceSimilar.getScore());
return faceSimilar.getScore();
}
public int getAge(byte[] imageData) {
this.ageAndGenderProcess(imageData);
List<AgeInfo> ageInfoList = new ArrayList<AgeInfo>();
this.faceEngine.getAge(ageInfoList);
LOGGER.info("年龄:" + ageInfoList.get(0).getAge());
return ageInfoList.get(0).getAge();
}
private void ageAndGenderProcess(byte[] imageData) {
ImageInfo imageInfo = getRGBData(imageData);
List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
this.faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList);
this.faceEngine.setLivenessParam(0.5f, 0.7f);
this.faceEngine.process(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList, functionConfiguration);
}
public int getGender(byte[] imageData) {
this.ageAndGenderProcess(imageData);
List<GenderInfo> genderInfoList = new ArrayList<GenderInfo>();
this.faceEngine.getGender(genderInfoList);
LOGGER.info("性别:" + genderInfoList.get(0).getGender());
return genderInfoList.get(0).getGender();
}
public Face3DAngle face3DAngle(byte[] imageData) {
this.ageAndGenderProcess(imageData);
List<Face3DAngle> face3DAngleList = new ArrayList<Face3DAngle>();
this.faceEngine.getFace3DAngle(face3DAngleList);
if (face3DAngleList.size() > 0) {
LOGGER.info("3D角度:" + face3DAngleList.get(0).getPitch() + "," + face3DAngleList.get(0).getRoll() + "," + face3DAngleList.get(0).getYaw());
return face3DAngleList.get(0);
}
return null;
}
public String face3DAngleString(byte[] imageData) {
String res = null;
Face3DAngle face3DAngle = this.face3DAngle(imageData);
if (face3DAngle != null) {
res = "3D角度:" + face3DAngle.getPitch() + "," + face3DAngle.getRoll() + "," + face3DAngle.getYaw();
}
return res;
}
public int getLiveness(byte[] imageData) {
this.ageAndGenderProcess(imageData);
List<LivenessInfo> livenessInfoList = new ArrayList<LivenessInfo>();
this.faceEngine.getLiveness(livenessInfoList);
LOGGER.info("活体:" + livenessInfoList.get(0).getLiveness());
return livenessInfoList.get(0).getLiveness();
}
public int getLivenessIr(byte[] imageData) {
this.ageAndGenderProcess(imageData);
List<IrLivenessInfo> irLivenessInfo = new ArrayList<>();
this.faceEngine.getLivenessIr(irLivenessInfo);
if (irLivenessInfo.size() > 0 ){
LOGGER.info("IR活体:" + irLivenessInfo.get(0).getLiveness());
return irLivenessInfo.get(0).getLiveness();
}
return -1;
}
public VersionInfo getVersion(){
return this.faceEngine.getVersion();
}
private FunctionConfiguration setFunctionConfiguration(){
FunctionConfiguration functionConfiguration = new FunctionConfiguration();
functionConfiguration.setSupportAge(true);
functionConfiguration.setSupportFace3dAngle(true);
functionConfiguration.setSupportFaceDetect(true);
functionConfiguration.setSupportFaceRecognition(true);
functionConfiguration.setSupportGender(true);
functionConfiguration.setSupportLiveness(true);
functionConfiguration.setSupportIRLiveness(true);
return functionConfiguration;
}
@PostConstruct
public void init() {
ApiConfig.ArcFaceApi arcFaceApi = this.apiConfig.getArcFaceApi();
String app_id = arcFaceApi.getApp_id();
String sdk_key = arcFaceApi.getSdk_key();
String libPath = arcFaceApi.getLib_path();
if (libPath == null || libPath.equals("")) {
LOGGER.error("人脸识别引擎库加载失败");
throw new RuntimeException("人脸识别引擎库加载失败");
}
FaceEngine faceEngine = new FaceEngine(libPath);
int errorCode = faceEngine.activeOnline(app_id, sdk_key);
if (errorCode != ErrorInfo.MOK.getValue() && errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
LOGGER.error("引擎激活失败");
throw new RuntimeException("引擎激活失败");
}
ActiveFileInfo activeFileInfo = new ActiveFileInfo();
errorCode = faceEngine.getActiveFileInfo(activeFileInfo);
if (errorCode != ErrorInfo.MOK.getValue() && errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
LOGGER.info("获取激活文件信息失败");
}
EngineConfiguration engineConfiguration = new EngineConfiguration();
engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_0_ONLY);
engineConfiguration.setDetectFaceMaxNum(10);
engineConfiguration.setDetectFaceScaleVal(32);
this.functionConfiguration = setFunctionConfiguration();
engineConfiguration.setFunctionConfiguration(functionConfiguration);
errorCode = faceEngine.init(engineConfiguration);
if (errorCode != ErrorInfo.MOK.getValue()) {
LOGGER.info("初始化引擎失败");
throw new RuntimeException("初始化引擎出错!");
}
this.faceEngine = faceEngine;
}
@PreDestroy
public void destory_face() throws Exception {
this.faceEngine.unInit();
LOGGER.info("人脸识别引擎卸载成功,时间:" + new Date());
}
}
4. controller
@Api(tags = "人脸识别")
@RestController
@RequestMapping("/faceEngine")
public class FaceEngineController extends BaseController {
@Autowired
private FaceEngineService faceEngineService;
@ApiOperation("检测是否为人脸")
@PostMapping("/checkIsPortrait")
public boolean checkIsPortrait(MultipartFile file){
boolean isPortrait = false;
try {
isPortrait = this.faceEngineService.checkIsPortrait(file.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
return isPortrait;
}
@ApiOperation("检测人脸的相似度")
@PostMapping("/faceSimilarScore")
public String faceSimilarScore(MultipartFile target,MultipartFile source){
String faceSimilarScore = null;
Float score = null;
try {
score = this.faceEngineService.faceSimilarScore(target.getBytes(), source.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
if (null != score){
faceSimilarScore = CommonUtil.float_percentage(score,3);
}
return faceSimilarScore;
}
@ApiOperation("检测年龄")
@PostMapping("/getAge")
public Integer getAge(MultipartFile file){
Integer age = null;
try {
age = this.faceEngineService.getAge(file.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
return age;
}
@ApiOperation("获取性别")
@PostMapping("/getGender")
public String getGender(MultipartFile file){
Integer gender = null;
try {
gender = this.faceEngineService.getGender(file.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
if (gender != null){
return Gender.getGender(gender).getDesc();
}
return null;
}
@ApiOperation("获取人脸三维角度信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "file" , value = "检测图形",required = true),
@ApiImplicitParam(name = "isJson",value = "是否是JSON")
})
@PostMapping("/face3DAngleString")
public String face3DAngleString(MultipartFile file,Boolean isJson){
String res = null;
if (isJson == null){
isJson = false;
}
try {
if (isJson){
Face3DAngle face3DAngle = this.faceEngineService.face3DAngle(file.getBytes());
res = JSON.toJSONString(face3DAngle);
}else {
res = this.faceEngineService.face3DAngleString(file.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
return res;
}
@ApiOperation("活体检测")
@PostMapping("/getLiveness")
public Integer getLiveness(MultipartFile file){
Integer liveness = null;
try {
liveness = this.faceEngineService.getLiveness(file.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
return liveness;
}
@ApiOperation("获取IR活体信息")
@PostMapping("/getLivenessIr")
public Integer getLivenessIr(MultipartFile file){
Integer liveness = null;
try {
liveness = this.faceEngineService.getLivenessIr(file.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
return liveness;
}
@ApiOperation("获取SDK版本信息")
@PostMapping("/getVersion")
public VersionInfo getVersion(){
return this.faceEngineService.getVersion();
}
}