unity接入实现人脸识别应用-基于虹软人脸识别算法4.0    

一、准备工作

1、下载虹软人脸识别增值版SDK 4.0

1)注册并登录开发者中心

访问https://www.arcsoft.com.cn/登录后创建新应用并添加SDK

unity接入实现人脸识别应用-基于虹软人脸识别算法4.0_unity

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开发环境!

unity接入实现人脸识别应用-基于虹软人脸识别算法4.0_人脸识别_02

二、创建DEMO工程

1、创建Unity工程

Unity Hub-》项目-》新建-》创建即可完成unity工程的创建

unity接入实现人脸识别应用-基于虹软人脸识别算法4.0_人脸识别_03

2、引入虹软人脸识别SDK

下载完成虹软人脸识别SDK后解压-》lib-》X64即可看到libarcsoft_face.dll和libarcsoft_face_engine.dll两个动态库文件

unity接入实现人脸识别应用-基于虹软人脸识别算法4.0_unity_04

注意:在unity工程的下图中红色箭头标注,32位和64位的Arcface SDK需要分别勾选x86和x64,libarcsoft_face.dll和libarcsoft_face_engine.dll两个文件都需要此操作!

unity接入实现人脸识别应用-基于虹软人脸识别算法4.0_人脸识别_05

 

 

3、项目工程目录说明

下图中1、2部分取自官网的VS窗体Demo,3部分取自虹软人脸识别SDK,4部分是在VS窗体Demo的基础上进行修改的类。

unity接入实现人脸识别应用-基于虹软人脸识别算法4.0_unity_06

三、运行调试程序

1、虹软人脸识别SDK在线激活

unity接入实现人脸识别应用-基于虹软人脸识别算法4.0_人脸识别_07

2、可以进行人脸识别

unity接入实现人脸识别应用-基于虹软人脸识别算法4.0_人脸识别_08

四、核心代码说明

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

unity接入实现人脸识别应用-基于虹软人脸识别算法4.0_unity_09

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 console10         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 SELECT23     public void GoWebCam01()24     {25         WebCamDevice[] cam_devices = WebCamTexture.devices;26         // for debugging purposes, prints available devices to the console27         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                 else104                 {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=dbad527e123             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中读取特征值进行人脸数据的比对。