/// <summary> /// 初始化 /// </summary> /// <param name="appId">虹软SDK的AppId</param> /// <param name="dKey">虹软SDK人脸检测的Key</param> /// <param name="rKey">虹软SDK人脸比对的Key</param> /// <param name="orientPriority">脸部角度,毋宁说是鼻子方向,上下为0或180度,左右为90或270度</param> /// <param name="scale">最小人脸尺寸有效值范围[2,50] 推荐值 16。该尺寸是人脸相对于所在图片的长边的占比。例如,如果用户想检测到的最小人脸尺寸是图片长度的 1/8,那么这个 scale 就应该设置为8</param> /// <param name="maxFaceNumber">用户期望引擎最多能检测出的人脸数有效值范围[1,100]</param> /// <param name="faceDataPath">人脸数据文件夹</param> /// <param name="rateH">视频图片采集高度和显示高度的比值</param> /// <param name="rateW">视频图片采集宽度和显示宽度的比值</param> /// <param name="message"></param> /// <returns></returns> public static bool Init(out string message, string appId, string dKey, string rKey, EOrientPriority orientPriority = EOrientPriority.Ext0, int scale = 16, int maxFaceNumber = 10, string faceDataPath = "d:\\FeatureData") { if (scale < 2 || scale > 50) { message = "scale的值必须在2-50之间"; return(false); } if (maxFaceNumber < 1 || maxFaceNumber > 100) { message = "maxFaceNumber的值必须在1-100之间"; return(false); } _DBuffer = Marshal.AllocCoTaskMem(20 * 1024 * 1024); _RBuffer = Marshal.AllocCoTaskMem(40 * 1024 * 1024); var initResult = (ErrorCode)ArcWrapper.DInit(appId, dKey, _DBuffer, 20 * 1024 * 1024, out _DEnginer, (int)orientPriority, scale, maxFaceNumber); if (initResult != ErrorCode.Ok) { message = $"初始化人脸检测引擎失败,错误代码:{(int)initResult},错误描述:{ ((DescriptionAttribute)(initResult.GetType().GetCustomAttribute(typeof(DescriptionAttribute), false))).Description}"; return(false); } initResult = (ErrorCode)ArcWrapper.RInit(appId, rKey, _RBuffer, 40 * 1024 * 1024, out _REngine); if (initResult != ErrorCode.Ok) { message = $"初始化人脸比对引擎失败,错误代码:{(int)initResult},错误描述:{ ((DescriptionAttribute)(initResult.GetType().GetCustomAttribute(typeof(DescriptionAttribute), false))).Description}"; return(false); } FaceResults = new FaceResults(maxFaceNumber); _FaceDataPath = faceDataPath; if (!Directory.Exists(faceDataPath)) { Directory.CreateDirectory(faceDataPath); } else { foreach (var file in Directory.GetFiles(faceDataPath)) { var info = new FileInfo(file); var data = File.ReadAllBytes(file); var faceModel = new FaceModel { lFeatureSize = data.Length, pbFeature = Marshal.AllocCoTaskMem(data.Length) }; Marshal.Copy(data, 0, faceModel.pbFeature, data.Length); _FaceLib.Items.Add(new FaceLib.Item() { OrderId = 0, ID = info.Name.Replace(info.Extension, ""), FaceModel = faceModel }); } } _MaxFaceNumber = maxFaceNumber; message = "初始化成功"; return(true); }
/// <summary> /// 初始化,主要用于视频,取消人脸方向参数 /// </summary> /// <param name="appId">虹软SDK的AppId</param> /// <param name="dKey">虹软SDK人脸检测的Key</param> /// <param name="rKey">虹软SDK人脸比对的Key</param> /// <param name="orientPriority">脸部角度,毋宁说是鼻子方向,上下为0或180度,左右为90或270度</param> /// <param name="scale">最小人脸尺寸有效值范围[2,50] 推荐值 16。该尺寸是人脸相对于所在图片的长边的占比。例如,如果用户想检测到的最小人脸尺寸是图片长度的 1/8,那么这个 scale 就应该设置为8</param> /// <param name="maxFaceNumber">用户期望引擎最多能检测出的人脸数有效值范围[1,100]</param> /// <param name="faceDataPath">人脸数据文件夹</param> /// <param name="message"></param> /// <returns></returns> public static bool Init(out string message, string appId, string dKey, string rKey, int scale = 50, int maxFaceNumber = 10, string faceDataPath = "d:\\FeatureData") { if (scale < 2 || scale > 50) { message = "scale的值必须在2-50之间"; return(false); } if (maxFaceNumber < 1 || maxFaceNumber > 100) { message = "人脸数量必须在1-100之间"; return(false); } _DBuffer = Marshal.AllocCoTaskMem(20 * 1024 * 1024); var initResult = (ErrorCode)ArcWrapper.DInit(appId, dKey, _DBuffer, 20 * 1024 * 1024, out _DEnginer, (int)ArcFace.EOrientPriority.Only0, scale, maxFaceNumber); if (initResult != ErrorCode.Ok) { message = $"初始化人脸检测引擎失败,错误代码:{(int)initResult},错误描述:{ initResult.GetType().GetMember(initResult.ToString()).FirstOrDefault()?.GetCustomAttribute<DescriptionAttribute>().Description ?? initResult.ToString()}"; return(false); } for (int i = 0; i < TaskNum; i++) { _RBuffer[i] = Marshal.AllocCoTaskMem(40 * 1024 * 1024); initResult = (ErrorCode)ArcWrapper.RInit(appId, rKey, _RBuffer[i], 40 * 1024 * 1024, out _REngine[i]); if (initResult != ErrorCode.Ok) { message = $"初始化人脸比对引擎失败,错误代码:{(int)initResult},错误描述:{ initResult.GetType().GetMember(initResult.ToString()).FirstOrDefault()?.GetCustomAttribute<DescriptionAttribute>().Description ?? initResult.ToString()}"; return(false); } _FaceLib[i] = new FaceLib(); } CacheFaceResults = new FaceResults(maxFaceNumber); _MaxFaceNumber = maxFaceNumber; _FaceDataPath = faceDataPath; _FaceImagePath = Path.Combine(_FaceDataPath, "Image"); if (!Directory.Exists(faceDataPath)) { Directory.CreateDirectory(faceDataPath); } if (!Directory.Exists(_FaceImagePath)) { Directory.CreateDirectory(_FaceImagePath); } int index = 0; for (int i = 0; i < 100; i++) { foreach (var file in Directory.GetFiles(faceDataPath)) { var info = new FileInfo(file); var data = File.ReadAllBytes(file); var pFeature = Marshal.AllocCoTaskMem(data.Length); Marshal.Copy(data, 0, pFeature, data.Length); _FaceLib[index % TaskNum].Items.Add(new FaceLib.Item() { OrderId = 0, ID = info.Name.Replace(info.Extension, ""), FaceModel = new FaceModel { Size = FeatureSize, PFeature = pFeature } }); index++; } } message = "初始化成功"; return(true); }