/// <summary> /// 人脸检测(PS:检测RGB图像的人脸时,必须保证图像的宽度能被4整除,否则会失败) /// </summary> /// <param name="pEngine">引擎Handle</param> /// <param name="imageInfo">图像数据</param> /// <returns>人脸检测结果</returns> public static ASF_MultiFaceInfo DetectFace(IntPtr pEngine, ImageInfo imageInfo) { ASF_MultiFaceInfo multiFaceInfo = new ASF_MultiFaceInfo(); IntPtr pMultiFaceInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_MultiFaceInfo>()); int retCode = ASFFunctions.ASFDetectFaces(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pMultiFaceInfo); multiFaceInfo = MemoryUtil.PtrToStructure <ASF_MultiFaceInfo>(pMultiFaceInfo); return(multiFaceInfo); }
/// <summary> /// 提取单人脸特征 /// </summary> /// <param name="pEngine">人脸识别引擎</param> /// <param name="image">图片</param> /// <param name="singleFaceInfo">单人脸信息</param> /// <returns>单人脸特征</returns> public static IntPtr ExtractFeature(IntPtr pEngine, Image image, ASF_SingleFaceInfo singleFaceInfo) { ImageInfo imageInfo = ImageUtil.ReadBMP(image); // 读取图片 IntPtr pSingleFaceInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_SingleFaceInfo>()); // 申请单人脸结构体指针空间 MemoryUtil.StructureToPtr(singleFaceInfo, pSingleFaceInfo); // 指针化 IntPtr pFaceFeature = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_FaceFeature>()); // 申请人脸特征指针空间 int retCode = -1; try { retCode = ASFFunctions.ASFFaceFeatureExtract(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pSingleFaceInfo, pFaceFeature);// 单人脸特征提取 } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine("FR Extract Feature result:" + retCode); if (retCode != 0) { //释放指针 MemoryUtil.Free(pSingleFaceInfo); MemoryUtil.Free(pFaceFeature); ASF_FaceFeature emptyFeature = new ASF_FaceFeature(); IntPtr pEmptyFeature = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_FaceFeature>()); MemoryUtil.StructureToPtr(emptyFeature, pEmptyFeature); return(pEmptyFeature); } //人脸特征feature过滤 ASF_FaceFeature faceFeature = MemoryUtil.PtrToStructure <ASF_FaceFeature>(pFaceFeature); byte[] feature = new byte[faceFeature.featureSize]; MemoryUtil.Copy(faceFeature.feature, feature, 0, faceFeature.featureSize); ASF_FaceFeature localFeature = new ASF_FaceFeature(); localFeature.feature = MemoryUtil.Malloc(feature.Length); MemoryUtil.Copy(feature, 0, localFeature.feature, feature.Length); localFeature.featureSize = feature.Length; IntPtr pLocalFeature = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_FaceFeature>()); MemoryUtil.StructureToPtr(localFeature, pLocalFeature); //释放指针 MemoryUtil.Free(pSingleFaceInfo); MemoryUtil.Free(pFaceFeature); MemoryUtil.Free(imageInfo.imgData); return(pLocalFeature); }
/// <summary> /// 单人脸性别检测 /// </summary> /// <param name="pEngine">引擎Handle</param> /// <param name="image">图片</param> /// <param name="singleFaceInfo">单人脸信息</param> /// <returns>性别估计结果</returns> public static ASF_GenderInfo GenderEstimation(IntPtr pEngine, Image image, ASF_SingleFaceInfo singleFaceInfo) { ImageInfo imageInfo = ImageUtil.ReadBMP(image); ASF_MultiFaceInfo multiFaceInfo = new ASF_MultiFaceInfo(); multiFaceInfo.faceRects = MemoryUtil.Malloc(MemoryUtil.SizeOf <MRECT>()); MemoryUtil.StructureToPtr <MRECT>(singleFaceInfo.faceRect, multiFaceInfo.faceRects); multiFaceInfo.faceOrients = MemoryUtil.Malloc(MemoryUtil.SizeOf <int>()); MemoryUtil.StructureToPtr <int>(singleFaceInfo.faceOrient, multiFaceInfo.faceOrients); multiFaceInfo.faceNum = 1; ASF_GenderInfo genderInfo = GenderEstimation(pEngine, imageInfo, multiFaceInfo); MemoryUtil.Free(imageInfo.imgData); return(genderInfo); }
/// <summary> /// 获取多个人脸检测结果中面积最大的人脸 /// </summary> /// <param name="multiFaceInfo">人脸检测结果</param> /// <returns>面积最大的人脸信息</returns> public static ASF_SingleFaceInfo GetMaxFace(ASF_MultiFaceInfo multiFaceInfo) { ASF_SingleFaceInfo singleFaceInfo = new ASF_SingleFaceInfo(); singleFaceInfo.faceRect = new MRECT(); singleFaceInfo.faceOrient = 1; int maxArea = 0; int index = -1; for (int i = 0; i < multiFaceInfo.faceNum; i++) { MRECT rect = MemoryUtil.PtrToStructure <MRECT>(multiFaceInfo.faceRects + MemoryUtil.SizeOf <MRECT>() * i); int area = (rect.right - rect.left) * (rect.bottom - rect.top); if (maxArea <= area) { maxArea = area; index = i; } } if (index != -1) { singleFaceInfo.faceRect = MemoryUtil.PtrToStructure <MRECT>(multiFaceInfo.faceRects + MemoryUtil.SizeOf <MRECT>() * index); singleFaceInfo.faceOrient = MemoryUtil.PtrToStructure <int>(multiFaceInfo.faceOrients + MemoryUtil.SizeOf <int>() * index); } return(singleFaceInfo); }