unity接入實現人臉識別應用-基於虹軟人臉識別算法4.0

一、準備工作

1、下載虹軟人臉識別增值版SDK 4.0

1)註冊並登錄開發者中心

2)下載虹軟人臉識別SDK

2、安裝Unity3D及Visual Studio 2019開發環境

1)安裝Unity Hub

2)安裝Unity 2020.3.1f1c1

二、創建DEMO工程

1、創建Unity工程

2、引入虹軟人臉識別SDK

3、項目工程目錄說明

三、運行調試程序

1、虹軟人臉識別SDK在線激活

2、可以進行人臉識別

四、核心代碼說明

1、關於System.Drawing.dll缺失的問題說明

2、關於Unity中使用選擇.NET版本的說明

3、人臉識別核心代碼分析

五、DEMO源碼下載 ————————————————

一、準備工作

1、下載虹軟人臉識別增值版SDK 4.0

1)註冊並登錄開發者中心

訪問https://www.arcsoft.com.cn/登錄後創建新應用並添加SDK 在這裏插入圖片描述

2)下載虹軟人臉識別SDK

點擊上圖中V4.0版本的下載箭頭可以下載SDK

2、安裝Unity3D及Visual Studio 2019開發環境

1)安裝Unity Hub

訪問https://unity.cn/並下載安裝Unity Hub

2)安裝Unity 2020.3.1f1c1

Unity Hub安裝完成後打開,依據下圖內容安裝Unity 2020.3.1f1c1,注意:勾選Visual Studio可以下載VS開發環境! 在這裏插入圖片描述

二、創建DEMO工程

1、創建Unity工程

Unity Hub-》項目-》新建-》創建即可完成unity工程的創建 在這裏插入圖片描述

2、引入虹軟人臉識別SDK

下載完成虹軟人臉識別SDK後解壓-》lib-》X64即可看到libarcsoft_face.dll和libarcsoft_face_engine.dll兩個動態庫文件 在這裏插入圖片描述 注意:在unity工程的下圖中紅色箭頭標註,32位和64位的Arcface SDK需要分別勾選x86和x64,libarcsoft_face.dll和libarcsoft_face_engine.dll兩個文件都需要此操作! 在這裏插入圖片描述

3、項目工程目錄說明

下圖中1、2部分取自官網的VS窗體Demo,3部分取自虹軟人臉識別SDK,4部分是在VS窗體Demo的基礎上進行修改的類。 在這裏插入圖片描述

三、運行調試程序

1、虹軟人臉識別SDK在線激活

在這裏插入圖片描述

2、可以進行人臉識別

四、核心代碼說明

1、關於System.Drawing.dll缺失的問題說明

注意:如果代碼中只引入libarcsoft_face.dll和libarcsoft_face_engine.dll會報錯System.Drawing.dll文件丟失

可以在unity的安裝目錄~\2020.3.1f1c1\Editor\Data\MonoBleedingEdge\lib\mono\4.7.1-api\下找到該文件並複製進入工程內

2、關於Unity中使用選擇.NET版本的說明

在unity工程的player設置中選擇Scripting Backend位Mono,Api Compatibility Level*爲.NET 4.x

在這裏插入圖片描述

3、人臉識別核心代碼分析

1)攝像頭初始化 WebCamTexture.devices

    public RawImage rawimage;
    WebCamTexture webCamTexture;
 
    public Text webCamDisplayText;
 
    void Start()
    {
        WebCamDevice[] cam_devices = WebCamTexture.devices;
        // for debugging purposes, prints available devices to the console
        for (int i = 0; i < cam_devices.Length; i++)
        {
            print("Webcam available: " + cam_devices[i].name);
        }
 
        GoWebCam01();
 
        InitEngines();
 
        btnStartVideo_Click(new object(), new EventArgs());
    }
 
    //CAMERA 01 SELECT
    public void GoWebCam01()
    {
        WebCamDevice[] cam_devices = WebCamTexture.devices;
        // for debugging purposes, prints available devices to the console
        for (int i = 0; i < cam_devices.Length; i++)
        {
            print("Webcam available: " + cam_devices[i].name);
        }
 
        webCamTexture = new WebCamTexture(cam_devices[0].name, 1280, 720, 30);
        rawimage.texture = webCamTexture;
        if (webCamTexture != null)
        {
            //webCamTexture.Play();
            Debug.Log("Web Cam Connected : " + webCamTexture.deviceName + "\n");
        }
        webCamDisplayText.text = "Camera Type: " + cam_devices[0].name.ToString();
    }

2)圖片格式轉換 Texture2D to Image

    public static Image Texture2Image(Texture2D texture)
    {
        if (texture == null)
        {
            return null;
        }
        //Save the texture to the stream.
        byte[] bytes = texture.EncodeToPNG();
 
        //Memory stream to store the bitmap data.
        MemoryStream ms = new MemoryStream(bytes);
 
        //Seek the beginning of the stream.
        ms.Seek(0, SeekOrigin.Begin);
 
        //Create an image from a stream.
        Image bmp2 = Bitmap.FromStream(ms);
 
        //Close the stream, we nolonger need it.
        ms.Close();
        ms = null;
 
        return bmp2;
    }

3)初始化人臉識別庫 注意:此代碼片段中appId sdkKey64 sdkKey32 activeKey64 activeKey32的值需要根據虹軟開發者中心的實際數值進行填寫

    private void InitEngines()
    {
        try
        {
            webCamDisplayText.text += "測試";
 
            //讀取配置文件
            //AppSettingsReader reader = new AppSettingsReader();
            //rgbCameraIndex = (int)reader.GetValue("RGB_CAMERA_INDEX", typeof(int));
            //irCameraIndex = (int)reader.GetValue("IR_CAMERA_INDEX", typeof(int));
            //frMatchTime = (int)reader.GetValue("FR_MATCH_TIME", typeof(int));
            //liveMatchTime = (int)reader.GetValue("LIVENESS_MATCH_TIME", typeof(int));
 
            AppSettingsReader reader = new AppSettingsReader();
            rgbCameraIndex = 0;
            irCameraIndex = 1;
            frMatchTime = 20;
            liveMatchTime = 20;
 
            int retCode = 0;
            bool isOnlineActive = true;//true(在線激活) or false(離線激活)
            try
            {
                if (isOnlineActive)
                {
                    #region 讀取在線激活配置信息
                    //string appId = (string)reader.GetValue("APPID", typeof(string));
                    //string sdkKey64 = (string)reader.GetValue("SDKKEY64", typeof(string));
                    //string sdkKey32 = (string)reader.GetValue("SDKKEY32", typeof(string));
                    //string activeKey64 = (string)reader.GetValue("ACTIVEKEY64", typeof(string));
                    //string activeKey32 = (string)reader.GetValue("ACTIVEKEY32", typeof(string));
 
                    string appId = "";
                    string sdkKey64 = "";
                    string sdkKey32 = "";
                    string activeKey64 = "";
                    string activeKey32 = "";
 
                    webCamDisplayText.text += "111111";
 
                    //判斷CPU位數
                    var is64CPU = Environment.Is64BitProcess;
                    if (string.IsNullOrWhiteSpace(appId) || string.IsNullOrWhiteSpace(is64CPU ? sdkKey64 : sdkKey32) || string.IsNullOrWhiteSpace(is64CPU ? activeKey64 : activeKey32))
                    {
                        Debug.LogError(string.Format("請在App.config配置文件中先配置APP_ID和SDKKEY{0}、ACTIVEKEY{0}!", is64CPU ? "64" : "32"));
                        //MessageBox.Show(string.Format("請在App.config配置文件中先配置APP_ID和SDKKEY{0}、ACTIVEKEY{0}!", is64CPU ? "64" : "32"));
 
                        //System.Environment.Exit(0);
                        Quit();
                    }
                    #endregion
 
                    webCamDisplayText.text += "準備激活";
 
                    //在線激活引擎    如出現錯誤,1.請先確認從官網下載的sdk庫已放到對應的bin中,2.當前選擇的CPU爲x86或者x64
                    retCode = imageEngine.ASFOnlineActivation(appId, is64CPU ? sdkKey64 : sdkKey32, is64CPU ? activeKey64 : activeKey32);
 
                    webCamDisplayText.text += "激活完成";
                }
                else
                {
                    #region 讀取離線激活配置信息
                    string offlineActiveFilePath = (string)reader.GetValue("OfflineActiveFilePath", typeof(string));
                    if (string.IsNullOrWhiteSpace(offlineActiveFilePath) || !File.Exists(offlineActiveFilePath))
                    {
                        string deviceInfo;
                        retCode = imageEngine.ASFGetActiveDeviceInfo(out deviceInfo);
                        if (retCode != 0)
                        {
                            Debug.LogError("獲取設備信息失敗,錯誤碼:" + retCode);
                            //MessageBox.Show("獲取設備信息失敗,錯誤碼:" + retCode);
                        }
                        else
                        {
                            File.WriteAllText("ActiveDeviceInfo.txt", deviceInfo);
                            Debug.LogError("獲取設備信息成功,已保存到運行根目錄ActiveDeviceInfo.txt文件,請在官網執行離線激活操作,將生成的離線授權文件路徑在App.config裏配置後再重新運行");
                            //MessageBox.Show("獲取設備信息成功,已保存到運行根目錄ActiveDeviceInfo.txt文件,請在官網執行離線激活操作,將生成的離線授權文件路徑在App.config裏配置後再重新運行");
                        }
                        //System.Environment.Exit(0);
                        Quit();
                    }
                    #endregion
                    //離線激活
                    retCode = imageEngine.ASFOfflineActivation(offlineActiveFilePath);
                }
                if (retCode != 0 && retCode != 90114)
                {
                    Debug.LogError("激活SDK失敗,錯誤碼:" + retCode);
                    //MessageBox.Show("激活SDK失敗,錯誤碼:" + retCode);
                    //System.Environment.Exit(0);
                    Quit();
                }
 
                webCamDisplayText.text += retCode.ToString();
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("無法加載 DLL"))
                {
                    Debug.LogError("請將SDK相關DLL放入bin對應的x86或x64下的文件夾中!");
                    //MessageBox.Show("請將SDK相關DLL放入bin對應的x86或x64下的文件夾中!");
                }
                else
                {
                    Debug.LogError("激活SDK失敗,請先檢查依賴環境及SDK的平臺、版本是否正確!");
                    //MessageBox.Show("激活SDK失敗,請先檢查依賴環境及SDK的平臺、版本是否正確!");
                }
                //System.Environment.Exit(0);
                Quit();
            }
 
            //初始化引擎
            DetectionMode detectMode = DetectionMode.ASF_DETECT_MODE_IMAGE;
            //Video模式下檢測臉部的角度優先值
            ASF_OrientPriority videoDetectFaceOrientPriority = ASF_OrientPriority.ASF_OP_ALL_OUT;
            //Image模式下檢測臉部的角度優先值
            ASF_OrientPriority imageDetectFaceOrientPriority = ASF_OrientPriority.ASF_OP_ALL_OUT;
            //最大需要檢測的人臉個數
            int detectFaceMaxNum = 6;
            //引擎初始化時需要初始化的檢測功能組合
            int combinedMask = FaceEngineMask.ASF_FACE_DETECT | FaceEngineMask.ASF_FACERECOGNITION | FaceEngineMask.ASF_AGE | FaceEngineMask.ASF_GENDER | FaceEngineMask.ASF_FACE3DANGLE | FaceEngineMask.ASF_IMAGEQUALITY | FaceEngineMask.ASF_MASKDETECT;
            //初始化引擎,正常值爲0,其他返回值請參考http://ai.arcsoft.com.cn/bbs/forum.php?mod=viewthread&tid=19&_dsign=dbad527e
            retCode = imageEngine.ASFInitEngine(detectMode, imageDetectFaceOrientPriority, detectFaceMaxNum, combinedMask);
            Console.WriteLine("InitEngine Result:" + retCode);
            AppendText((retCode == 0) ? "圖片引擎初始化成功!" : string.Format("圖片引擎初始化失敗!錯誤碼爲:{0}", retCode));
            if (retCode != 0)
            {
                //禁用相關功能按鈕
                //ControlsEnable(false, chooseMultiImgBtn, matchBtn, btnClearFaceList, chooseImgBtn);
            }
 
            //初始化視頻模式下人臉檢測引擎
            DetectionMode detectModeVideo = DetectionMode.ASF_DETECT_MODE_VIDEO;
            int combinedMaskVideo = FaceEngineMask.ASF_FACE_DETECT | FaceEngineMask.ASF_FACERECOGNITION | FaceEngineMask.ASF_FACELANDMARK;
            retCode = videoEngine.ASFInitEngine(detectModeVideo, videoDetectFaceOrientPriority, detectFaceMaxNum, combinedMaskVideo);
            AppendText((retCode == 0) ? "視頻引擎初始化成功!" : string.Format("視頻引擎初始化失敗!錯誤碼爲:{0}", retCode));
            if (retCode != 0)
            {
                //禁用相關功能按鈕
                //ControlsEnable(false, chooseMultiImgBtn, matchBtn, btnClearFaceList, chooseImgBtn);
            }
 
            //RGB視頻專用FR引擎
            combinedMask = FaceEngineMask.ASF_FACE_DETECT | FaceEngineMask.ASF_FACERECOGNITION | FaceEngineMask.ASF_LIVENESS | FaceEngineMask.ASF_MASKDETECT;
            retCode = videoRGBImageEngine.ASFInitEngine(detectMode, videoDetectFaceOrientPriority, detectFaceMaxNum, combinedMask);
            AppendText((retCode == 0) ? "RGB處理引擎初始化成功!" : string.Format("RGB處理引擎初始化失敗!錯誤碼爲:{0}", retCode));
            if (retCode != 0)
            {
                //禁用相關功能按鈕
                //ControlsEnable(false, chooseMultiImgBtn, matchBtn, btnClearFaceList, chooseImgBtn);
            }
            //設置活體閾值
            videoRGBImageEngine.ASFSetLivenessParam(thresholdRgb);
 
            //IR視頻專用FR引擎
            combinedMask = FaceEngineMask.ASF_FACE_DETECT | FaceEngineMask.ASF_FACERECOGNITION | FaceEngineMask.ASF_IR_LIVENESS;
            retCode = videoIRImageEngine.ASFInitEngine(detectModeVideo, videoDetectFaceOrientPriority, detectFaceMaxNum, combinedMask);
            AppendText((retCode == 0) ? "IR處理引擎初始化成功!\r\n" : string.Format("IR處理引擎初始化失敗!錯誤碼爲:{0}\r\n", retCode));
            if (retCode != 0)
            {
                //禁用相關功能按鈕
                //ControlsEnable(false, chooseMultiImgBtn, matchBtn, btnClearFaceList, chooseImgBtn);
            }
            //設置活體閾值
            videoIRImageEngine.ASFSetLivenessParam(thresholdRgb, thresholdIr);
 
            initVideo();
        }
        catch (Exception ex)
        {
            LogUtil.LogInfo(GetType(), ex);
            Debug.LogError("程序初始化異常,請在App.config中修改日誌配置,根據日誌查找原因!");
            //MessageBox.Show("程序初始化異常,請在App.config中修改日誌配置,根據日誌查找原因!");
 
            Quit();
            //System.Environment.Exit(0);
        }
    }

五、DEMO源碼下載 鏈接:https://pan.baidu.com/s/1FXs94jbAEseoERpVDzysFA 提取碼:iabc 複製這段內容後打開百度網盤手機App,操作更方便哦

注意:DEMO工程只是實現了攝像頭初始化、採集圖片格式轉換、虹軟人臉在線註冊和人臉特徵值實時提取等功能,在使用圖片進行人臉註冊功能時會報錯,未解決!

提供思路:可以將人臉註冊的功能單獨提取並將特徵值存入數據庫,然後從unity中讀取特徵值進行人臉數據的比對。

瞭解更多人臉識別產品相關內容請到虹軟視覺開放平臺

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章