spring-boot人脸检测整合虹软sdk

添加依赖

    <dependencies>
        <dependency>
            <groupId>com.arcsoft.facegroupId>
            <artifactId>arcsoft-sdk-faceartifactId>
            <version>2.2.0.1version>
            <scope>systemscope>
            <systemPath>${basedir}/lib/arcsoft-sdk-face-2.2.0.1.jarsystemPath>
        dependency>
    dependencies>

创建lib文件夹,

  • 引入WIN64文件下所有dll
  • 引入arcsoft-sdk-face-2.2.0.1.jar

在build下添加

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <includeSystemScope>trueincludeSystemScope>
                configuration>
            plugin>
        plugins>
    build>

激活引擎

import com.arcsoft.face.FaceEngine;
import com.arcsoft.face.enums.ErrorInfo;

public class faceActive {
    public static void main(String[] args) {
        //填自己的AppID和SDKkey
        //记得把前缀删掉
        String appId = "";
        String sdkKey = "";

        //dll文件所在目录,绝对路径
        FaceEngine faceEngine = new FaceEngine("D:\\Projects\\JavawebProjects\\test\\lib\\WIN64");
        //激活引擎
        int activeCode = faceEngine.activeOnline(appId, sdkKey);

        if (activeCode != ErrorInfo.MOK.getValue() && activeCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
            System.out.println("引擎激活失败");
        }else {
            System.out.println("引擎激活成功");
        }
    }
}

在spring boot启动时初始化引擎

package club.twzw.face.demo;

import com.arcsoft.face.*;
import com.arcsoft.face.enums.DetectMode;
import com.arcsoft.face.enums.DetectOrient;
import com.arcsoft.face.enums.ErrorInfo;
import com.arcsoft.face.enums.ImageFormat;
import com.arcsoft.face.toolkit.ImageInfo;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static com.arcsoft.face.toolkit.ImageFactory.getRGBData;

@SpringBootApplication
public class DemoApplication {


    public static void main(String[] args) {

        FaceEngine faceEngine = activeEngine();
        ImageInfo imageInfo = getRGBData(new File("lib/imgs/timg.jpg"));

        List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
        List<AgeInfo> faceAgeInfoList = new ArrayList<AgeInfo>();
        List<GenderInfo> faceGanderInfoList = new ArrayList<GenderInfo>();
        faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList);


        //特征提取
        FaceFeature faceFeature = new FaceFeature();
        faceEngine.extractFaceFeature(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList.get(0), faceFeature);


        FunctionConfiguration configuration = new FunctionConfiguration();
        configuration.setSupportAge(true);
        configuration.setSupportFace3dAngle(true);
        configuration.setSupportGender(true);
        configuration.setSupportLiveness(true);
        faceEngine.process(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList, configuration);


        faceEngine.getAge(faceAgeInfoList);
        System.out.println("年龄: " + faceAgeInfoList.get(0).getAge());

        faceEngine.getGender(faceGanderInfoList);
        if (faceGanderInfoList.get(0).getGender() == 0) {
            System.out.println("性别: 男");
        }else {
            System.out.println("性别: 女" );
        }

        List<LivenessInfo> livenessInfoList = new ArrayList<LivenessInfo>();
        int livenessCode = faceEngine.getLiveness(livenessInfoList);
        System.out.println("活体:" + livenessInfoList.get(0).getLiveness());

    }


    public static FaceEngine activeEngine() {

        //填自己的AppID和SDKkey
        String appId = "";
        String sdkKey = "";

        //dll文件所在目录
        FaceEngine faceEngine = new FaceEngine("D:\\Projects\\JavawebProjects\\demo\\lib\\WIN64");
        //激活引擎
        int activeCode = faceEngine.activeOnline(appId, sdkKey);

        if (activeCode != ErrorInfo.MOK.getValue() && activeCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
            throw new ExceptionInInitializerError("failed to active engine");
        }
        //引擎配置
        EngineConfiguration engineConfiguration = new EngineConfiguration();
        engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
        engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_0_ONLY);

        //功能配置
        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);
        engineConfiguration.setFunctionConfiguration(functionConfiguration);


        //初始化引擎
        int initCode = faceEngine.init(engineConfiguration);

        if (initCode != ErrorInfo.MOK.getValue()) {
            throw new ExceptionInInitializerError("failed to init engine");
        }
        return faceEngine;
    }

}

你可能感兴趣的:(java)