1.初始化
[DllImport(“libarcsoft_face_engine.dll”, EntryPoint = “ASFInitEngine”, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]private static extern int ASFInitEngine(uint detectMode, int orientPriority, int scale, int maxFaceNumber, uint combinedMask, out IntPtr pEngine);开始时,写了个long detectMode,返回错误信息是:0x16004,(detectFaceScaleVal 不支持)正式鬼扯,害我各种调整scale。改成uint就ok了。话说就一个mode,您弄const uint ASF_DETECT_MODE_VIDEO = 0x00000000; //Video模式,一般用于多帧连续检测const uint ASF_DETECT_MODE_IMAGE = 0xFFFFFFFF; //Image模式,一般用于静态图的单次检测这两宝贝,我也是醉了。0/1不行吗?2.人脸检测[DllImport(“libarcsoft_face_engine.dll”, EntryPoint = “ASFDetectFaces”, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]private static extern int ASFDetectFaces(IntPtr pEngine, int width, int height, int format, IntPtr pImageData, out ASF_MultiFaceInfo faceInfo);开始时,弄了张身份证照片,102的宽度,不是4的倍数,返回0x1600F,手动编辑图片,拉伸成104的宽度便告成功。最终写了个方法,自动设成4的倍数,如下:private (int W, int H, IntPtr PImageData) GetImageData(Bitmap bitmap){ var bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); int width = (bitmap.Width + 3) / 4 * 4; var bytesCount = bmpData.Height * width * 3; IntPtr pImageData = Marshal.AllocCoTaskMem(bytesCount); if (width == bitmap.Width) CopyMemory(pImageData, bmpData.Scan0, bytesCount); else for (int i = 0; i < bitmap.Height; i++) CopyMemory(IntPtr.Add(pImageData, i * width * 3), IntPtr.Add(bmpData.Scan0, i * bmpData.Stride), bmpData.Stride); bitmap.UnlockBits(bmpData); return new ValueTuple(width, bitmap.Height, pImageData); }
踩了这两个坑后,便愉快的运行了。
话说2.0的优点也不少:
1.特征值缩小了,由原来的20多K变成1032字节(咱就不能变成1024?)2.比对速度快了很多很多,四线程10万次的比对也只要3秒多钟。