经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
unity接入实现人脸识别应用-基于虹软人脸识别算法4.0
来源:cnblogs  作者:lizidun  时间:2021/4/6 10:21:51  对本文有异议

一、准备工作

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

2)图片格式转换 Texture2D to Image

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

 3)初始化人脸识别库

  1. 注意:此代码片段中appId sdkKey64 sdkKey32 activeKey64 activeKey32的值需要根据虹软开发者中心的实际数值进行填写
  1. 1 private void InitEngines()
  2. 2 {
  3. 3 try
  4. 4 {
  5. 5 webCamDisplayText.text += "测试";
  6. 6
  7. 7 //读取配置文件
  8. 8 //AppSettingsReader reader = new AppSettingsReader();
  9. 9 //rgbCameraIndex = (int)reader.GetValue("RGB_CAMERA_INDEX", typeof(int));
  10. 10 //irCameraIndex = (int)reader.GetValue("IR_CAMERA_INDEX", typeof(int));
  11. 11 //frMatchTime = (int)reader.GetValue("FR_MATCH_TIME", typeof(int));
  12. 12 //liveMatchTime = (int)reader.GetValue("LIVENESS_MATCH_TIME", typeof(int));
  13. 13
  14. 14 AppSettingsReader reader = new AppSettingsReader();
  15. 15 rgbCameraIndex = 0;
  16. 16 irCameraIndex = 1;
  17. 17 frMatchTime = 20;
  18. 18 liveMatchTime = 20;
  19. 19
  20. 20 int retCode = 0;
  21. 21 bool isOnlineActive = true;//true(在线激活) or false(离线激活)
  22. 22 try
  23. 23 {
  24. 24 if (isOnlineActive)
  25. 25 {
  26. 26 #region 读取在线激活配置信息
  27. 27 //string appId = (string)reader.GetValue("APPID", typeof(string));
  28. 28 //string sdkKey64 = (string)reader.GetValue("SDKKEY64", typeof(string));
  29. 29 //string sdkKey32 = (string)reader.GetValue("SDKKEY32", typeof(string));
  30. 30 //string activeKey64 = (string)reader.GetValue("ACTIVEKEY64", typeof(string));
  31. 31 //string activeKey32 = (string)reader.GetValue("ACTIVEKEY32", typeof(string));
  32. 32
  33. 33 string appId = "";
  34. 34 string sdkKey64 = "";
  35. 35 string sdkKey32 = "";
  36. 36 string activeKey64 = "";
  37. 37 string activeKey32 = "";
  38. 38
  39. 39 webCamDisplayText.text += "111111";
  40. 40
  41. 41 //判断CPU位数
  42. 42 var is64CPU = Environment.Is64BitProcess;
  43. 43 if (string.IsNullOrWhiteSpace(appId) || string.IsNullOrWhiteSpace(is64CPU ? sdkKey64 : sdkKey32) || string.IsNullOrWhiteSpace(is64CPU ? activeKey64 : activeKey32))
  44. 44 {
  45. 45 Debug.LogError(string.Format("请在App.config配置文件中先配置APP_ID和SDKKEY{0}、ACTIVEKEY{0}!", is64CPU ? "64" : "32"));
  46. 46 //MessageBox.Show(string.Format("请在App.config配置文件中先配置APP_ID和SDKKEY{0}、ACTIVEKEY{0}!", is64CPU ? "64" : "32"));
  47. 47
  48. 48 //System.Environment.Exit(0);
  49. 49 Quit();
  50. 50 }
  51. 51 #endregion
  52. 52
  53. 53 webCamDisplayText.text += "准备激活";
  54. 54
  55. 55 //在线激活引擎 如出现错误,1.请先确认从官网下载的sdk库已放到对应的bin中,2.当前选择的CPU为x86或者x64
  56. 56 retCode = imageEngine.ASFOnlineActivation(appId, is64CPU ? sdkKey64 : sdkKey32, is64CPU ? activeKey64 : activeKey32);
  57. 57
  58. 58 webCamDisplayText.text += "激活完成";
  59. 59 }
  60. 60 else
  61. 61 {
  62. 62 #region 读取离线激活配置信息
  63. 63 string offlineActiveFilePath = (string)reader.GetValue("OfflineActiveFilePath", typeof(string));
  64. 64 if (string.IsNullOrWhiteSpace(offlineActiveFilePath) || !File.Exists(offlineActiveFilePath))
  65. 65 {
  66. 66 string deviceInfo;
  67. 67 retCode = imageEngine.ASFGetActiveDeviceInfo(out deviceInfo);
  68. 68 if (retCode != 0)
  69. 69 {
  70. 70 Debug.LogError("获取设备信息失败,错误码:" + retCode);
  71. 71 //MessageBox.Show("获取设备信息失败,错误码:" + retCode);
  72. 72 }
  73. 73 else
  74. 74 {
  75. 75 File.WriteAllText("ActiveDeviceInfo.txt", deviceInfo);
  76. 76 Debug.LogError("获取设备信息成功,已保存到运行根目录ActiveDeviceInfo.txt文件,请在官网执行离线激活操作,将生成的离线授权文件路径在App.config里配置后再重新运行");
  77. 77 //MessageBox.Show("获取设备信息成功,已保存到运行根目录ActiveDeviceInfo.txt文件,请在官网执行离线激活操作,将生成的离线授权文件路径在App.config里配置后再重新运行");
  78. 78 }
  79. 79 //System.Environment.Exit(0);
  80. 80 Quit();
  81. 81 }
  82. 82 #endregion
  83. 83 //离线激活
  84. 84 retCode = imageEngine.ASFOfflineActivation(offlineActiveFilePath);
  85. 85 }
  86. 86 if (retCode != 0 && retCode != 90114)
  87. 87 {
  88. 88 Debug.LogError("激活SDK失败,错误码:" + retCode);
  89. 89 //MessageBox.Show("激活SDK失败,错误码:" + retCode);
  90. 90 //System.Environment.Exit(0);
  91. 91 Quit();
  92. 92 }
  93. 93
  94. 94 webCamDisplayText.text += retCode.ToString();
  95. 95 }
  96. 96 catch (Exception ex)
  97. 97 {
  98. 98 if (ex.Message.Contains("无法加载 DLL"))
  99. 99 {
  100. 100 Debug.LogError("请将SDK相关DLL放入bin对应的x86或x64下的文件夹中!");
  101. 101 //MessageBox.Show("请将SDK相关DLL放入bin对应的x86或x64下的文件夹中!");
  102. 102 }
  103. 103 else
  104. 104 {
  105. 105 Debug.LogError("激活SDK失败,请先检查依赖环境及SDK的平台、版本是否正确!");
  106. 106 //MessageBox.Show("激活SDK失败,请先检查依赖环境及SDK的平台、版本是否正确!");
  107. 107 }
  108. 108 //System.Environment.Exit(0);
  109. 109 Quit();
  110. 110 }
  111. 111
  112. 112 //初始化引擎
  113. 113 DetectionMode detectMode = DetectionMode.ASF_DETECT_MODE_IMAGE;
  114. 114 //Video模式下检测脸部的角度优先值
  115. 115 ASF_OrientPriority videoDetectFaceOrientPriority = ASF_OrientPriority.ASF_OP_ALL_OUT;
  116. 116 //Image模式下检测脸部的角度优先值
  117. 117 ASF_OrientPriority imageDetectFaceOrientPriority = ASF_OrientPriority.ASF_OP_ALL_OUT;
  118. 118 //最大需要检测的人脸个数
  119. 119 int detectFaceMaxNum = 6;
  120. 120 //引擎初始化时需要初始化的检测功能组合
  121. 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. 122 //初始化引擎,正常值为0,其他返回值请参考http://ai.arcsoft.com.cn/bbs/forum.php?mod=viewthread&tid=19&_dsign=dbad527e
  123. 123 retCode = imageEngine.ASFInitEngine(detectMode, imageDetectFaceOrientPriority, detectFaceMaxNum, combinedMask);
  124. 124 Console.WriteLine("InitEngine Result:" + retCode);
  125. 125 AppendText((retCode == 0) ? "图片引擎初始化成功!" : string.Format("图片引擎初始化失败!错误码为:{0}", retCode));
  126. 126 if (retCode != 0)
  127. 127 {
  128. 128 //禁用相关功能按钮
  129. 129 //ControlsEnable(false, chooseMultiImgBtn, matchBtn, btnClearFaceList, chooseImgBtn);
  130. 130 }
  131. 131
  132. 132 //初始化视频模式下人脸检测引擎
  133. 133 DetectionMode detectModeVideo = DetectionMode.ASF_DETECT_MODE_VIDEO;
  134. 134 int combinedMaskVideo = FaceEngineMask.ASF_FACE_DETECT | FaceEngineMask.ASF_FACERECOGNITION | FaceEngineMask.ASF_FACELANDMARK;
  135. 135 retCode = videoEngine.ASFInitEngine(detectModeVideo, videoDetectFaceOrientPriority, detectFaceMaxNum, combinedMaskVideo);
  136. 136 AppendText((retCode == 0) ? "视频引擎初始化成功!" : string.Format("视频引擎初始化失败!错误码为:{0}", retCode));
  137. 137 if (retCode != 0)
  138. 138 {
  139. 139 //禁用相关功能按钮
  140. 140 //ControlsEnable(false, chooseMultiImgBtn, matchBtn, btnClearFaceList, chooseImgBtn);
  141. 141 }
  142. 142
  143. 143 //RGB视频专用FR引擎
  144. 144 combinedMask = FaceEngineMask.ASF_FACE_DETECT | FaceEngineMask.ASF_FACERECOGNITION | FaceEngineMask.ASF_LIVENESS | FaceEngineMask.ASF_MASKDETECT;
  145. 145 retCode = videoRGBImageEngine.ASFInitEngine(detectMode, videoDetectFaceOrientPriority, detectFaceMaxNum, combinedMask);
  146. 146 AppendText((retCode == 0) ? "RGB处理引擎初始化成功!" : string.Format("RGB处理引擎初始化失败!错误码为:{0}", retCode));
  147. 147 if (retCode != 0)
  148. 148 {
  149. 149 //禁用相关功能按钮
  150. 150 //ControlsEnable(false, chooseMultiImgBtn, matchBtn, btnClearFaceList, chooseImgBtn);
  151. 151 }
  152. 152 //设置活体阈值
  153. 153 videoRGBImageEngine.ASFSetLivenessParam(thresholdRgb);
  154. 154
  155. 155 //IR视频专用FR引擎
  156. 156 combinedMask = FaceEngineMask.ASF_FACE_DETECT | FaceEngineMask.ASF_FACERECOGNITION | FaceEngineMask.ASF_IR_LIVENESS;
  157. 157 retCode = videoIRImageEngine.ASFInitEngine(detectModeVideo, videoDetectFaceOrientPriority, detectFaceMaxNum, combinedMask);
  158. 158 AppendText((retCode == 0) ? "IR处理引擎初始化成功!\r\n" : string.Format("IR处理引擎初始化失败!错误码为:{0}\r\n", retCode));
  159. 159 if (retCode != 0)
  160. 160 {
  161. 161 //禁用相关功能按钮
  162. 162 //ControlsEnable(false, chooseMultiImgBtn, matchBtn, btnClearFaceList, chooseImgBtn);
  163. 163 }
  164. 164 //设置活体阈值
  165. 165 videoIRImageEngine.ASFSetLivenessParam(thresholdRgb, thresholdIr);
  166. 166
  167. 167 initVideo();
  168. 168 }
  169. 169 catch (Exception ex)
  170. 170 {
  171. 171 LogUtil.LogInfo(GetType(), ex);
  172. 172 Debug.LogError("程序初始化异常,请在App.config中修改日志配置,根据日志查找原因!");
  173. 173 //MessageBox.Show("程序初始化异常,请在App.config中修改日志配置,根据日志查找原因!");
  174. 174
  175. 175 Quit();
  176. 176 //System.Environment.Exit(0);
  177. 177 }
  178. 178 }

五、DEMO源码下载

链接:https://pan.baidu.com/s/1FXs94jbAEseoERpVDzysFA
提取码:iabc
复制这段内容后打开百度网盘手机App,操作更方便哦

 

注意:DEMO工程只是实现了摄像头初始化、采集图片格式转换、虹软人脸在线注册和人脸特征值实时提取等功能,在使用图片进行人脸注册功能时会报错,未解决!

提供思路:可以将人脸注册的功能单独提取并将特征值存入数据库,然后从unity中读取特征值进行人脸数据的比对。

原文链接:http://www.cnblogs.com/haoqingwanzhang/p/14481553.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号