1, Preparatory work
1. Download SDK 4.0 of hongruan face recognition value-added version
1) Register and log in to the developer center
2) Download hongsoft face recognition SDK
2. Install Unity3D and Visual Studio 2019 development environment
1) Install Unity Hub
2) Install Unity 2020.3.1f1c1
2, Create DEMO project
1. Create Unity project
2. Introduce hongsoft face recognition SDK
3. Description of project catalogue
3, Run debugging program
1. Hongruan face recognition SDK online activation
2. Face recognition is possible
4, Core code description
1. About system Drawing. DLL missing problem description
2. About using selection in Unity NET Version Description
3. Face recognition core code analysis
5, Download DEMO source code————————————————
1, Preparatory work
1. Download SDK 4.0 of hongruan face recognition value-added version
1) Register and log in to the developer center
visit https://www.arcsoft.com.cn/ Create a new application and add SDK after login
2) Download hongsoft face recognition SDK
Click V4 in the above figure Downloadable version of SDK 0
2. Install Unity3D and Visual Studio 2019 development environment
1) Install Unity Hub
visit https://unity.cn/ Download and install Unity Hub
2) Install Unity 2020.3.1f1c1
Open Unity Hub after installation, and install Unity 2020.3.1f1c1 according to the following figure. Note: check Visual Studio to download VS development environment!
2, Create DEMO project
1. Create Unity project
Unity Hub - Project - New - create to complete the creation of the unity project
2. Introduce hongsoft face recognition SDK
After downloading the hongsoft face recognition SDK, unzip "lib -" X64 to see libarcsoft_face.dll and libarcsoft_face_engine.dll two dynamic library files Note: in the following figure of the unity project, the red arrow indicates that x86, x64 and libarcsoft should be checked for 32-bit and 64 bit Arcface SDK respectively_ face. DLL and libarcsoft_face_engine.dll both files need this operation!
3. Description of project catalogue
In the figure below, parts 1 and 2 are taken from the VS form Demo on the official website, Part 3 is taken from the hongruan face recognition SDK, and Part 4 is a class modified on the basis of the VS form Demo.
3, Run debugging program
1. Hongruan face recognition SDK online activation
2. Face recognition is possible
4, Core code description
1. About system Drawing. DLL missing problem description
Note: if only libarcsoft is introduced into the code_ face. DLL and libarcsoft_face_engine.dll will report an error Drawing. DLL file missing
You can find the file in the installation directory ~ \ 2020.3.1f1c1\Editor\Data\MonoBleedingEdge\lib\mono.7.1-api \ of unity and copy it into the project
2. About using selection in Unity NET Version Description
Select Scripting Backend bit Mono in player setting of unit project, and Api Compatibility Level * is NET 4.x
3. Face recognition core code analysis
1) Camera initialization 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) Image format conversion 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) Initialize the face recognition database. Note: the value of appId sdkKey64 sdkKey32 activeKey64 activeKey32 in this code segment needs to be filled in according to the actual value of hongruan Developer Center
private void InitEngines() { try { webCamDisplayText.text += "test"; //Read configuration file //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 (online activation) or false (offline activation) try { if (isOnlineActive) { #region read online activation configuration information //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"; //Judge CPU bits var is64CPU = Environment.Is64BitProcess; if (string.IsNullOrWhiteSpace(appId) || string.IsNullOrWhiteSpace(is64CPU ? sdkKey64 : sdkKey32) || string.IsNullOrWhiteSpace(is64CPU ? activeKey64 : activeKey32)) { Debug.LogError(string.Format("Please in App.config Configure first in the configuration file APP_ID and SDKKEY{0},ACTIVEKEY{0}!", is64CPU ? "64" : "32")); //MessageBox.Show(string.Format("please configure APP_ID and SDKKEY{0} and activekey {0} in App.config configuration file first!", is64CPU ? "64" : "32")); //System.Environment.Exit(0); Quit(); } #endregion webCamDisplayText.text += "Ready to activate"; //If an error occurs in the online activation engine, 1 Please confirm that the sdk library downloaded from the official website has been placed in the corresponding bin, 2 The currently selected CPU is x86 or x64 retCode = imageEngine.ASFOnlineActivation(appId, is64CPU ? sdkKey64 : sdkKey32, is64CPU ? activeKey64 : activeKey32); webCamDisplayText.text += "Activation complete"; } else { #region read offline activation configuration information 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("Failed to get device information, error code:" + retCode); //MessageBox.Show("failed to obtain device information, error code:" + retCode); } else { File.WriteAllText("ActiveDeviceInfo.txt", deviceInfo); Debug.LogError("The device information is obtained successfully and has been saved to the root directory of the operation ActiveDeviceInfo.txt File, please execute the offline activation operation on the official website, and the generated offline authorization file path is App.config Re run after configuration"); //MessageBox.Show("the device information is obtained successfully and has been saved to the ActiveDeviceInfo.txt file in the running root directory. Please perform offline activation on the official website and configure the path of the generated offline authorization file in App.config before running again"); } //System.Environment.Exit(0); Quit(); } #endregion //Offline Activation retCode = imageEngine.ASFOfflineActivation(offlineActiveFilePath); } if (retCode != 0 && retCode != 90114) { Debug.LogError("activation SDK fail,Error code:" + retCode); //MessageBox.Show("failed to activate SDK, error code:" + retcode "); //System.Environment.Exit(0); Quit(); } webCamDisplayText.text += retCode.ToString(); } catch (Exception ex) { if (ex.Message.Contains("Unable to load DLL")) { Debug.LogError("Will please SDK relevant DLL Put bin Corresponding x86 or x64 Folder under!"); //MessageBox.Show("please put the SDK related DLL s into the folder under x86 or x64 corresponding to bin!"); } else { Debug.LogError("activation SDK fail,Please check the environment first SDK Is the platform and version correct!"); //MessageBox.Show("failed to activate SDK, please check whether the dependent environment and SDK platform and version are correct!"); } //System.Environment.Exit(0); Quit(); } //Initialize engine DetectionMode detectMode = DetectionMode.ASF_DETECT_MODE_IMAGE; //Detecting the angle priority value of the face in Video mode ASF_OrientPriority videoDetectFaceOrientPriority = ASF_OrientPriority.ASF_OP_ALL_OUT; //Priority value of face angle detection in Image mode ASF_OrientPriority imageDetectFaceOrientPriority = ASF_OrientPriority.ASF_OP_ALL_OUT; //Maximum number of faces to detect int detectFaceMaxNum = 6; //Detection function combination that needs to be initialized during engine initialization int combinedMask = FaceEngineMask.ASF_FACE_DETECT | FaceEngineMask.ASF_FACERECOGNITION | FaceEngineMask.ASF_AGE | FaceEngineMask.ASF_GENDER | FaceEngineMask.ASF_FACE3DANGLE | FaceEngineMask.ASF_IMAGEQUALITY | FaceEngineMask.ASF_MASKDETECT; //Initialize the engine. The normal value is 0. Please refer to other return values 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) ? "Picture engine initialized successfully!" : string.Format("Picture engine initialization failed!The error code is:{0}", retCode)); if (retCode != 0) { //Disable related function buttons //ControlsEnable(false, chooseMultiImgBtn, matchBtn, btnClearFaceList, chooseImgBtn); } //Initialize the face detection engine in video mode 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) ? "Video engine initialized successfully!" : string.Format("Video engine initialization failed!The error code is:{0}", retCode)); if (retCode != 0) { //Disable related function buttons //ControlsEnable(false, chooseMultiImgBtn, matchBtn, btnClearFaceList, chooseImgBtn); } //Special FR engine for RGB video 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 Processing engine initialized successfully!" : string.Format("RGB Processing engine initialization failed!The error code is:{0}", retCode)); if (retCode != 0) { //Disable related function buttons //ControlsEnable(false, chooseMultiImgBtn, matchBtn, btnClearFaceList, chooseImgBtn); } //Set living threshold videoRGBImageEngine.ASFSetLivenessParam(thresholdRgb); //Dedicated FR engine for IR Video combinedMask = FaceEngineMask.ASF_FACE_DETECT | FaceEngineMask.ASF_FACERECOGNITION | FaceEngineMask.ASF_IR_LIVENESS; retCode = videoIRImageEngine.ASFInitEngine(detectModeVideo, videoDetectFaceOrientPriority, detectFaceMaxNum, combinedMask); AppendText((retCode == 0) ? "IR Processing engine initialized successfully!\r\n" : string.Format("IR Processing engine initialization failed!The error code is:{0}\r\n", retCode)); if (retCode != 0) { //Disable related function buttons //ControlsEnable(false, chooseMultiImgBtn, matchBtn, btnClearFaceList, chooseImgBtn); } //Set living threshold videoIRImageEngine.ASFSetLivenessParam(thresholdRgb, thresholdIr); initVideo(); } catch (Exception ex) { LogUtil.LogInfo(GetType(), ex); Debug.LogError("Program initialization exception,Please in App.config Modify log configuration in,Find the reason according to the log!"); //MessageBox.Show("the program initialization is abnormal, please modify the log configuration in App.config and find the reason according to the log!"); Quit(); //System.Environment.Exit(0); } }
5, DEMO source code download link: https://pan.baidu.com/s/1FXs94jbAEseoERpVDzysFA Extraction code: after iabc copies this content, open Baidu online disk mobile App, which is more convenient to operate
Note: DEMO project only realizes the functions of camera initialization, image format conversion, hongsoft face online registration and real-time extraction of face eigenvalues. An error will be reported when using the image for face registration, which has not been solved!
Provide ideas: the function of face registration can be extracted separately and the eigenvalues can be stored in the database, and then the eigenvalues can be read from unity for face data comparison.
For more information about face recognition products, please visit Hongruan vision open platform oh