/// <summary> /// 性别检测 /// </summary> /// <param name="pEngine">引擎Handle</param> /// <param name="imageInfo">图像数据</param> /// <param name="multiFaceInfo">人脸检测结果</param> /// <returns>保存性别检测结果结构体</returns> public static ASF_GenderInfo DetectGender(IntPtr pEngine, ImageInfo imageInfo, ASF_MultiFaceInfo multiFaceInfo, out int retCode) { retCode = -1; IntPtr pMultiFaceInfo = UtilToolMemory.Malloc(UtilToolMemory.SizeOf <ASF_MultiFaceInfo>()); UtilToolMemory.StructureToPtr(multiFaceInfo, pMultiFaceInfo); if (multiFaceInfo.faceNum == 0) { return(new ASF_GenderInfo()); } //人脸信息处理 retCode = ASFFunctions.ASFProcess(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pMultiFaceInfo, FaceEngineMask.ASF_GENDER); if (retCode == 0) { //获取性别信息 IntPtr pGenderInfo = UtilToolMemory.Malloc(UtilToolMemory.SizeOf <ASF_GenderInfo>()); retCode = ASFFunctions.ASFGetGender(pEngine, pGenderInfo); Console.WriteLine("Get Gender Result:" + retCode); ASF_GenderInfo genderInfo = UtilToolMemory.PtrToStructure <ASF_GenderInfo>(pGenderInfo); //释放内存 UtilToolMemory.Free(pMultiFaceInfo); UtilToolMemory.Free(pGenderInfo); return(genderInfo); } else { return(new ASF_GenderInfo()); } }
/// <summary> /// 人脸3D角度检测 /// </summary> /// <param name="pEngine">引擎Handle</param> /// <param name="imageInfo">图像数据</param> /// <param name="multiFaceInfo">人脸检测结果</param> /// <returns>保存人脸3D角度检测结果结构体</returns> public static ASF_Face3DAngle DetectFace3DAngle(IntPtr pEngine, ImageInfo imageInfo, ASF_MultiFaceInfo multiFaceInfo, out int retCode) { IntPtr pMultiFaceInfo = UtilToolMemory.Malloc(UtilToolMemory.SizeOf <ASF_MultiFaceInfo>()); UtilToolMemory.StructureToPtr(multiFaceInfo, pMultiFaceInfo); if (multiFaceInfo.faceNum == 0) { retCode = -1; return(new ASF_Face3DAngle()); } //人脸信息处理 retCode = ASFFunctions.ASFProcess(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pMultiFaceInfo, FaceEngineMask.ASF_FACE3DANGLE); if (retCode == 0) { //获取人脸3D角度 IntPtr pFace3DAngleInfo = UtilToolMemory.Malloc(UtilToolMemory.SizeOf <ASF_Face3DAngle>()); retCode = ASFFunctions.ASFGetFace3DAngle(pEngine, pFace3DAngleInfo); Console.WriteLine("Get Face3D Angle Result:" + retCode); ASF_Face3DAngle face3DAngle = UtilToolMemory.PtrToStructure <ASF_Face3DAngle>(pFace3DAngleInfo); //释放内存 UtilToolMemory.Free(pMultiFaceInfo); UtilToolMemory.Free(pFace3DAngleInfo); return(face3DAngle); } else { return(new ASF_Face3DAngle()); } }
/// <summary> /// 年龄检测 /// </summary> /// <param name="pEngine">引擎Handle</param> /// <param name="imageInfo">图像数据</param> /// <param name="multiFaceInfo">人脸检测结果</param> /// <returns>年龄检测结构体</returns> public static ASF_AgeInfo AgeEstimation(IntPtr pEngine, ImageInfo imageInfo, ASF_MultiFaceInfo multiFaceInfo, out int retCode) { retCode = -1; IntPtr pMultiFaceInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_MultiFaceInfo>()); MemoryUtil.StructureToPtr(multiFaceInfo, pMultiFaceInfo); if (multiFaceInfo.faceNum == 0) { return(new ASF_AgeInfo()); } //人脸信息处理 retCode = ASFFunctions.ASFProcess(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pMultiFaceInfo, FaceEngineMask.ASF_AGE); if (retCode == 0) { //获取年龄信息 IntPtr pAgeInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_AgeInfo>()); retCode = ASFFunctions.ASFGetAge(pEngine, pAgeInfo); Console.WriteLine("Get Age Result:" + retCode); ASF_AgeInfo ageInfo = MemoryUtil.PtrToStructure <ASF_AgeInfo>(pAgeInfo); //释放内存 MemoryUtil.Free(pMultiFaceInfo); MemoryUtil.Free(pAgeInfo); return(ageInfo); } else { return(new ASF_AgeInfo()); } }
/// <summary> /// 年龄检测 /// </summary> /// <param name="pEngine">引擎Handle</param> /// <param name="imageInfo">图像数据</param> /// <param name="multiFaceInfo">人脸检测结果</param> /// <returns>年龄检测结构体</returns> public static ASF_AgeInfo DetectAge(IntPtr pEngine, ImageInfo imageInfo, ASF_MultiFaceInfo multiFaceInfo) { IntPtr pMultiFaceInfo = UtilToolMemory.Malloc(UtilToolMemory.SizeOf <ASF_MultiFaceInfo>()); UtilToolMemory.StructureToPtr(multiFaceInfo, pMultiFaceInfo); if (multiFaceInfo.faceNum == 0) { return(new ASF_AgeInfo()); } //人脸信息处理 int retCode = ASFFunctions.ASFProcess(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pMultiFaceInfo, FaceEngineMask.ASF_AGE); //获取年龄信息 IntPtr pAgeInfo = UtilToolMemory.Malloc(UtilToolMemory.SizeOf <ASF_AgeInfo>()); retCode = ASFFunctions.ASFGetAge(pEngine, pAgeInfo); Console.WriteLine("Get Age Result:" + retCode); ASF_AgeInfo ageInfo = UtilToolMemory.PtrToStructure <ASF_AgeInfo>(pAgeInfo); //释放内存 UtilToolMemory.Free(pMultiFaceInfo); UtilToolMemory.Free(pAgeInfo); return(ageInfo); }
/// <summary> /// 性别检测 /// </summary> /// <param name="pEngine">引擎Handle</param> /// <param name="imageInfo">图像数据</param> /// <param name="multiFaceInfo">人脸检测结果</param> /// <returns>保存性别估计结果结构体</returns> public static ASF_GenderInfo GenderEstimation(IntPtr pEngine, ImageInfo imageInfo, ASF_MultiFaceInfo multiFaceInfo) { IntPtr pMultiFaceInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_MultiFaceInfo>()); MemoryUtil.StructureToPtr(multiFaceInfo, pMultiFaceInfo); if (multiFaceInfo.faceNum == 0) { return(new ASF_GenderInfo()); } //人脸信息处理 int retCode = ASFFunctions.ASFProcess(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pMultiFaceInfo, FaceEngineMask.ASF_GENDER); //获取性别信息 IntPtr pGenderInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_GenderInfo>()); retCode = ASFFunctions.ASFGetGender(pEngine, pGenderInfo); Console.WriteLine("Get Gender Result:" + retCode); ASF_GenderInfo genderInfo = MemoryUtil.PtrToStructure <ASF_GenderInfo>(pGenderInfo); //释放内存 MemoryUtil.Free(pMultiFaceInfo); MemoryUtil.Free(pGenderInfo); return(genderInfo); }
/// <summary> /// Age detection /// </summary> /// <param name="pEngine">Engine Handle</param> /// <param name="imageInfo">Image data</param> /// <param name="multiFaceInfo">Face detection results</param> /// <returns>Age detection structures</returns> public static ASF_AgeInfo AgeEstimation(IntPtr pEngine, ImageInfo imageInfo, ASF_MultiFaceInfo multiFaceInfo) { IntPtr pMultiFaceInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_MultiFaceInfo>()); MemoryUtil.StructureToPtr(multiFaceInfo, pMultiFaceInfo); if (multiFaceInfo.faceNum == 0) { return(new ASF_AgeInfo()); } //Face information processing int retCode = ASFFunctions.ASFProcess(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pMultiFaceInfo, FaceEngineMask.ASF_AGE); //Get age information IntPtr pAgeInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_AgeInfo>()); retCode = ASFFunctions.ASFGetAge(pEngine, pAgeInfo); Console.WriteLine("Get Age Result:" + retCode); ASF_AgeInfo ageInfo = MemoryUtil.PtrToStructure <ASF_AgeInfo>(pAgeInfo); //Release memory MemoryUtil.Free(pMultiFaceInfo); MemoryUtil.Free(pAgeInfo); return(ageInfo); }
/// <summary> /// Face 3D Angle detection /// </summary> /// <param name="pEngine">Engine Handle</param> /// <param name="imageInfo">Image data</param> /// <param name="multiFaceInfo">Face detection results</param> /// <returns>Save face 3D Angle detection results structure</returns> public static ASF_Face3DAngle Face3DAngleDetection(IntPtr pEngine, ImageInfo imageInfo, ASF_MultiFaceInfo multiFaceInfo, out int retCode) { IntPtr pMultiFaceInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_MultiFaceInfo>()); MemoryUtil.StructureToPtr(multiFaceInfo, pMultiFaceInfo); if (multiFaceInfo.faceNum == 0) { retCode = -1; return(new ASF_Face3DAngle()); } //Face information processing retCode = ASFFunctions.ASFProcess(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pMultiFaceInfo, FaceEngineMask.ASF_FACE3DANGLE); if (retCode == 0) { //Get the 3D Angle of the face IntPtr pFace3DAngleInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_Face3DAngle>()); retCode = ASFFunctions.ASFGetFace3DAngle(pEngine, pFace3DAngleInfo); Console.WriteLine("Get Face3D Angle Result:" + retCode); ASF_Face3DAngle face3DAngle = MemoryUtil.PtrToStructure <ASF_Face3DAngle>(pFace3DAngleInfo); //Release memory MemoryUtil.Free(pMultiFaceInfo); MemoryUtil.Free(pFace3DAngleInfo); return(face3DAngle); } else { return(new ASF_Face3DAngle()); } }
/// <summary> /// Gender tests /// </summary> /// <param name="pEngine">Engine Handle</param> /// <param name="imageInfo">Image data</param> /// <param name="multiFaceInfo">Face detection results</param> /// <returns>Save the gender detection result structure</returns> public static ASF_GenderInfo GenderEstimation(IntPtr pEngine, ImageInfo imageInfo, ASF_MultiFaceInfo multiFaceInfo, out int retCode) { retCode = -1; IntPtr pMultiFaceInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_MultiFaceInfo>()); MemoryUtil.StructureToPtr(multiFaceInfo, pMultiFaceInfo); if (multiFaceInfo.faceNum == 0) { return(new ASF_GenderInfo()); } //Face information processing retCode = ASFFunctions.ASFProcess(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pMultiFaceInfo, FaceEngineMask.ASF_GENDER); if (retCode == 0) { //Access to gender information IntPtr pGenderInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_GenderInfo>()); retCode = ASFFunctions.ASFGetGender(pEngine, pGenderInfo); Console.WriteLine("Get Gender Result:" + retCode); ASF_GenderInfo genderInfo = MemoryUtil.PtrToStructure <ASF_GenderInfo>(pGenderInfo); //Release memory MemoryUtil.Free(pMultiFaceInfo); MemoryUtil.Free(pGenderInfo); return(genderInfo); } else { return(new ASF_GenderInfo()); } }
/// <summary> /// 人脸信息检测(年龄/性别/人脸3D角度) /// </summary> /// <param name="imageFormat">图片格式</param> /// <param name="image">图片</param> /// <param name="multiFaceInfo">多人脸对象</param> /// <param name="combinedMask">检测属性</param> /// <returns>返回0表示正常;返回负数请根据ErrorCodeUtil类注释查看;其他值请在官网-帮助中心查询</returns> public int ASFProcess(Image image, MultiFaceInfo multiFaceInfo, int combinedMask, ASF_ImagePixelFormat imageFormat = ASF_ImagePixelFormat.ASVL_PAF_RGB24_B8G8R8) { int retCode = -1; if (multiFaceInfo == null) { return(ErrorCodeUtil.MULPTIFACEINFO_IS_NULL); } if (image == null) { return(ErrorCodeUtil.IMAGE_IS_NULL); } ImageInfo imageInfo = new ImageInfo(); imageInfo = ASF_ImagePixelFormat.ASVL_PAF_RGB24_B8G8R8.Equals(imageFormat) ? ImageUtil.ReadBMP(image) : ImageUtil.ReadBMP_IR(image); if (imageInfo == null) { return(ErrorCodeUtil.IMAGE_DATA_READ_FAIL); } //将多人脸对象的信息转化到结构体中 ASF_MultiFaceInfo multiFaceInfoStruct = new ASF_MultiFaceInfo(); IntPtr pMultiFaceInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_MultiFaceInfo>()); multiFaceInfoStruct.faceNum = multiFaceInfo.faceNum; if (multiFaceInfo.faceNum > 0) { if (multiFaceInfo.faceID != null) { multiFaceInfoStruct.faceID = MemoryUtil.Malloc(multiFaceInfo.faceNum * MemoryUtil.SizeOf <int>()); Marshal.Copy(multiFaceInfo.faceID, 0, multiFaceInfoStruct.faceID, multiFaceInfo.faceNum); } multiFaceInfoStruct.faceOrients = MemoryUtil.Malloc(multiFaceInfo.faceNum * MemoryUtil.SizeOf <int>()); Marshal.Copy(multiFaceInfo.faceOrients, 0, multiFaceInfoStruct.faceOrients, multiFaceInfo.faceNum); multiFaceInfoStruct.faceRects = MemoryUtil.Malloc(MemoryUtil.SizeOf <MRECT>() * multiFaceInfo.faceNum); byte[] allByte = new byte[MemoryUtil.SizeOf <MRECT>() * multiFaceInfo.faceNum]; for (int i = 0; i < multiFaceInfo.faceNum; i++) { byte[] tempBytes = MemoryUtil.StructToBytes(multiFaceInfo.faceRects[i]); tempBytes.CopyTo(allByte, MemoryUtil.SizeOf <MRECT>() * i); } Marshal.Copy(allByte, 0, multiFaceInfoStruct.faceRects, allByte.Length); } MemoryUtil.StructureToPtr(multiFaceInfoStruct, pMultiFaceInfo); //调用SDK接口 retCode = ASFFunctions.ASFProcess(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pMultiFaceInfo, combinedMask); //释放内存 MemoryUtil.FreeArray(imageInfo.imgData, multiFaceInfoStruct.faceID, multiFaceInfoStruct.faceOrients, multiFaceInfoStruct.faceRects, pMultiFaceInfo); return(retCode); }
/// <summary> /// RGB活体检测 /// </summary> /// <param name="pEngine">引擎Handle</param> /// <param name="imageInfo">图像数据</param> /// <param name="multiFaceInfo">活体检测结果</param> /// <returns>保存活体检测结果结构体</returns> public static ASF_LivenessInfo LivenessInfo_RGB(IntPtr pEngine, ImageInfo imageInfo, ASF_MultiFaceInfo multiFaceInfo, out int retCode) { IntPtr pMultiFaceInfo = UtilToolMemory.Malloc(UtilToolMemory.SizeOf <ASF_MultiFaceInfo>()); UtilToolMemory.StructureToPtr(multiFaceInfo, pMultiFaceInfo); if (multiFaceInfo.faceNum == 0) { retCode = -1; //释放内存 UtilToolMemory.Free(pMultiFaceInfo); return(new ASF_LivenessInfo()); } try { //人脸信息处理 retCode = ASFFunctions.ASFProcess(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pMultiFaceInfo, FaceEngineMask.ASF_LIVENESS); if (retCode == 0) { //获取活体检测结果 IntPtr pLivenessInfo = UtilToolMemory.Malloc(UtilToolMemory.SizeOf <ASF_LivenessInfo>()); retCode = ASFFunctions.ASFGetLivenessScore(pEngine, pLivenessInfo); Console.WriteLine("Get Liveness Result:" + retCode); ASF_LivenessInfo livenessInfo = UtilToolMemory.PtrToStructure <ASF_LivenessInfo>(pLivenessInfo); //释放内存 UtilToolMemory.Free(pMultiFaceInfo); UtilToolMemory.Free(pLivenessInfo); return(livenessInfo); } else { //释放内存 UtilToolMemory.Free(pMultiFaceInfo); return(new ASF_LivenessInfo()); } } catch { retCode = -1; //释放内存 UtilToolMemory.Free(pMultiFaceInfo); return(new ASF_LivenessInfo()); } }
/// <summary> /// RGB Vivo detection /// </summary> /// <param name="pEngine">Engine Handle</param> /// <param name="imageInfo">Image data</param> /// <param name="multiFaceInfo">In vivo test results</param> /// <returns>Preserve the structure of in vivo detection results</returns> public static ASF_LivenessInfo LivenessInfo_RGB(IntPtr pEngine, ImageInfo imageInfo, ASF_MultiFaceInfo multiFaceInfo, out int retCode) { IntPtr pMultiFaceInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_MultiFaceInfo>()); MemoryUtil.StructureToPtr(multiFaceInfo, pMultiFaceInfo); if (multiFaceInfo.faceNum == 0) { retCode = -1; //Release memory MemoryUtil.Free(pMultiFaceInfo); return(new ASF_LivenessInfo()); } try { //Face information processing retCode = ASFFunctions.ASFProcess(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pMultiFaceInfo, FaceEngineMask.ASF_LIVENESS); if (retCode == 0) { //Obtain in vivo test results IntPtr pLivenessInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_LivenessInfo>()); retCode = ASFFunctions.ASFGetLivenessScore(pEngine, pLivenessInfo); Console.WriteLine("Get Liveness Result:" + retCode); ASF_LivenessInfo livenessInfo = MemoryUtil.PtrToStructure <ASF_LivenessInfo>(pLivenessInfo); //Release memory MemoryUtil.Free(pMultiFaceInfo); MemoryUtil.Free(pLivenessInfo); return(livenessInfo); } else { //Release memory MemoryUtil.Free(pMultiFaceInfo); return(new ASF_LivenessInfo()); } } catch { retCode = -1; //Release memory MemoryUtil.Free(pMultiFaceInfo); return(new ASF_LivenessInfo()); } }
/// <summary> /// 获取人脸信息列表 /// </summary> /// <param name="pEngine"></param> /// <param name="bitmap"></param> /// <returns></returns> public static List <FaceInfoModel> GetFaceInfos(IntPtr pEngine, Image bitmap) { List <FaceInfoModel> listRet = new List <FaceInfoModel>(); try { List <int> AgeList = new List <int>(); List <int> GenderList = new List <int>(); //检测人脸,得到Rect框 ASF_MultiFaceInfo multiFaceInfo = FaceUtil.DetectFace(pEngine, bitmap); MultiFaceModel multiFaceModel = new MultiFaceModel(multiFaceInfo); //人脸信息处理 ImageInfo imageInfo = ImageUtil.ReadBMP(bitmap); int retCode = ASFFunctions.ASFProcess(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, ref multiFaceInfo, FaceEngineMask.ASF_AGE | FaceEngineMask.ASF_GENDER); //获取年龄信息 ASF_AgeInfo ageInfo = new ASF_AgeInfo(); retCode = ASFFunctions.ASFGetAge(pEngine, ref ageInfo); AgeList = ageInfo.PtrToAgeArray(ageInfo.ageArray, ageInfo.num); //获取性别信息 ASF_GenderInfo genderInfo = new ASF_GenderInfo(); retCode = ASFFunctions.ASFGetGender(pEngine, ref genderInfo); GenderList = genderInfo.PtrToGenderArray(genderInfo.genderArray, genderInfo.num); for (int i = 0; i < multiFaceInfo.faceNum; i++) { FaceInfoModel faceInfo = new FaceInfoModel(); faceInfo.age = AgeList[i]; faceInfo.gender = GenderList[i]; faceInfo.faceRect = multiFaceModel.FaceInfoList[i].faceRect; faceInfo.feature = ExtractFeature(pEngine, bitmap, multiFaceModel.FaceInfoList[i]);//提取单人脸特征 faceInfo.faceOrient = multiFaceModel.FaceInfoList[i].faceOrient; listRet.Add(faceInfo); } return(listRet);//返回多人脸信息 } catch { return(listRet); } }