脚手架搭建之 集成虹软 人脸识别

1. 虹软 官网
2. 引入依赖
	<dependency>
            <groupId>com.arcsoft.face</groupId>
            <artifactId>arcsoft-sdk-face</artifactId>
            <version>3.0.0.0</version>
        </dependency>

在这里插入图片描述

3. 直接贴出完整代码
@Service
public class FaceEngineService {

    private static final Logger LOGGER = LoggerFactory.getLogger(FaceEngineService.class);

    private FaceEngine faceEngine;

    private FunctionConfiguration functionConfiguration;

    @Autowired
    private ApiConfig apiConfig;

    /**
     * 检测图片是否为人像
     *
     * @param imageInfo 图像对象
     * @return true:人像,false:非人像
     */
    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();
    }

    /**
     * 检测图片是否为人像
     *
     * @param imageData 图像对象
     * @return true:人像,false:非人像
     */
    public boolean checkIsPortrait(byte[] imageData) {
        return this.checkIsPortrait(ImageFactory.getRGBData(imageData));
    }

    /**
     * 检测图片是否为人像
     *
     * @param file 图像对象
     * @return true:人像,false:非人像
     */
    public boolean checkIsPortrait(File file) {
        return this.checkIsPortrait(ImageFactory.getRGBData(file));
    }

    /**
     * 提取人脸特征
     *
     * @param imageData 图片数据
     */
    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;
    }

    /**
     * 人脸相似度
     *
     * @return float
     */
    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();
    }

    /**
     * 人脸相似度
     *
     * @param targetFaceFeature 目标
     * @param sourceFaceFeature 源
     * @return float
     */
    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();
    }

    /**
     * 获取 年龄 和 性别 之前处理图像
     *
     * @param imageData 图像数据
     */
    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);
    }

    /**
     * 性别检测
     *
     * @param imageData 图像数据
     */
    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();
    }

    /**
     * 3D信息检测
     */
    public Face3DAngle face3DAngle(byte[] imageData) {
        this.ageAndGenderProcess(imageData);
        //3D信息检测
        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;
    }

    /**
     * @param imageData 图像数据
     * @return string 3D 角度信息
     */
    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;
    }

    /**
     * 活体检测
     *
     * @param imageData 图像信息
     */
    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();
    }

    /**
     * 获取IR活体信息
     * @param imageData 图像信息
     * @return int
     */
    public int getLivenessIr(byte[] imageData) {
        this.ageAndGenderProcess(imageData);
        //IR活体检测
        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;
    }

    /**
     * 获取版本信息
     *
     * @return VersionInfo
     */
    public VersionInfo getVersion(){
        return this.faceEngine.getVersion();
    }

    /**
     * 功能配置
     * @return functionConfiguration
     */
    private FunctionConfiguration setFunctionConfiguration(){
        FunctionConfiguration functionConfiguration = new FunctionConfiguration();
        //是否支持年龄检测功能
        functionConfiguration.setSupportAge(true);
        //是否支持3D检测功能
        functionConfiguration.setSupportFace3dAngle(true);
        //是否支持人脸检测功能
        functionConfiguration.setSupportFaceDetect(true);
        //是否支持人脸识别功能
        functionConfiguration.setSupportFaceRecognition(true);
        //是否支持性别检测功能
        functionConfiguration.setSupportGender(true);
        //是否支持RGB活体检测功能
        functionConfiguration.setSupportLiveness(true);
        //是否支持IR活体检测功能
        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_ALL_OUT);
        //0 度 正脸
        engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_0_ONLY);
        engineConfiguration.setDetectFaceMaxNum(10);
        //image 推荐 32 , video 推荐 16
        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;
    }

    /**
     * int gender 性别,,未知性别=-1 、男性=0 、女性=1
     * @param file 图像文件
     * @return gender
     */
    @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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值