美文网首页
【C#】调用虹软人脸识别2.0踩过的坑

【C#】调用虹软人脸识别2.0踩过的坑

作者: 如果天空不蓝 | 来源:发表于2019-03-05 14:15 被阅读0次

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的倍数,如下:

[![复制代码](https://img.haomeiwen.com/i15827081/ce20696c8cf2fe6f.gif?imageMogr2/auto-orient/strip)](javascript:void(0); "复制代码") 

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: &quot;Courier New&quot; !important; font-size: 12px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">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<int, int, IntPtr>(width, bitmap.Height, pImageData);
    } </pre>

[![复制代码](https://img.haomeiwen.com/i15827081/b0b5d9d5994f76da.gif?imageMogr2/auto-orient/strip)](javascript:void(0); "复制代码")

踩了这两个坑后,便愉快的运行了。

话说2.0的优点也不少:
1.特征值缩小了,由原来的20多K变成1032字节(咱就不能变成1024?)
2.比对速度快了很多很多,四线程10万次的比对也只要3秒多钟。

相关文章

网友评论

      本文标题:【C#】调用虹软人脸识别2.0踩过的坑

      本文链接:https://www.haomeiwen.com/subject/lbnnuqtx.html