public static FaceInfo[] doFaceDetection(IntPtr hFDEngine, ASVLOFFSCREEN inputImg) { FaceInfo[] faceInfo = new FaceInfo[0]; IntPtr pFaceRes = IntPtr.Zero; IntPtr ret = AFD_FSDKLibrary.AFD_FSDK_StillImageFaceDetection(hFDEngine, ref (inputImg), ref (pFaceRes)); if (ret.ToInt64() != 0) { Console.WriteLine(String.Format("AFD_FSDK_StillImageFaceDetection 0x{0:x}", ret)); return(faceInfo); } AFD_FSDK_FACERES faceRes = (AFD_FSDK_FACERES)Marshal.PtrToStructure(pFaceRes, typeof(AFD_FSDK_FACERES)); if (faceRes.nFace > 0) { faceInfo = new FaceInfo[faceRes.nFace]; for (int i = 0; i < faceRes.nFace; i++) { MRECT rect = (MRECT)Marshal.PtrToStructure(faceRes.rcFace + i * Marshal.SizeOf(typeof(MRECT)), typeof(MRECT)); int orient = Marshal.ReadInt32(faceRes.lfaceOrient + 4 * i); faceInfo[i] = new FaceInfo(); faceInfo[i].left = rect.left; faceInfo[i].top = rect.top; faceInfo[i].right = rect.right; faceInfo[i].bottom = rect.bottom; faceInfo[i].orient = orient; Console.WriteLine(String.Format("{0} ({1} {2} {3} {4}) orient {5}", i, rect.left, rect.top, rect.right, rect.bottom, orient)); } } return(faceInfo); }
public static AFR_FSDK_FACEMODEL extractFRFeature(IntPtr hFREngine, ASVLOFFSCREEN inputImg, FaceInfo faceInfo) { AFR_FSDK_FACEINPUT faceinput = new AFR_FSDK_FACEINPUT(); faceinput.lOrient = faceInfo.orient; faceinput.rcFace.left = faceInfo.left; faceinput.rcFace.top = faceInfo.top; faceinput.rcFace.right = faceInfo.right; faceinput.rcFace.bottom = faceInfo.bottom; AFR_FSDK_FACEMODEL faceFeature = new AFR_FSDK_FACEMODEL(IntPtr.Zero, 0); IntPtr ret = AFR_FSDKLibrary.AFR_FSDK_ExtractFRFeature(hFREngine, ref (inputImg), ref (faceinput), ref (faceFeature)); if (ret.ToInt64() != 0) { Console.WriteLine(String.Format("AFR_FSDK_ExtractFRFeature ret 0x{0:x}", ret)); return(new AFR_FSDK_FACEMODEL(IntPtr.Zero, 0)); } try { return(faceFeature.deepCopy()); } catch (Exception e) { Console.WriteLine(e.ToString()); return(new AFR_FSDK_FACEMODEL(IntPtr.Zero, 0)); } }
/// <summary> /// 人脸特征提取 /// </summary> /// <param name="image">人脸图像</param> /// <param name="faceRes">人脸信息检测结果</param> /// <returns>接口返回码,返回0表示正常,返回其他值请在开发者中心-帮助中心查询</returns> public int ArcSoft_FIC_FaceDataFeatureExtraction(Image image, ref FaceRes faceRes) { faceRes = new FaceRes(); IntPtr pFaceRes = MemoryUtil.Malloc(MemoryUtil.SizeOf <AFICFSDKFACERES>()); ASVLOFFSCREEN imagetemp = ImageUtil.ReadBmp(image, false); IntPtr pInputFaceData = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASVLOFFSCREEN>()); MemoryUtil.StructToPtr(imagetemp, pInputFaceData); //调用SDK的人脸特征提取接口 int retCode = FICFunctions.ArcSoft_FIC_FaceDataFeatureExtraction(pEngine, mode, pInputFaceData, pFaceRes); if (!retCode.Equals(0)) { MemoryUtil.Free(pFaceRes); MemoryUtil.Free(pInputFaceData); MemoryUtil.Free(imagetemp.ppu8Plane[0]); return(retCode); } AFICFSDKFACERES faceResStruct = MemoryUtil.PtrToStruct <AFICFSDKFACERES>(pFaceRes); faceRes.nFace = faceResStruct.nFace; faceRes.rcFace = faceResStruct.rcFace; MemoryUtil.Free(pFaceRes); MemoryUtil.Free(pInputFaceData); MemoryUtil.Free(imagetemp.ppu8Plane[0]); return(retCode); }
/// <summary> /// 获取IR图片信息 /// </summary> /// <param name="image">图片</param> /// <returns>图片数据</returns> public static ASVLOFFSCREEN ReadBmpIR(Image image, bool checkWidth = true) { if (checkWidth && image.Width % 4 != 0) { image = ScaleImage(image, image.Width - (image.Width % 4), image.Height); } ASVLOFFSCREEN offInput = new ASVLOFFSCREEN(); Bitmap bm = new Bitmap(image); BitmapData data = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); try { //位图中第一个像素数据的地址。它也可以看成是位图中的第一个扫描行 IntPtr ptr = data.Scan0; //定义数组长度 int soureBitArrayLength = data.Height * Math.Abs(data.Stride); byte[] sourceBitArray = new byte[soureBitArrayLength]; //将bitmap中的内容拷贝到数组中 MemoryUtil.Copy(ptr, 0, sourceBitArray, soureBitArrayLength); //填充引用对象字段值 offInput.i32Width = data.Width; offInput.i32Height = data.Height; offInput.u32PixelArrayFormat = (int)ASF_ImagePixelFormat.ASVL_PAF_GRAY; //获取去除对齐位后度图像数据 int ir_len = offInput.i32Width * offInput.i32Height; byte[] destBitArray = new byte[ir_len]; offInput.pi32Pitch = new int[4]; offInput.pi32Pitch[0] = offInput.i32Width; //灰度化 int j = 0; double colortemp = 0; for (int i = 0; i < sourceBitArray.Length; i += 3) { colortemp = sourceBitArray[i + 2] * 0.299 + sourceBitArray[i + 1] * 0.587 + sourceBitArray[i] * 0.114; destBitArray[j++] = (byte)colortemp; } offInput.ppu8Plane = new IntPtr[4]; offInput.ppu8Plane[0] = MemoryUtil.Malloc(destBitArray.Length); MemoryUtil.Copy(destBitArray, 0, offInput.ppu8Plane[0], destBitArray.Length); return(offInput); } catch (Exception e) { Console.WriteLine(e); throw; } finally { bm.UnlockBits(data); bm.Dispose(); } }
/// <summary> /// 获取RGB图片信息 /// </summary> /// <param name="image">图片</param> /// <returns>图片数据</returns> public static ImageInfo ReadBMP(Bitmap bm) { CheckImageWidthAndHeight(ref bm); //调整图像宽度,需要宽度为4的倍数 if (bm.Width % 4 != 0) { bm = ScaleImage(bm, bm.Width - (bm.Width % 4), bm.Height); } ImageInfo imageInfo = new ImageInfo(); //将Image转换为Format24bppRgb格式的BMP BitmapData data = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); try { //位图中第一个像素数据的地址。它也可以看成是位图中的第一个扫描行 IntPtr ptr = data.Scan0; //定义数组长度 int soureBitArrayLength = data.Height * Math.Abs(data.Stride); byte[] sourceBitArray = new byte[soureBitArrayLength]; //将bitmap中的内容拷贝到ptr_bgr数组中 Marshal.Copy(ptr, sourceBitArray, 0, soureBitArrayLength); //填充引用对象字段值 imageInfo.width = data.Width; imageInfo.height = data.Height; imageInfo.format = (int)ASF_ImagePixelFormat.ASVL_PAF_RGB24_B8G8R8; //步长的设置 imageInfo.widthStep = data.Stride; imageInfo.imgData = Marshal.AllocHGlobal(sourceBitArray.Length); ASVLOFFSCREEN asfImageData = new ASVLOFFSCREEN(); asfImageData.i32Width = imageInfo.width; asfImageData.i32Height = imageInfo.height; asfImageData.u32PixelArrayFormat = (uint)imageInfo.format; asfImageData.pi32Pitch = new int[4]; asfImageData.ppu8Plane = new IntPtr[4]; asfImageData.pi32Pitch[0] = imageInfo.widthStep; asfImageData.ppu8Plane[0] = imageInfo.imgData; imageInfo.ASFImageData = asfImageData; Marshal.Copy(sourceBitArray, 0, imageInfo.imgData, sourceBitArray.Length); return(imageInfo); } catch (Exception e) { throw e; } finally { bm.UnlockBits(data); } }
private void pictureBox1_Click(object sender, EventArgs e) { int width = 0; int height = 0; int pitch = 0; OpenFileDialog op1 = new OpenFileDialog(); if (op1.ShowDialog() == DialogResult.OK) { pictureBox1.ImageLocation = op1.FileName; } Bitmap bitmap = new Bitmap(pictureBox1.ImageLocation); // IntPtr imageDataPtr= bitmap.GetHbitmap(); byte[] imageData = readBmp(bitmap, ref width, ref height, ref pitch); IntPtr imageDataPtr = Marshal.AllocHGlobal(imageData.Length); Marshal.Copy(imageData, 0, imageDataPtr, imageData.Length); ASVLOFFSCREEN offInput = new ASVLOFFSCREEN(); offInput.u32PixelArrayFormat = 513; offInput.ppu8Plane = new IntPtr[4]; offInput.ppu8Plane[0] = imageDataPtr; offInput.i32Width = width; offInput.i32Height = height; offInput.pi32Pitch = new int[4]; offInput.pi32Pitch[0] = pitch; faceRes = new AFD_FSDK_FACERES(); IntPtr offInputPtr = Marshal.AllocHGlobal(Marshal.SizeOf(offInput)); Marshal.StructureToPtr(offInput, offInputPtr, false); IntPtr faceResPtr = Marshal.AllocHGlobal(Marshal.SizeOf(faceRes)); code = Init.ArcSoft_FIC_FaceDataFeatureExtraction(detectEngine, 0, offInputPtr, ref faceResPtr); }
/// <summary> /// 获取图片信息 /// </summary> /// <param name="image"></param> /// <returns></returns> public static ASVLOFFSCREEN ReadBmp(Image image) { ASVLOFFSCREEN offInput = new ASVLOFFSCREEN(); //将Image转换为Format24bppRgb格式的BMP Bitmap bm = new Bitmap(image); //将Bitmap锁定到系统内存中,获得BitmapData BitmapData data = bm.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); //位图中第一个像素数据的地址。它也可以看成是位图中的第一个扫描行 IntPtr ptr = data.Scan0; //定义数组长度 int soureBitArrayLength = data.Height * Math.Abs(data.Stride); byte[] sourceBitArray = new byte[soureBitArrayLength]; //将bitmap中的内容拷贝到ptr_bgr数组中 MemoryUtil.Copy(ptr, sourceBitArray, 0, soureBitArrayLength); int width = data.Width; int height = data.Height; int pitch = Math.Abs(data.Stride); //获取去除对齐位后度图像数据 int line = width * 3; int bgr_len = line * height; byte[] destBitArray = new byte[bgr_len]; /* * 图片像素数据在内存中是按行存储,一般图像库都会有一个内存对齐,在每行像素的末尾位置 * 每行的对齐位会使每行多出一个像素空间(三通道如RGB会多出3个字节,四通道RGBA会多出4个字节) * 以下循环目的是去除每行末尾的对齐位,将有效的像素拷贝到新的数组 */ for (int i = 0; i < height; ++i) { Array.Copy(sourceBitArray, i * pitch, destBitArray, i * line, line); } pitch = line; bm.UnlockBits(data); IntPtr imageDataPtr = MemoryUtil.Malloc(destBitArray.Length); MemoryUtil.Copy(destBitArray, 0, imageDataPtr, destBitArray.Length); offInput.u32PixelArrayFormat = ASF_ImagePixelFormat.ASVL_PAF_RGB24_B8G8R8; offInput.ppu8Plane = new IntPtr[4]; offInput.ppu8Plane[0] = imageDataPtr; offInput.i32Width = width; offInput.i32Height = height; offInput.pi32Pitch = new int[4]; offInput.pi32Pitch[0] = pitch; return(offInput); }
/// <summary> /// 证件照特征提取 /// </summary> /// <param name="image">证件照</param> /// <returns>接口返回码,返回0表示正常,返回其他值请在开发者中心-帮助中心查询</returns> public int ArcSoft_FIC_IdCardDataFeatureExtraction(Image image) { int retCode = -1; if (image != null) { ASVLOFFSCREEN imagetemp = ImageUtil.ReadBmp(image); IntPtr pInputIdcardData = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASVLOFFSCREEN>()); MemoryUtil.StructToPtr(imagetemp, pInputIdcardData); //调用SDK retCode = FICFunctions.ArcSoft_FIC_IdCardDataFeatureExtraction(pEngine, pInputIdcardData); MemoryUtil.Free(pInputIdcardData); MemoryUtil.Free(imagetemp.ppu8Plane[0]); } return(retCode); }
/// <summary> /// 人脸信息(IR活体)检测 /// </summary> /// <param name="bitmap">人脸图像</param> /// <param name="faceRes">人脸信息</param> /// <param name="combinedMask">检测属性,默认IR活体检测</param> /// <returns>接口返回码,返回0表示正常,返回其他值请在开发者中心-帮助中心查询</returns> public int ArcSoft_FIC_Process_IR(Bitmap bitmap, FaceRes faceRes, int combinedMask = EngineMask.AFIC_LIVENESS_IR) { int retCode = -1; ASVLOFFSCREEN imageinfo = ImageUtil.ReadBmpIR(bitmap); AFICFSDKFACERES sFacerects = new AFICFSDKFACERES(); sFacerects.nFace = faceRes.nFace; sFacerects.rcFace = faceRes.rcFace; IntPtr pImageInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASVLOFFSCREEN>()); MemoryUtil.StructToPtr(imageinfo, pImageInfo); IntPtr pFacerects = MemoryUtil.Malloc(MemoryUtil.SizeOf <AFICFSDKFACERES>()); MemoryUtil.StructToPtr(sFacerects, pFacerects); //调用SDK retCode = FICFunctions.ArcSoft_FIC_Process_IR(pEngine, pImageInfo, pFacerects, combinedMask); MemoryUtil.Free(imageinfo.ppu8Plane[0]); MemoryUtil.Free(pImageInfo); MemoryUtil.Free(pFacerects); return(retCode); }
/// <summary> /// 获取RGB图片信息 /// </summary> /// <param name="image">RGB图片</param> /// <returns></returns> public static ASVLOFFSCREEN ReadBmp(Image image, bool checkWidth = true) { if (checkWidth && image.Width % 4 != 0) { image = ScaleImage(image, image.Width - (image.Width % 4), image.Height); } ASVLOFFSCREEN offInput = new ASVLOFFSCREEN(); //将Image转换为Format24bppRgb格式的BMP Bitmap bm = new Bitmap(image); //将Bitmap锁定到系统内存中,获得BitmapData System.Drawing.Imaging.BitmapData data = bm.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); //位图中第一个像素数据的地址。它也可以看成是位图中的第一个扫描行 IntPtr ptr = data.Scan0; //定义数组长度 int soureBitArrayLength = data.Height * Math.Abs(data.Stride); byte[] sourceBitArray = new byte[soureBitArrayLength]; //将bitmap中的内容拷贝到ptr_bgr数组中 MemoryUtil.Copy(ptr, 0, sourceBitArray, soureBitArrayLength); int width = data.Width; int height = data.Height; int pitch = Math.Abs(data.Stride); bm.UnlockBits(data); bm.Dispose(); IntPtr imageDataPtr = MemoryUtil.Malloc(sourceBitArray.Length); MemoryUtil.Copy(sourceBitArray, 0, imageDataPtr, sourceBitArray.Length); offInput.u32PixelArrayFormat = (uint)ASF_ImagePixelFormat.ASVL_PAF_RGB24_B8G8R8; offInput.ppu8Plane = new IntPtr[4]; offInput.ppu8Plane[0] = imageDataPtr; offInput.i32Width = width; offInput.i32Height = height; offInput.pi32Pitch = new int[4]; offInput.pi32Pitch[0] = pitch; return(offInput); }
/// <summary> /// 证件照特征提取 /// </summary> /// <param name="hFICEngine">FIC 引擎Handle</param> /// <param name="isVideo">人脸数据类型 1-视频 0-静态图片</param> /// <param name="bitmap">人脸图像原始数据</param> /// <returns>人脸特征提取结果</returns> public static int IdCardDataFeatureExtraction(IntPtr hFICEngine, Image image) { lock (locks) { if (image.Width % 4 != 0) { image = ImageUtil.ScaleImage(image, image.Width - (image.Width % 4), image.Height); } //Bitmap bitmap = new Bitmap(image); ASVLOFFSCREEN offInput = ImageUtil.ReadBmp(image); IntPtr offInputPtr = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASVLOFFSCREEN>()); MemoryUtil.StructureToPtr(offInput, offInputPtr); IntPtr faceResPtr = MemoryUtil.Malloc(MemoryUtil.SizeOf <AFIC_FSDK_FACERES>()); int result = ASIDCardFunctions.ArcSoft_FIC_IdCardDataFeatureExtraction(hFICEngine, offInputPtr); MemoryUtil.Free(offInput.ppu8Plane[0]); MemoryUtil.Free(offInputPtr); MemoryUtil.Free(faceResPtr); return(result); } }
/// <summary> /// 人脸特征提取 /// </summary> /// <param name="hFICEngine">FIC 引擎Handle</param> /// <param name="isVideo">人脸数据类型 1-视频 0-静态图片</param> /// <param name="bitmap">人脸图像原始数据</param> /// <returns>人脸检测结果</returns> public static int FaceDataFeatureExtraction(IntPtr hFICEngine, bool isVideo, Bitmap bitmap, ref AFIC_FSDK_FACERES faceRes) { lock (locks) { int result = -1; if (bitmap != null) { ASVLOFFSCREEN offInput = ImageUtil.ReadBmp(bitmap); IntPtr offInputPtr = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASVLOFFSCREEN>()); MemoryUtil.StructureToPtr(offInput, offInputPtr); IntPtr faceResPtr = MemoryUtil.Malloc(MemoryUtil.SizeOf <AFIC_FSDK_FACERES>()); result = ASIDCardFunctions.ArcSoft_FIC_FaceDataFeatureExtraction(hFICEngine, isVideo, offInputPtr, faceResPtr); faceRes = MemoryUtil.PtrToStructure <AFIC_FSDK_FACERES>(faceResPtr); MemoryUtil.Free(offInput.ppu8Plane[0]); MemoryUtil.Free(offInputPtr); MemoryUtil.Free(faceResPtr); } return(result); } }
public static ASVLOFFSCREEN loadImage(String filePath) { ASVLOFFSCREEN inputImg = new ASVLOFFSCREEN(); inputImg.pi32Pitch = new int[4]; inputImg.ppu8Plane = new IntPtr[4]; if (bUseBGRToEngine) { BufferInfo bufferInfo = ImageLoader.getBGRFromFile(filePath); inputImg.u32PixelArrayFormat = ASVL_COLOR_FORMAT.ASVL_PAF_RGB24_B8G8R8; inputImg.i32Width = bufferInfo.width; inputImg.i32Height = bufferInfo.height; inputImg.pi32Pitch[0] = bufferInfo.stride; inputImg.ppu8Plane[0] = Marshal.AllocHGlobal(inputImg.pi32Pitch[0] * inputImg.i32Height); Marshal.Copy(bufferInfo.buffer, 0, inputImg.ppu8Plane[0], inputImg.pi32Pitch[0] * inputImg.i32Height); inputImg.ppu8Plane[1] = IntPtr.Zero; inputImg.ppu8Plane[2] = IntPtr.Zero; inputImg.ppu8Plane[3] = IntPtr.Zero; } else { BufferInfo bufferInfo = ImageLoader.getI420FromFile(filePath); inputImg.u32PixelArrayFormat = ASVL_COLOR_FORMAT.ASVL_PAF_I420; inputImg.i32Width = bufferInfo.width; inputImg.i32Height = bufferInfo.height; inputImg.pi32Pitch[0] = inputImg.i32Width; inputImg.pi32Pitch[1] = inputImg.i32Width / 2; inputImg.pi32Pitch[2] = inputImg.i32Width / 2; inputImg.ppu8Plane[0] = Marshal.AllocHGlobal(inputImg.pi32Pitch[0] * inputImg.i32Height); Marshal.Copy(bufferInfo.buffer, 0, inputImg.ppu8Plane[0], inputImg.pi32Pitch[0] * inputImg.i32Height); inputImg.ppu8Plane[1] = Marshal.AllocHGlobal(inputImg.pi32Pitch[1] * inputImg.i32Height / 2); Marshal.Copy(bufferInfo.buffer, inputImg.pi32Pitch[0] * inputImg.i32Height, inputImg.ppu8Plane[1], inputImg.pi32Pitch[1] * inputImg.i32Height / 2); inputImg.ppu8Plane[2] = Marshal.AllocHGlobal(inputImg.pi32Pitch[2] * inputImg.i32Height / 2); Marshal.Copy(bufferInfo.buffer, inputImg.pi32Pitch[0] * inputImg.i32Height + inputImg.pi32Pitch[1] * inputImg.i32Height / 2, inputImg.ppu8Plane[2], inputImg.pi32Pitch[2] * inputImg.i32Height / 2); inputImg.ppu8Plane[3] = IntPtr.Zero; } return(inputImg); }
public static ASVLOFFSCREEN loadRAWImage(String yuv_filePath, int yuv_width, int yuv_height, int yuv_format) { int yuv_rawdata_size = 0; ASVLOFFSCREEN inputImg = new ASVLOFFSCREEN(); inputImg.pi32Pitch = new int[4]; inputImg.ppu8Plane = new IntPtr[4]; inputImg.u32PixelArrayFormat = yuv_format; inputImg.i32Width = yuv_width; inputImg.i32Height = yuv_height; if (ASVL_COLOR_FORMAT.ASVL_PAF_I420 == inputImg.u32PixelArrayFormat) { inputImg.pi32Pitch[0] = inputImg.i32Width; inputImg.pi32Pitch[1] = inputImg.i32Width / 2; inputImg.pi32Pitch[2] = inputImg.i32Width / 2; yuv_rawdata_size = inputImg.i32Width * inputImg.i32Height * 3 / 2; } else if (ASVL_COLOR_FORMAT.ASVL_PAF_NV12 == inputImg.u32PixelArrayFormat) { inputImg.pi32Pitch[0] = inputImg.i32Width; inputImg.pi32Pitch[1] = inputImg.i32Width; yuv_rawdata_size = inputImg.i32Width * inputImg.i32Height * 3 / 2; } else if (ASVL_COLOR_FORMAT.ASVL_PAF_NV21 == inputImg.u32PixelArrayFormat) { inputImg.pi32Pitch[0] = inputImg.i32Width; inputImg.pi32Pitch[1] = inputImg.i32Width; yuv_rawdata_size = inputImg.i32Width * inputImg.i32Height * 3 / 2; } else if (ASVL_COLOR_FORMAT.ASVL_PAF_YUYV == inputImg.u32PixelArrayFormat) { inputImg.pi32Pitch[0] = inputImg.i32Width * 2; yuv_rawdata_size = inputImg.i32Width * inputImg.i32Height * 2; } else if (ASVL_COLOR_FORMAT.ASVL_PAF_RGB24_B8G8R8 == inputImg.u32PixelArrayFormat) { inputImg.pi32Pitch[0] = inputImg.i32Width * 3; yuv_rawdata_size = inputImg.i32Width * inputImg.i32Height * 3; } else { Console.WriteLine("unsupported yuv format"); Environment.Exit(0); } //load YUV Image Data from File byte[] imagedata = new byte[yuv_rawdata_size]; FileStream f = new FileStream(yuv_filePath, FileMode.Open); BinaryReader br = null; try { br = new BinaryReader(f); br.Read(imagedata, 0, yuv_rawdata_size); } catch (Exception e) { Console.WriteLine(e.ToString()); Console.WriteLine(e.Message); Environment.Exit(0); } finally { try { if (br != null) { br.Close(); } } catch (Exception e) { } } if (ASVL_COLOR_FORMAT.ASVL_PAF_I420 == inputImg.u32PixelArrayFormat) { inputImg.ppu8Plane[0] = Marshal.AllocHGlobal(inputImg.pi32Pitch[0] * inputImg.i32Height); Marshal.Copy(imagedata, 0, inputImg.ppu8Plane[0], inputImg.pi32Pitch[0] * inputImg.i32Height); inputImg.ppu8Plane[1] = Marshal.AllocHGlobal(inputImg.pi32Pitch[1] * inputImg.i32Height / 2); Marshal.Copy(imagedata, inputImg.pi32Pitch[0] * inputImg.i32Height, inputImg.ppu8Plane[1], inputImg.pi32Pitch[1] * inputImg.i32Height / 2); inputImg.ppu8Plane[2] = Marshal.AllocHGlobal(inputImg.pi32Pitch[2] * inputImg.i32Height / 2); Marshal.Copy(imagedata, inputImg.pi32Pitch[0] * inputImg.i32Height + inputImg.pi32Pitch[1] * inputImg.i32Height / 2, inputImg.ppu8Plane[2], inputImg.pi32Pitch[2] * inputImg.i32Height / 2); inputImg.ppu8Plane[3] = IntPtr.Zero; } else if (ASVL_COLOR_FORMAT.ASVL_PAF_NV12 == inputImg.u32PixelArrayFormat) { inputImg.ppu8Plane[0] = Marshal.AllocHGlobal(inputImg.pi32Pitch[0] * inputImg.i32Height); Marshal.Copy(imagedata, 0, inputImg.ppu8Plane[0], inputImg.pi32Pitch[0] * inputImg.i32Height); inputImg.ppu8Plane[1] = Marshal.AllocHGlobal(inputImg.pi32Pitch[1] * inputImg.i32Height / 2); Marshal.Copy(imagedata, inputImg.pi32Pitch[0] * inputImg.i32Height, inputImg.ppu8Plane[1], inputImg.pi32Pitch[1] * inputImg.i32Height / 2); inputImg.ppu8Plane[2] = IntPtr.Zero; inputImg.ppu8Plane[3] = IntPtr.Zero; } else if (ASVL_COLOR_FORMAT.ASVL_PAF_NV21 == inputImg.u32PixelArrayFormat) { inputImg.ppu8Plane[0] = Marshal.AllocHGlobal(inputImg.pi32Pitch[0] * inputImg.i32Height); Marshal.Copy(imagedata, 0, inputImg.ppu8Plane[0], inputImg.pi32Pitch[0] * inputImg.i32Height); inputImg.ppu8Plane[1] = Marshal.AllocHGlobal(inputImg.pi32Pitch[1] * inputImg.i32Height / 2); Marshal.Copy(imagedata, inputImg.pi32Pitch[0] * inputImg.i32Height, inputImg.ppu8Plane[1], inputImg.pi32Pitch[1] * inputImg.i32Height / 2); inputImg.ppu8Plane[2] = IntPtr.Zero; inputImg.ppu8Plane[3] = IntPtr.Zero; } else if (ASVL_COLOR_FORMAT.ASVL_PAF_YUYV == inputImg.u32PixelArrayFormat) { inputImg.ppu8Plane[0] = Marshal.AllocHGlobal(inputImg.pi32Pitch[0] * inputImg.i32Height); Marshal.Copy(imagedata, 0, inputImg.ppu8Plane[0], inputImg.pi32Pitch[0] * inputImg.i32Height); inputImg.ppu8Plane[1] = IntPtr.Zero; inputImg.ppu8Plane[2] = IntPtr.Zero; inputImg.ppu8Plane[3] = IntPtr.Zero; } else if (ASVL_COLOR_FORMAT.ASVL_PAF_RGB24_B8G8R8 == inputImg.u32PixelArrayFormat) { inputImg.ppu8Plane[0] = Marshal.AllocHGlobal(inputImg.pi32Pitch[0] * inputImg.i32Height); Marshal.Copy(imagedata, 0, inputImg.ppu8Plane[0], inputImg.pi32Pitch[0] * inputImg.i32Height); inputImg.ppu8Plane[1] = IntPtr.Zero; inputImg.ppu8Plane[2] = IntPtr.Zero; inputImg.ppu8Plane[3] = IntPtr.Zero; } else { Console.WriteLine("unsupported yuv format"); Environment.Exit(0); } return(inputImg); }
public static float compareFaceSimilarity(IntPtr hFDEngine, IntPtr hFREngine, ASVLOFFSCREEN inputImgA, ASVLOFFSCREEN inputImgB) { // Do Face Detect FaceInfo[] faceInfosA = doFaceDetection(hFDEngine, inputImgA); if (faceInfosA.Length < 1) { Console.WriteLine("no face in Image A "); return(0.0f); } FaceInfo[] faceInfosB = doFaceDetection(hFDEngine, inputImgB); if (faceInfosB.Length < 1) { Console.WriteLine("no face in Image B "); return(0.0f); } // Extract Face Feature AFR_FSDK_FACEMODEL faceFeatureA = extractFRFeature(hFREngine, inputImgA, faceInfosA[0]); if (faceFeatureA.pbFeature == IntPtr.Zero) { Console.WriteLine("extract face feature in Image A failed"); return(0.0f); } AFR_FSDK_FACEMODEL faceFeatureB = extractFRFeature(hFREngine, inputImgB, faceInfosB[0]); if (faceFeatureB.pbFeature == IntPtr.Zero) { Console.WriteLine("extract face feature in Image B failed"); faceFeatureA.freeUnmanaged(); return(0.0f); } // calc similarity between faceA and faceB float fSimilScore = 0.0f; IntPtr ret = AFR_FSDKLibrary.AFR_FSDK_FacePairMatching(hFREngine, ref (faceFeatureA), ref (faceFeatureB), ref (fSimilScore)); faceFeatureA.freeUnmanaged(); faceFeatureB.freeUnmanaged(); if (ret.ToInt64() != 0) { Console.WriteLine(String.Format("AFR_FSDK_FacePairMatching failed:ret 0x{0:x}", ret)); return(0.0f); } return(fSimilScore); }
public static extern MResult ASFFaceFeatureExtractEx(IntPtr hEngine, ASVLOFFSCREEN imgData, ASF_SingleFaceInfo faceInfo, out ASF_FaceFeature feature, int threadNum = 1);
public static extern MResult ASFUpdateFaceDataEx(IntPtr hEngine, ASVLOFFSCREEN imgData, out ASF_MultiFaceInfo detectedFaces);
public static extern MResult ASFImageQualityDetectEx(IntPtr hEngine, ASVLOFFSCREEN imgData, ASF_SingleFaceInfo faceInfo, int isMask, out float confidenceLevel, ASF_DetectModel detectModel = ASF_DetectModel.ASF_DETECT_MODEL_RGB);
public static Boolean checkAndMarkFace(Bitmap image) { // Bitmap image = a; BitmapData data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); //位图中第一个像素数据的地址。它也可以看成是位图中的第一个扫描行 IntPtr ptr = data.Scan0; //定义数组长度 int soureBitArrayLength = data.Height * Math.Abs(data.Stride); byte[] sourceBitArray = new byte[soureBitArrayLength]; //将bitmap中的内容拷贝到ptr_bgr数组中 Marshal.Copy(ptr, sourceBitArray, 0, soureBitArrayLength); int width = data.Width; int height = data.Height; int pitch = Math.Abs(data.Stride); image.UnlockBits(data); data = null; int line = width * 3; int bgr_len = line * height; byte[] destBitArray = new byte[bgr_len]; for (int i = 0; i < height; ++i) { Array.Copy(sourceBitArray, i * pitch, destBitArray, i * line, line); } pitch = line; // image.UnlockBits(data); byte[] imageData = destBitArray;//readBmp(image, ref width, ref height, ref pitch); IntPtr imageDataPtr = Marshal.AllocHGlobal(imageData.Length); Marshal.Copy(imageData, 0, imageDataPtr, imageData.Length); ASVLOFFSCREEN offInput = new ASVLOFFSCREEN(); offInput.u32PixelArrayFormat = 513; offInput.ppu8Plane = new IntPtr[4]; offInput.ppu8Plane[0] = imageDataPtr; offInput.i32Width = width; offInput.i32Height = height; offInput.pi32Pitch = new int[4]; offInput.pi32Pitch[0] = pitch; IntPtr offInputPtr = Marshal.AllocHGlobal(Marshal.SizeOf(offInput)); Marshal.StructureToPtr(offInput, offInputPtr, false); AFD_FSDK_FACERES faceRes = new AFD_FSDK_FACERES(); IntPtr faceResPtr = Marshal.AllocHGlobal(Marshal.SizeOf(faceRes)); int detectResult; if (!IS32) { detectResult = AFDFunction.AFD_FSDK_StillImageFaceDetection(detectEngine, offInputPtr, ref faceResPtr); } else { detectResult = AFDFunction.AFD_FSDK_StillImageFaceDetection32(detectEngine, offInputPtr, ref faceResPtr); } if (detectResult != 0) { //return false; } faceRes = (AFD_FSDK_FACERES)Marshal.PtrToStructure(faceResPtr, typeof(AFD_FSDK_FACERES)); bool flag = (faceRes.nFace > 0 && faceRes.nFace < 10); Marshal.FreeHGlobal(imageDataPtr); Marshal.FreeHGlobal(offInputPtr); return(flag); }
public static extern MResult ASFFaceFeatureExtractEx(IntPtr hEngine, ASVLOFFSCREEN imgData, ASF_SingleFaceInfo faceInfo, ASF_RegisterOrNot registerOrNot, int mask, out ASF_FaceFeature feature);
public static extern MResult ASFProcessEx(IntPtr hEngine, ASVLOFFSCREEN imgData, ASF_MultiFaceInfo detectedFaces, int combinedMask);
public static extern MResult ASFDetectFacesEx(IntPtr hEngine, ASVLOFFSCREEN imgData, out ASF_MultiFaceInfo detectedFaces, ASF_DetectModel detectModel = ASF_DetectModel.ASF_DETECT_MODEL_RGB);
public static extern MResult ASFProcessEx_IR(IntPtr hEngine, ASVLOFFSCREEN imgData, ASF_MultiFaceInfo detectedFaces, ASF_Mask combinedMask = ASF_Mask.ASF_IR_LIVENESS);