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


一、准備工作

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

 1 public RawImage rawimage;
 2     WebCamTexture webCamTexture;
 3 
 4     public Text webCamDisplayText;
 5 
 6     void Start()
 7     {
 8         WebCamDevice[] cam_devices = WebCamTexture.devices;
 9         // for debugging purposes, prints available devices to the console
10         for (int i = 0; i < cam_devices.Length; i++)
11         {
12             print("Webcam available: " + cam_devices[i].name);
13         }
14 
15         GoWebCam01();
16 
17         InitEngines();
18 
19         btnStartVideo_Click(new object(), new EventArgs());
20     }
21 
22     //CAMERA 01 SELECT
23     public void GoWebCam01()
24     {
25         WebCamDevice[] cam_devices = WebCamTexture.devices;
26         // for debugging purposes, prints available devices to the console
27         for (int i = 0; i < cam_devices.Length; i++)
28         {
29             print("Webcam available: " + cam_devices[i].name);
30         }
31 
32         webCamTexture = new WebCamTexture(cam_devices[0].name, 1280, 720, 30);
33         rawimage.texture = webCamTexture;
34         if (webCamTexture != null)
35         {
36             //webCamTexture.Play();
37             Debug.Log("Web Cam Connected : " + webCamTexture.deviceName + "\n");
38         }
39         webCamDisplayText.text = "Camera Type: " + cam_devices[0].name.ToString();
40     }

2)圖片格式轉換 Texture2D to Image

 1     public static Image Texture2Image(Texture2D texture)
 2     {
 3         if (texture == null)
 4         {
 5             return null;
 6         }
 7         //Save the texture to the stream.
 8         byte[] bytes = texture.EncodeToPNG();
 9 
10         //Memory stream to store the bitmap data.
11         MemoryStream ms = new MemoryStream(bytes);
12 
13         //Seek the beginning of the stream.
14         ms.Seek(0, SeekOrigin.Begin);
15 
16         //Create an image from a stream.
17         Image bmp2 = Bitmap.FromStream(ms);
18 
19         //Close the stream, we nolonger need it.
20         ms.Close();
21         ms = null;
22 
23         return bmp2;
24     }

 3)初始化人臉識別庫

注意:此代碼片段中appId sdkKey64 sdkKey32 activeKey64 activeKey32的值需要根據虹軟開發者中心的實際數值進行填寫
  1 private void InitEngines()
  2     {
  3         try
  4         {
  5             webCamDisplayText.text += "測試";
  6 
  7             //讀取配置文件
  8             //AppSettingsReader reader = new AppSettingsReader();
  9             //rgbCameraIndex = (int)reader.GetValue("RGB_CAMERA_INDEX", typeof(int));
 10             //irCameraIndex = (int)reader.GetValue("IR_CAMERA_INDEX", typeof(int));
 11             //frMatchTime = (int)reader.GetValue("FR_MATCH_TIME", typeof(int));
 12             //liveMatchTime = (int)reader.GetValue("LIVENESS_MATCH_TIME", typeof(int));
 13 
 14             AppSettingsReader reader = new AppSettingsReader();
 15             rgbCameraIndex = 0;
 16             irCameraIndex = 1;
 17             frMatchTime = 20;
 18             liveMatchTime = 20;
 19 
 20             int retCode = 0;
 21             bool isOnlineActive = true;//true(在線激活) or false(離線激活)
 22             try
 23             {
 24                 if (isOnlineActive)
 25                 {
 26                     #region 讀取在線激活配置信息
 27                     //string appId = (string)reader.GetValue("APPID", typeof(string));
 28                     //string sdkKey64 = (string)reader.GetValue("SDKKEY64", typeof(string));
 29                     //string sdkKey32 = (string)reader.GetValue("SDKKEY32", typeof(string));
 30                     //string activeKey64 = (string)reader.GetValue("ACTIVEKEY64", typeof(string));
 31                     //string activeKey32 = (string)reader.GetValue("ACTIVEKEY32", typeof(string));
 32 
 33                     string appId = "";
 34                     string sdkKey64 = "";
 35                     string sdkKey32 = "";
 36                     string activeKey64 = "";
 37                     string activeKey32 = "";
 38 
 39                     webCamDisplayText.text += "111111";
 40 
 41                     //判斷CPU位數
 42                     var is64CPU = Environment.Is64BitProcess;
 43                     if (string.IsNullOrWhiteSpace(appId) || string.IsNullOrWhiteSpace(is64CPU ? sdkKey64 : sdkKey32) || string.IsNullOrWhiteSpace(is64CPU ? activeKey64 : activeKey32))
 44                     {
 45                         Debug.LogError(string.Format("請在App.config配置文件中先配置APP_ID和SDKKEY{0}、ACTIVEKEY{0}!", is64CPU ? "64" : "32"));
 46                         //MessageBox.Show(string.Format("請在App.config配置文件中先配置APP_ID和SDKKEY{0}、ACTIVEKEY{0}!", is64CPU ? "64" : "32"));
 47 
 48                         //System.Environment.Exit(0);
 49                         Quit();
 50                     }
 51                     #endregion
 52 
 53                     webCamDisplayText.text += "准備激活";
 54 
 55                     //在線激活引擎    如出現錯誤,1.請先確認從官網下載的sdk庫已放到對應的bin中,2.當前選擇的CPU為x86或者x64
 56                     retCode = imageEngine.ASFOnlineActivation(appId, is64CPU ? sdkKey64 : sdkKey32, is64CPU ? activeKey64 : activeKey32);
 57 
 58                     webCamDisplayText.text += "激活完成";
 59                 }
 60                 else
 61                 {
 62                     #region 讀取離線激活配置信息
 63                     string offlineActiveFilePath = (string)reader.GetValue("OfflineActiveFilePath", typeof(string));
 64                     if (string.IsNullOrWhiteSpace(offlineActiveFilePath) || !File.Exists(offlineActiveFilePath))
 65                     {
 66                         string deviceInfo;
 67                         retCode = imageEngine.ASFGetActiveDeviceInfo(out deviceInfo);
 68                         if (retCode != 0)
 69                         {
 70                             Debug.LogError("獲取設備信息失敗,錯誤碼:" + retCode);
 71                             //MessageBox.Show("獲取設備信息失敗,錯誤碼:" + retCode);
 72                         }
 73                         else
 74                         {
 75                             File.WriteAllText("ActiveDeviceInfo.txt", deviceInfo);
 76                             Debug.LogError("獲取設備信息成功,已保存到運行根目錄ActiveDeviceInfo.txt文件,請在官網執行離線激活操作,將生成的離線授權文件路徑在App.config里配置后再重新運行");
 77                             //MessageBox.Show("獲取設備信息成功,已保存到運行根目錄ActiveDeviceInfo.txt文件,請在官網執行離線激活操作,將生成的離線授權文件路徑在App.config里配置后再重新運行");
 78                         }
 79                         //System.Environment.Exit(0);
 80                         Quit();
 81                     }
 82                     #endregion
 83                     //離線激活
 84                     retCode = imageEngine.ASFOfflineActivation(offlineActiveFilePath);
 85                 }
 86                 if (retCode != 0 && retCode != 90114)
 87                 {
 88                     Debug.LogError("激活SDK失敗,錯誤碼:" + retCode);
 89                     //MessageBox.Show("激活SDK失敗,錯誤碼:" + retCode);
 90                     //System.Environment.Exit(0);
 91                     Quit();
 92                 }
 93 
 94                 webCamDisplayText.text += retCode.ToString();
 95             }
 96             catch (Exception ex)
 97             {
 98                 if (ex.Message.Contains("無法加載 DLL"))
 99                 {
100                     Debug.LogError("請將SDK相關DLL放入bin對應的x86或x64下的文件夾中!");
101                     //MessageBox.Show("請將SDK相關DLL放入bin對應的x86或x64下的文件夾中!");
102                 }
103                 else
104                 {
105                     Debug.LogError("激活SDK失敗,請先檢查依賴環境及SDK的平台、版本是否正確!");
106                     //MessageBox.Show("激活SDK失敗,請先檢查依賴環境及SDK的平台、版本是否正確!");
107                 }
108                 //System.Environment.Exit(0);
109                 Quit();
110             }
111 
112             //初始化引擎
113             DetectionMode detectMode = DetectionMode.ASF_DETECT_MODE_IMAGE;
114             //Video模式下檢測臉部的角度優先值
115             ASF_OrientPriority videoDetectFaceOrientPriority = ASF_OrientPriority.ASF_OP_ALL_OUT;
116             //Image模式下檢測臉部的角度優先值
117             ASF_OrientPriority imageDetectFaceOrientPriority = ASF_OrientPriority.ASF_OP_ALL_OUT;
118             //最大需要檢測的人臉個數
119             int detectFaceMaxNum = 6;
120             //引擎初始化時需要初始化的檢測功能組合
121             int combinedMask = FaceEngineMask.ASF_FACE_DETECT | FaceEngineMask.ASF_FACERECOGNITION | FaceEngineMask.ASF_AGE | FaceEngineMask.ASF_GENDER | FaceEngineMask.ASF_FACE3DANGLE | FaceEngineMask.ASF_IMAGEQUALITY | FaceEngineMask.ASF_MASKDETECT;
122             //初始化引擎,正常值為0,其他返回值請參考http://ai.arcsoft.com.cn/bbs/forum.php?mod=viewthread&tid=19&_dsign=dbad527e
123             retCode = imageEngine.ASFInitEngine(detectMode, imageDetectFaceOrientPriority, detectFaceMaxNum, combinedMask);
124             Console.WriteLine("InitEngine Result:" + retCode);
125             AppendText((retCode == 0) ? "圖片引擎初始化成功!" : string.Format("圖片引擎初始化失敗!錯誤碼為:{0}", retCode));
126             if (retCode != 0)
127             {
128                 //禁用相關功能按鈕
129                 //ControlsEnable(false, chooseMultiImgBtn, matchBtn, btnClearFaceList, chooseImgBtn);
130             }
131 
132             //初始化視頻模式下人臉檢測引擎
133             DetectionMode detectModeVideo = DetectionMode.ASF_DETECT_MODE_VIDEO;
134             int combinedMaskVideo = FaceEngineMask.ASF_FACE_DETECT | FaceEngineMask.ASF_FACERECOGNITION | FaceEngineMask.ASF_FACELANDMARK;
135             retCode = videoEngine.ASFInitEngine(detectModeVideo, videoDetectFaceOrientPriority, detectFaceMaxNum, combinedMaskVideo);
136             AppendText((retCode == 0) ? "視頻引擎初始化成功!" : string.Format("視頻引擎初始化失敗!錯誤碼為:{0}", retCode));
137             if (retCode != 0)
138             {
139                 //禁用相關功能按鈕
140                 //ControlsEnable(false, chooseMultiImgBtn, matchBtn, btnClearFaceList, chooseImgBtn);
141             }
142 
143             //RGB視頻專用FR引擎
144             combinedMask = FaceEngineMask.ASF_FACE_DETECT | FaceEngineMask.ASF_FACERECOGNITION | FaceEngineMask.ASF_LIVENESS | FaceEngineMask.ASF_MASKDETECT;
145             retCode = videoRGBImageEngine.ASFInitEngine(detectMode, videoDetectFaceOrientPriority, detectFaceMaxNum, combinedMask);
146             AppendText((retCode == 0) ? "RGB處理引擎初始化成功!" : string.Format("RGB處理引擎初始化失敗!錯誤碼為:{0}", retCode));
147             if (retCode != 0)
148             {
149                 //禁用相關功能按鈕
150                 //ControlsEnable(false, chooseMultiImgBtn, matchBtn, btnClearFaceList, chooseImgBtn);
151             }
152             //設置活體閾值
153             videoRGBImageEngine.ASFSetLivenessParam(thresholdRgb);
154 
155             //IR視頻專用FR引擎
156             combinedMask = FaceEngineMask.ASF_FACE_DETECT | FaceEngineMask.ASF_FACERECOGNITION | FaceEngineMask.ASF_IR_LIVENESS;
157             retCode = videoIRImageEngine.ASFInitEngine(detectModeVideo, videoDetectFaceOrientPriority, detectFaceMaxNum, combinedMask);
158             AppendText((retCode == 0) ? "IR處理引擎初始化成功!\r\n" : string.Format("IR處理引擎初始化失敗!錯誤碼為:{0}\r\n", retCode));
159             if (retCode != 0)
160             {
161                 //禁用相關功能按鈕
162                 //ControlsEnable(false, chooseMultiImgBtn, matchBtn, btnClearFaceList, chooseImgBtn);
163             }
164             //設置活體閾值
165             videoIRImageEngine.ASFSetLivenessParam(thresholdRgb, thresholdIr);
166 
167             initVideo();
168         }
169         catch (Exception ex)
170         {
171             LogUtil.LogInfo(GetType(), ex);
172             Debug.LogError("程序初始化異常,請在App.config中修改日志配置,根據日志查找原因!");
173             //MessageBox.Show("程序初始化異常,請在App.config中修改日志配置,根據日志查找原因!");
174 
175             Quit();
176             //System.Environment.Exit(0);
177         }
178     }

五、DEMO源碼下載

鏈接:https://pan.baidu.com/s/1FXs94jbAEseoERpVDzysFA
提取碼:iabc
復制這段內容后打開百度網盤手機App,操作更方便哦

 

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

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM