首頁 > 軟體

Java基於虹軟實現臉部辨識、人臉比對、活性檢測等

2022-02-21 16:00:43

虹軟

  • 免費,高階版本試用
  • 支援線上、離線
  • 有 Java SDK,C++ SDK

一、註冊虹軟開發者平臺

點選註冊

註冊完成後可在“我的應用”中新建應用,獲得 APP_IDSDK_Key,請記住這兩個資訊,後續 SDK 中會用到。

接下來下載SDK就行了。

二、開始使用SDK

SDK包結構
在下載的sdk包中,包結構大概是這樣

|—demo
| |—ArcFaceDemo Demo工程
|—doc
| |—ARCSOFT_ARC_FACE_DEVELOPER’S_GUIDE.PDF 開發說明檔案
|—inc
| |—amcomdef.h 平臺檔案
| |—asvloffscreen.h 平臺檔案
| |—arcsoft_face_sdk.h 介面檔案
| |—merror.h 錯誤碼檔案
|—lib
|—|---Win32/x64
| |—|---libarcsoft_face.dll 演演算法庫
| |—|---libarcsoft_face_engine.dll 引擎庫
| |—|---libarcsoft_face_engine.lib 引擎庫
|—samplecode
| |—samplecode.cpp 範例程式碼
|—releasenotes.txt 說明檔案

在專案中引入 SDK 包

<dependency>
    <groupId>arcsoft</groupId>
    <artifactId>arcsoft-sdk-face</artifactId>
    <version>3.0.0.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/arcsoft-sdk-face-3.0.0.0.jar</systemPath>
</dependency>

簡單的整合

package com.study;

import com.arcsoft.face.*;
import com.arcsoft.face.enums.*;
import com.arcsoft.face.toolkit.ImageFactory;
import com.arcsoft.face.toolkit.ImageInfo;
import com.arcsoft.face.toolkit.ImageInfoEx;
import com.study.exception.CustomException;
import com.study.vo.FaceDetailInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * 整合虹軟-臉部辨識測試
 *
 * @author ouyangrongtao
 * @since 2022-02-20 19:12
 */
public class FaceEngineMain {

    // 從上述的開發者平臺-「我的應用」 獲取
    private static final String APP_ID = "";
    private static final String SDK_KEY = "";

    // sdk安裝路徑
    private static final String ARC_FACE_PATH = "arcsoft";

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

    public static void main(String[] args) {
        FaceEngineMain faceEngineMain = new FaceEngineMain();
        // 啟用
        FaceEngine faceEngine = faceEngineMain.active();
        // 識別功能設定
        FunctionConfiguration functionConfiguration = faceEngineMain.getFunctionConfiguration();
        // 初始化識別引擎
        faceEngineMain.initEngine(faceEngine, functionConfiguration);

        ImageInfo imageInfo = ImageFactory.getRGBData(new File("d:\aaa.jpeg"));
        ImageInfo imageInfo2 = ImageFactory.getRGBData(new File("d:\bbb.jpeg"));

        // 人臉檢測&特徵提取1
        List<FaceDetailInfo> faceDetailInfoList1 = faceEngineMain.detectFaces(faceEngine, imageInfo);

        // 人臉檢測&特徵提取2
        List<FaceDetailInfo> faceDetailInfoList2 = faceEngineMain.detectFaces(faceEngine, imageInfo2);

        /*
         * 特徵比對
         * 用於證件照或生活照與證件照之間的特徵比對,推薦閾值0.82
         * 用於生活照之間的特徵比對,推薦閾值0.80
         */
        FaceSimilar faceSimilar = faceEngineMain.compareFaceFeature(faceEngine,
                faceDetailInfoList1.get(0).getFaceFeature(), faceDetailInfoList2.get(0).getFaceFeature());
        LOGGER.info("相似度:{}", faceSimilar.getScore());

        // 獲取人臉屬性
        faceEngineMain.getFaceAttributes(faceEngine, imageInfo);

        ImageInfo imageInfo3 = ImageFactory.getRGBData(new File("d:\ccc.jpg"));
        ImageInfo imageInfo4 = ImageFactory.getRGBData(new File("d:\ddd.jpg"));

        // 人臉檢測&特徵提取3
        List<FaceDetailInfo> faceDetailInfoList3 = faceEngineMain.detectFacesEx(faceEngine, imageInfo3, DetectModel.ASF_DETECT_MODEL_RGB);

        // 人臉檢測&特徵提取4
        List<FaceDetailInfo> faceDetailInfoList4 = faceEngineMain.detectFacesEx(faceEngine, imageInfo4, DetectModel.ASF_DETECT_MODEL_RGB);

        // 特徵比對
        FaceSimilar faceSimilar2 = faceEngineMain.compareFaceFeature(faceEngine,
                faceDetailInfoList3.get(0).getFaceFeature(), faceDetailInfoList4.get(0).getFaceFeature(), CompareModel.LIFE_PHOTO);
        /*
         * 特徵比對
         * 用於證件照或生活照與證件照之間的特徵比對,推薦閾值0.82
         * 用於生活照之間的特徵比對,推薦閾值0.80
         */
        LOGGER.info("相似度:{}", faceSimilar2.getScore());

        // 獲取人臉屬性
        faceEngineMain.getFaceAttributesEx(faceEngine, imageInfo);

        ImageInfo imageInfoGray = ImageFactory.getGrayData(new File("d:\ddd.jpg"));

        // 活體檢測 RGB & IR
        faceEngineMain.getLiveness(faceEngine, imageInfo, imageInfoGray);

        // 解除安裝
        faceEngineMain.unInit(faceEngine);
    }

    /**
     * 活體檢測
     * @param faceEngine 引擎
     * @param imageInfoRGB RGB圖片資訊
     * @param imageInfoGray Gray圖片資訊
     */
    private void getLiveness(FaceEngine faceEngine, ImageInfo imageInfoRGB, ImageInfo imageInfoGray) {
        // 人臉檢測
        List<FaceInfo> faceInfoList = new ArrayList<>();
        faceEngine.detectFaces(imageInfoRGB.getImageData(),
                imageInfoRGB.getWidth(), imageInfoRGB.getHeight(), imageInfoRGB.getImageFormat(), faceInfoList);
        // 設定活體測試閥值
        faceEngine.setLivenessParam(0.5f, 0.7f);

        // RGB人臉檢測
        FunctionConfiguration configuration = new FunctionConfiguration();
        configuration.setSupportLiveness(true);
        faceEngine.process(imageInfoRGB.getImageData(),
                imageInfoRGB.getWidth(), imageInfoRGB.getHeight(), imageInfoRGB.getImageFormat(), faceInfoList, configuration);

        // RGB活體檢測
        List<LivenessInfo> livenessInfoList = new ArrayList<>();
        faceEngine.getLiveness(livenessInfoList);
        LOGGER.info("RGB活體:{}", livenessInfoList.get(0).getLiveness());

        // IR屬性處理
        List<FaceInfo> faceInfoListGray = new ArrayList<>();
        // IR人臉檢查
        faceEngine.detectFaces(imageInfoGray.getImageData(),
                imageInfoGray.getWidth(), imageInfoGray.getHeight(), imageInfoGray.getImageFormat(), faceInfoListGray);

        configuration = new FunctionConfiguration();
        configuration.setSupportIRLiveness(true);
        faceEngine.processIr(imageInfoGray.getImageData(),
                imageInfoGray.getWidth(), imageInfoGray.getHeight(), imageInfoGray.getImageFormat(), faceInfoListGray, configuration);

        //IR活體檢測
        List<IrLivenessInfo> irLivenessInfo = new ArrayList<>();
        faceEngine.getLivenessIr(irLivenessInfo);
        LOGGER.info("IR活體:{}", irLivenessInfo.get(0).getLiveness());
    }

    /**
     * 人臉屬性檢測
     * @param faceEngine 引擎
     * @param imageInfo 圖片資訊
     */
    private void getFaceAttributesEx(FaceEngine faceEngine, ImageInfo imageInfo) {
        // 人臉檢測
        List<FaceInfo> faceInfoList = new ArrayList<>();
        faceEngine.detectFaces(imageInfo.getImageData(),
                imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList);

        ImageInfoEx imageInfoEx = new ImageInfoEx();
        imageInfoEx.setHeight(imageInfo.getHeight());
        imageInfoEx.setWidth(imageInfo.getWidth());
        imageInfoEx.setImageFormat(imageInfo.getImageFormat());
        imageInfoEx.setImageDataPlanes(new byte[][]{imageInfo.getImageData()});
        imageInfoEx.setImageStrides(new int[]{imageInfo.getWidth() * 3});

        //人臉屬性檢測
        FunctionConfiguration configuration = new FunctionConfiguration();
        configuration.setSupportGender(true);
        configuration.setSupportAge(true);
        configuration.setSupportFace3dAngle(true);
        faceEngine.process(imageInfoEx, faceInfoList, configuration);

        //性別檢測
        List<GenderInfo> genderInfoList = new ArrayList<>();
        faceEngine.getGender(genderInfoList);
        LOGGER.info("性別:{}", genderInfoList.get(0).getGender());

        //年齡檢測
        List<AgeInfo> ageInfoList = new ArrayList<>();
        faceEngine.getAge(ageInfoList);
        LOGGER.info("年齡:{}", ageInfoList.get(0).getAge());

        //3D資訊檢測
        List<Face3DAngle> face3DAngleList = new ArrayList<>();
        faceEngine.getFace3DAngle(face3DAngleList);
        Face3DAngle face3DAngle = face3DAngleList.get(0);
        LOGGER.info("3D角度:{}", face3DAngle.getPitch() + "," + face3DAngle.getRoll() + "," + face3DAngle.getYaw());
    }

    /**
     * 人臉屬性檢測
     * @param faceEngine 引擎
     * @param imageInfo 圖片資訊
     */
    private void getFaceAttributes(FaceEngine faceEngine, ImageInfo imageInfo) {
        //人臉屬性檢測
        FunctionConfiguration configuration = new FunctionConfiguration();
        configuration.setSupportGender(true);
        configuration.setSupportAge(true);
        configuration.setSupportFace3dAngle(true);

        // 人臉檢測
        List<FaceInfo> faceInfoList = new ArrayList<>();
        faceEngine.detectFaces(imageInfo.getImageData(),
                imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList);

        faceEngine.process(imageInfo.getImageData(),
                imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList, configuration);

        //性別檢測
        List<GenderInfo> genderInfoList = new ArrayList<>();
        faceEngine.getGender(genderInfoList);
        LOGGER.info("性別:{}", genderInfoList.get(0).getGender());

        //年齡檢測
        List<AgeInfo> ageInfoList = new ArrayList<>();
        faceEngine.getAge(ageInfoList);
        LOGGER.info("年齡:{}", ageInfoList.get(0).getAge());

        //3D資訊檢測
        List<Face3DAngle> face3DAngleList = new ArrayList<>();
        faceEngine.getFace3DAngle(face3DAngleList);
        Face3DAngle face3DAngle = face3DAngleList.get(0);
        LOGGER.info("3D角度:{}", face3DAngle.getPitch() + "," + face3DAngle.getRoll() + "," + face3DAngle.getYaw());
    }

    /**
     * 特徵比對-可設定比對模型
     * @param faceEngine 引擎
     * @param sourceFaceFeature 原特徵值
     * @param targetFaceFeature 比對的特徵值
     * @param compareModel 比對模型
     * @return 比對結果
     */
    private FaceSimilar compareFaceFeature(FaceEngine faceEngine, FaceFeature sourceFaceFeature, FaceFeature targetFaceFeature, CompareModel compareModel) {
        // 特徵比對
        FaceSimilar faceSimilar = new FaceSimilar();
        int errorCode = faceEngine.compareFaceFeature(targetFaceFeature, sourceFaceFeature, compareModel, faceSimilar);
        if (ErrorInfo.MOK.getValue() != errorCode) {
            LOGGER.error("人臉特徵比對失敗");
        }

        return faceSimilar;
    }

    /**
     * 特徵比對
     * @param faceEngine 引擎
     * @param sourceFaceFeature 原特徵值
     * @param targetFaceFeature 比對的特徵值
     * @return 比對結果
     */
    private FaceSimilar compareFaceFeature(FaceEngine faceEngine, FaceFeature sourceFaceFeature, FaceFeature targetFaceFeature) {
        // 特徵比對
        FaceSimilar faceSimilar = new FaceSimilar();
        int errorCode = faceEngine.compareFaceFeature(targetFaceFeature, sourceFaceFeature, faceSimilar);
        if (ErrorInfo.MOK.getValue() != errorCode) {
            LOGGER.error("人臉特徵比對失敗");
        }

        return faceSimilar;
    }

    /**
     * 人臉檢測&特徵提取--可設定檢測模式
     * @param faceEngine 引擎
     * @param imageInfo 圖片資訊
     * @param detectModel 檢測模式
     * @return 人臉資訊
     */
    private List<FaceDetailInfo> detectFacesEx(FaceEngine faceEngine, ImageInfo imageInfo, DetectModel detectModel) {
        ImageInfoEx imageInfoEx = new ImageInfoEx();
        imageInfoEx.setHeight(imageInfo.getHeight());
        imageInfoEx.setWidth(imageInfo.getWidth());
        imageInfoEx.setImageFormat(imageInfo.getImageFormat());
        imageInfoEx.setImageDataPlanes(new byte[][]{imageInfo.getImageData()});
        imageInfoEx.setImageStrides(new int[]{imageInfo.getWidth() * 3});

        List<FaceInfo> faceInfoList = new ArrayList<>();
        faceEngine.detectFaces(imageInfoEx, detectModel, faceInfoList);

        List<FaceDetailInfo> faceDetailInfoList = new ArrayList<>(faceInfoList.size());
        for (FaceInfo faceInfo : faceInfoList) {
            LOGGER.info("imageInfoEx 人臉檢測結果: {}", faceInfo);
            FaceFeature faceFeature = new FaceFeature();
            faceEngine.extractFaceFeature(imageInfoEx, faceInfo, faceFeature);

            LOGGER.info("imageInfoEx 特徵值大小:{}", faceFeature.getFeatureData().length);

            FaceDetailInfo faceDetailInfo = new FaceDetailInfo(faceInfo, faceFeature);
            faceDetailInfoList.add(faceDetailInfo);
        }

        return faceDetailInfoList;
    }

    /**
     * 人臉檢測&特徵提取
     * @param faceEngine 引擎
     * @param imageInfo 圖片資訊
     * @return 人臉資訊
     */
    private List<FaceDetailInfo> detectFaces(FaceEngine faceEngine, ImageInfo imageInfo) {
        // 人臉檢測
        List<FaceInfo> faceInfoList = new ArrayList<>();
        faceEngine.detectFaces(imageInfo.getImageData(),
                imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList);

        List<FaceDetailInfo> faceDetailInfoList = new ArrayList<>(faceInfoList.size());
        // 特徵提取
        for (FaceInfo faceInfo : faceInfoList) {
            LOGGER.info("人臉檢測結果: {}", faceInfo);

            FaceFeature faceFeature = new FaceFeature();
            faceEngine.extractFaceFeature(imageInfo.getImageData(),
                    imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfo, faceFeature);

            LOGGER.info("特徵值大小:{}", faceFeature.getFeatureData().length);

            FaceDetailInfo faceDetailInfo = new FaceDetailInfo(faceInfo, faceFeature);
            faceDetailInfoList.add(faceDetailInfo);
        }

        return faceDetailInfoList;
    }

    /**
     * 初始化識別引擎
     * @param faceEngine 臉部辨識引擎
     * @param functionConfiguration 功能設定
     */
    private void initEngine(FaceEngine faceEngine, FunctionConfiguration functionConfiguration) {
        // 引擎設定
        EngineConfiguration engineConfiguration = new EngineConfiguration();
        engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
        engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_ALL_OUT);
        engineConfiguration.setDetectFaceMaxNum(10);
        engineConfiguration.setDetectFaceScaleVal(16);

        engineConfiguration.setFunctionConfiguration(functionConfiguration);

        // 初始化引擎
        int errorCode = faceEngine.init(engineConfiguration);
        if (errorCode != ErrorInfo.MOK.getValue()) {
            throw new CustomException("初始化引擎失敗");
        }
    }

    /**
     * 識別功能設定
     */
    private FunctionConfiguration getFunctionConfiguration() {
        // 功能設定
        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;
    }

    /**
     * 啟用 初次使用SDK時需要對SDK先進行啟用,啟用後無需重複呼叫;呼叫此介面時必須為聯網狀態,啟用成功後即可離線使用;
     * @return FaceEngine 物件
     */
    private FaceEngine active() {
        URL resource = ClassLoader.getSystemResource(ARC_FACE_PATH);
        LOGGER.info("軟體安裝目錄:{}", resource);

        FaceEngine faceEngine = new FaceEngine(resource.getPath());

        ActiveFileInfo activeFileInfo = new ActiveFileInfo();
        int errorCode = faceEngine.getActiveFileInfo(activeFileInfo);
        if (errorCode != ErrorInfo.MOK.getValue()
                && errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
            LOGGER.info("獲取啟用檔案資訊失敗");
        }

        // 首次啟用
        errorCode = faceEngine.activeOnline(APP_ID, SDK_KEY);
        if (errorCode != ErrorInfo.MOK.getValue()
                && errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
            throw new CustomException("引擎啟用失敗");
        }

        LOGGER.info("啟用資訊:{}", activeFileInfo);

        return faceEngine;
    }

    /**
     * 解除安裝引擎
     * @param faceEngine 臉部辨識引擎
     */
    private void unInit(FaceEngine faceEngine) {
        faceEngine.unInit();
    }
}

效能資訊(參考官方檔案)

閥值設定推薦(參考官方檔案)

  1. 活體取值範圍為[0~1],推薦閾值如下,高於此閾值的即可判斷為活體。
    - RGB 活體:0.5
    - IR 活體:0.7

  2. 人臉比對取值範圍為[0~1],推薦閾值如下,高於此閾值的即可判斷為同一人。
    - 用於生活照之間的特徵比對,推薦閾值0.80
    - 用於證件照或生活照與證件照之間的特徵比對,推薦閾值0.82

產品檔案 https://ai.arcsoft.com.cn/manual/docs

 到此這篇關於Java基於虹軟實現臉部辨識、人臉比對、活性檢測等的文章就介紹到這了,更多相關Java 臉部辨識、人臉比對、活性檢測內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com