/// <summary> /// 人脸检测 /// </summary> /// <param name="pEngine">虹软引擎实例</param> /// <param name="image">图像 可以是window系统的图像对象</param> /// <returns></returns> public static ASF_MultiFaceInfo DetectFace(IntPtr pEngine, Image image) { lock (locks) //上锁 { //创建多张人脸的数据结构体 ASF_MultiFaceInfo multiFaceInfo = new ASF_MultiFaceInfo(); if (image != null) { ImageInfo imageInfo = ImageUtil.ReadBMP(image); if (imageInfo == null) { return(multiFaceInfo); } multiFaceInfo = DetectFace(pEngine, imageInfo); //释放掉图片信息,减少内存占用 MemoryUtil.Free(imageInfo.imgData); imageInfo.imgData = IntPtr.Zero; return(multiFaceInfo); } else { return(multiFaceInfo); } } }
/// <summary> /// 提取单人脸特征 /// </summary> /// <param name="pEngine"></param> /// <param name="image"></param> /// <param name="singleFaceInfo"></param> /// <returns></returns> public static ASF_FaceFeature ExtractFeature(IntPtr pEngine, Image image, ASF_SingleFaceInfo singleFaceInfo) { ImageInfo imageInfo = ImageUtil.ReadBMP(image); if (imageInfo == null) { return(ReturnEmptyFeature()); } 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); } if (retCode != 0) { MemoryUtil.Free(pSingleFaceInfo); MemoryUtil.Free(pFaceFeature); MemoryUtil.Free(imageInfo.imgData); pSingleFaceInfo = IntPtr.Zero; pFaceFeature = IntPtr.Zero; imageInfo.imgData = IntPtr.Zero; return(ReturnEmptyFeature()); } //人脸特征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; //释放指针 MemoryUtil.Free(pSingleFaceInfo); MemoryUtil.Free(pFaceFeature); MemoryUtil.Free(imageInfo.imgData); pSingleFaceInfo = IntPtr.Zero; imageInfo.imgData = IntPtr.Zero; return(localFeature); }
/// <summary> /// 提取人脸特征 /// </summary> /// <param name="pEngine"></param> /// <param name="image"></param> /// <param name="singleFaceInfo"></param> /// <returns></returns> public static IntPtr ExtractFeatrue(IntPtr pEngine, Image image, out ASF_SingleFaceInfo singleFaceInfo) { if (image == null) { return(ReturnEmptyStruct(out singleFaceInfo)); } if (image.Width > 1536 || image.Height > 1536) { image = ImageUtil.ScaleImage(image, 1536, 1536); } else { image = ImageUtil.ScaleImage(image, image.Width, image.Height); } ImageInfo imageInfo = ImageUtil.ReadBMP(image); if (imageInfo == null) { return(ReturnEmptyStruct(out singleFaceInfo)); } //进行人脸检测 ASF_MultiFaceInfo multiFaceInfo = DetectFace(pEngine, imageInfo); singleFaceInfo = new ASF_SingleFaceInfo(); IntPtr pFaceModel = ExtractFeatrue(pEngine, imageInfo, multiFaceInfo, out singleFaceInfo); MemoryUtil.Free(imageInfo.imgData); imageInfo.imgData = IntPtr.Zero; return(pFaceModel); }
public static ASF_MultiFaceInfo DetectFace(IntPtr pEngine, Image image) { lock (locks) { ASF_MultiFaceInfo multiFaceInfo = new ASF_MultiFaceInfo(); if (image != null) { if (image == null) { return(multiFaceInfo); } ImageInfo imageInfo = ImageUtil.ReadBMP(image); if (imageInfo == null) { return(multiFaceInfo); } multiFaceInfo = DetectFace(pEngine, imageInfo); MemoryUtil.Free(imageInfo.imgData); return(multiFaceInfo); } else { return(multiFaceInfo); } } }
public static ASF_MultiFaceInfo DetectFace(IntPtr pEngine, Image image) { lock (locks) { ASF_MultiFaceInfo multiFaceInfo = new ASF_MultiFaceInfo(); if (image != null) { if (image.Width > 1536 || image.Height > 1536) { image = ImageUtil.ScaleImage(image, 1536, 1536); } else { image = ImageUtil.ScaleImage(image, image.Width, image.Height); } if (image == null) { return(multiFaceInfo); } ImageInfo imageInfo = ImageUtil.ReadBMP(image); if (imageInfo == null) { return(multiFaceInfo); } multiFaceInfo = DetectFace(pEngine, imageInfo); MemoryUtil.Free(imageInfo.imgData); return(multiFaceInfo); } else { return(multiFaceInfo); } } }
/// <summary> /// 提取人脸特征 /// </summary> /// <param name="pEngine">引擎Handle</param> /// <param name="image">图像</param> /// <returns>保存人脸特征结构体指针</returns> public static IntPtr ExtractFeature(IntPtr pEngine, Image image, out ASF_SingleFaceInfo singleFaceInfo) { image = ImageUtil.ScaleImage(image, image.Width, image.Height); ImageInfo imageInfo = ImageUtil.ReadBMP(image); ASF_MultiFaceInfo multiFaceInfo = DetectFace(pEngine, imageInfo); singleFaceInfo = new ASF_SingleFaceInfo(); IntPtr pFaceModel = ExtractFeature(pEngine, imageInfo, multiFaceInfo, out singleFaceInfo); MemoryUtil.Free(imageInfo.imgData); return(pFaceModel); }
/// <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="pEngine">引擎Handle</param> /// <param name="image">图像</param> /// <returns>保存人脸特征结构体指针</returns> public static IntPtr ExtractFeature(IntPtr pEngine, Image image, out ASF_SingleFaceInfo singleFaceInfo) { if (image.Width > 1536 || image.Height > 1536) { image = ImageUtil.ScaleImage(image, 1536, 1536); } else { image = ImageUtil.ScaleImage(image, image.Width, image.Height); } if (image == null) { singleFaceInfo = new ASF_SingleFaceInfo(); ASF_FaceFeature emptyFeature = new ASF_FaceFeature(); IntPtr pEmptyFeature = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_FaceFeature>()); MemoryUtil.StructureToPtr(emptyFeature, pEmptyFeature); return(pEmptyFeature); } ImageInfo imageInfo = ImageUtil.ReadBMP(image); if (imageInfo == null) { singleFaceInfo = new ASF_SingleFaceInfo(); ASF_FaceFeature emptyFeature = new ASF_FaceFeature(); IntPtr pEmptyFeature = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_FaceFeature>()); MemoryUtil.StructureToPtr(emptyFeature, pEmptyFeature); return(pEmptyFeature); } ASF_MultiFaceInfo multiFaceInfo = DetectFace(pEngine, imageInfo); singleFaceInfo = new ASF_SingleFaceInfo(); IntPtr pFaceModel = ExtractFeature(pEngine, imageInfo, multiFaceInfo, out singleFaceInfo); MemoryUtil.Free(imageInfo.imgData); return(pFaceModel); }