/// <summary> /// Extracting facial features /// </summary> /// <param name="pEngine">Engine Handle</param> /// <param name="imageInfo">picture data</param> /// <param name="multiFaceInfo">Face detection results</param> /// <returns>Save the face feature structure pointer</returns> public static IntPtr ExtractFeature(IntPtr pEngine, ImageInfo imageInfo, ASF_MultiFaceInfo multiFaceInfo, out ASF_SingleFaceInfo singleFaceInfo) { singleFaceInfo = new ASF_SingleFaceInfo(); if (multiFaceInfo.faceRects == null) { ASF_FaceFeature emptyFeature = new ASF_FaceFeature(); IntPtr pEmptyFeature = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_FaceFeature>()); MemoryUtil.StructureToPtr(emptyFeature, pEmptyFeature); return(pEmptyFeature); } singleFaceInfo.faceRect = MemoryUtil.PtrToStructure <MRECT>(multiFaceInfo.faceRects); singleFaceInfo.faceOrient = MemoryUtil.PtrToStructure <int>(multiFaceInfo.faceOrients); IntPtr pSingleFaceInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_SingleFaceInfo>()); MemoryUtil.StructureToPtr(singleFaceInfo, pSingleFaceInfo); IntPtr pFaceFeature = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_FaceFeature>()); int retCode = ASFFunctions.ASFFaceFeatureExtract(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pSingleFaceInfo, pFaceFeature); Console.WriteLine("FR Extract Feature result:" + retCode); if (retCode != 0) { //Release the pointer 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); } //Face feature feature filtering 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); //Release pointer MemoryUtil.Free(pSingleFaceInfo); MemoryUtil.Free(pFaceFeature); return(pLocalFeature); }
/// <summary> /// Get picture information /// </summary> /// <param name="image">photograph</param> /// <returns>Success or failure</returns> public static ImageInfo ReadBMP(Image image) { ImageInfo imageInfo = new ImageInfo(); Image <Bgr, byte> my_Image = null; Bitmap bitmap = null; try { bitmap = new Bitmap(image); if (bitmap == null) { return(null); } my_Image = new Image <Bgr, byte>(bitmap); imageInfo.format = ASF_ImagePixelFormat.ASVL_PAF_RGB24_B8G8R8; imageInfo.width = my_Image.Width; imageInfo.height = my_Image.Height; imageInfo.imgData = MemoryUtil.Malloc(my_Image.Bytes.Length); MemoryUtil.Copy(my_Image.Bytes, 0, imageInfo.imgData, my_Image.Bytes.Length); return(imageInfo); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (my_Image != null) { my_Image.Dispose(); } if (bitmap != null) { bitmap.Dispose(); } } return(null); }
/// <summary> /// Get image IR information /// </summary> /// <param name="image">photograph</param> /// <returns>Success or failure</returns> public static ImageInfo ReadBMP_IR(Bitmap bitmap) { ImageInfo imageInfo = new ImageInfo(); Image <Bgr, byte> my_Image = null; Image <Gray, byte> gray_image = null; try { //Image grayscale conversion my_Image = new Image <Bgr, byte>(bitmap); gray_image = my_Image.Convert <Gray, byte>(); //Graying function imageInfo.format = ASF_ImagePixelFormat.ASVL_PAF_GRAY; imageInfo.width = gray_image.Width; imageInfo.height = gray_image.Height; imageInfo.imgData = MemoryUtil.Malloc(gray_image.Bytes.Length); MemoryUtil.Copy(gray_image.Bytes, 0, imageInfo.imgData, gray_image.Bytes.Length); return(imageInfo); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (my_Image != null) { my_Image.Dispose(); } if (gray_image != null) { gray_image.Dispose(); } } return(null); }
/// <summary> /// Extract single face feature /// </summary> /// <param name="pEngine">Engine Handle</param> /// <param name="image">photograph</param> /// <param name="singleFaceInfo">Single face information</param> /// <returns>Features of single face</returns> public static IntPtr ExtractFeature(IntPtr pEngine, Image image, ASF_SingleFaceInfo singleFaceInfo) { ImageInfo imageInfo = ImageUtil.ReadBMP(image); if (imageInfo == null) { ASF_FaceFeature emptyFeature = new ASF_FaceFeature(); IntPtr pEmptyFeature = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_FaceFeature>()); MemoryUtil.StructureToPtr(emptyFeature, pEmptyFeature); return(pEmptyFeature); } 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) { //Release the pointer MemoryUtil.Free(pSingleFaceInfo); MemoryUtil.Free(pFaceFeature); MemoryUtil.Free(imageInfo.imgData); ASF_FaceFeature emptyFeature = new ASF_FaceFeature(); IntPtr pEmptyFeature = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_FaceFeature>()); MemoryUtil.StructureToPtr(emptyFeature, pEmptyFeature); return(pEmptyFeature); } //Face feature feature filtering 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); //Release the pointer MemoryUtil.Free(pSingleFaceInfo); MemoryUtil.Free(pFaceFeature); MemoryUtil.Free(imageInfo.imgData); return(pLocalFeature); }