欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#版免费离线人脸识别之虹软ArcSoft V3.0(推荐)

程序员文章站 2022-03-21 14:08:00
【温馨提示】本文共678字(不含代码),8张图。预计阅读时间需要6分钟。1. 前言人脸识别&比对发展到今天,已经是一个非常成熟的技术了,而且应用在生活的方方面面,比如手机、车站、天网等。我从2...

【温馨提示】本文共678字(不含代码),8张图。预计阅读时间需要6分钟。

1. 前言

人脸识别&比对发展到今天,已经是一个非常成熟的技术了,而且应用在生活的方方面面,比如手机、车站、天网等。

我从2016年就开始做人脸识别相关的app,到现在差不多4个年头了,用过的sdk有微软认知服务、旷视科技的face++、开源的opencv。

这里就之前我用过的做一下对比。

web api windows sdk android sdk ios sdk 离线使用 价格 速度
微软认知服务 ✔️ 收费 取决于网速
旷视face++ ✔️ ✔️ ✔️ ✔️ 收费

web版取决于网速

本地sdk离线版识别速度没测试过,但应该很快

opencv ✔️ ✔️ ✔️ ✔️ 免费 有点慢

而今天介绍的这个虹软人脸识别服务,是免费的、免费的、免费的。

最重要的是它还支持离线识别,并且提供android、ios、c++、c#版sdk,现在已经升级到全新的3.0版本,支持活体识别。

web api  windows sdk  android sdk  ios sdk  离线使用  价格  速度 
虹软人脸识别  ✔️  ✔️  ✔️ ✔️ ✔️

免费版 - 需要在线激活

收费版 - 离线激活,提供更多高级服务 

web版取决于网速

本地sdk离线版识别速度极快

图片来自官网

C#版免费离线人脸识别之虹软ArcSoft V3.0(推荐)

C#版免费离线人脸识别之虹软ArcSoft V3.0(推荐)

C#版免费离线人脸识别之虹软ArcSoft V3.0(推荐)

2. 下载虹软sdk开发包

你可以去https://ai.arcsoft.com.cn/ucenter/resource/build/index.html#/index注册一个账号,然后就可以申请使用虹软离线sdk。

这里主要讲一下windows下的sdk使用。

注意win下面分为x86和x64两个版本,所以在编译app的时候不要选择any cpu,而是选择和你下载的一样的架构。

C#版免费离线人脸识别之虹软ArcSoft V3.0(推荐)

新建一个winform解决方案,选择编译架构,把你下载的sdk/lib里面的文件放进对应的debug目录。

C#版免费离线人脸识别之虹软ArcSoft V3.0(推荐)

C#版免费离线人脸识别之虹软ArcSoft V3.0(推荐)

3. 初始化识别引擎

sdk需要一个id和key,这些你都可以在虹软开发者中心申请到。

private void initengines()
        {//在线激活引擎    如出现错误,1.请先确认从官网下载的sdk库已放到对应的bin中,2.当前选择的cpu为x86或者x64
            int retcode = 0;
            try
            {
                retcode = asffunctions.asfactivation(appid, sdkkey);
            }
            catch (exception ex)
            {
                //禁用相关功能按钮
                //controlsenable(false, choosemultiimgbtn, matchbtn, btnclearfacelist, chooseimgbtn);
                if (ex.message.contains("无法加载 dll"))
                {
                    messagebox.show("请将sdk相关dll放入bin对应的x86或x64下的文件夹中!");
                }
                else
                {
                    messagebox.show("激活引擎失败!");
                }
                return;
            }
            console.writeline("activate result:" + retcode);

            //初始化引擎
            uint detectmode = detectionmode.asf_detect_mode_image;//image模式下检测脸部的角度优先值
            int imagedetectfaceorientpriority = asf_orientpriority.asf_op_0_only;
            //人脸在图片中所占比例,如果需要调整检测人脸尺寸请修改此值,有效数值为2-32
            int detectfacescaleval = 16;
            //最大需要检测的人脸个数
            int detectfacemaxnum = 5;
            //引擎初始化时需要初始化的检测功能组合
            int combinedmask = faceenginemask.asf_face_detect | faceenginemask.asf_facerecognition | faceenginemask.asf_age | faceenginemask.asf_gender | faceenginemask.asf_face3dangle;
            //初始化引擎,正常值为0,其他返回值请参考http://ai.arcsoft.com.cn/bbs/forum.php?mod=viewthread&tid=19&_dsign=dbad527e
            retcode = asffunctions.asfinitengine(detectmode, imagedetectfaceorientpriority, detectfacescaleval, detectfacemaxnum, combinedmask, ref pimageengine);
            console.writeline("initengine result:" + retcode);
            appendtext((retcode == 0) ? "引擎初始化成功!\r\n" : string.format("引擎初始化失败!错误码为:{0}\r\n", retcode));
        }

4. 注册人脸

要想识别人脸,首相要像指纹识别那样,把一个人的人脸事先录入进去,才可以实现识别。

我这里做一个简单的demo,输入一个名字,选择照片即可注册。

C#版免费离线人脸识别之虹软ArcSoft V3.0(推荐)

  private void btnselectimagetoregister_click(object sender, eventargs e)
        {
            openfiledialog openfiledialog = new openfiledialog();
            openfiledialog.title = "select";
            openfiledialog.filter = "image file|*.bmp;*.jpg;*.jpeg;*.png";
            //openfiledialog.multiselect = true;
            openfiledialog.filename = string.empty;
            if (openfiledialog.showdialog() == dialogresult.ok)
            {
                var numstart = imagepathlist.count;
                string filename = openfiledialog.filename;
                if (!checkimage(filename))
                    return;

                pictureboxselected.imagelocation = filename;
                currentleftfeature = intptr.zero;

                //人脸检测以及提取人脸特征
                threadpool.queueuserworkitem(new waitcallback(delegate
                {
                    image image = imageutil.readfromfile(filename);
                    if (image == null)
                    {
                        return;
                    }
                    if (image.width > 1536 || image.height > 1536)
                    {
                        image = imageutil.scaleimage(image, 1536, 1536);
                    }
                    if (image == null)
                    {
                        return;
                    }
                    if (image.width % 4 != 0)
                    {
                        image = imageutil.scaleimage(image, image.width - (image.width % 4), image.height);
                    }

                    //人脸检测
                    asf_multifaceinfo multifaceinfo = faceutil.detectface(pimageengine, image);
                    //判断检测结果
                    if (multifaceinfo.facenum > 0)
                    {
                        mrect rect = memoryutil.ptrtostructure<mrect>(multifaceinfo.facerects);
                        image = imageutil.cutimage(image, rect.left, rect.top, rect.right, rect.bottom);
                    }
                    else
                    {
                        if (image != null)
                        {
                            image.dispose();
                        }
                        return;
                    }

                    //提取人脸特征
                    asf_singlefaceinfo singlefaceinfo = new asf_singlefaceinfo();
                    image image1 = imageutil.readfromfile(filename);
                    if (image1 == null)
                    {
                        return;
                    }
                    currentleftfeature = faceutil.extractfeature(pimageengine, image1, out singlefaceinfo);
                    this.invoke(new action(delegate
                    {
                        if (singlefaceinfo.facerect.left == 0 && singlefaceinfo.facerect.right == 0)
                        {
                            appendtext(string.format("no face detected\r\r\n"));
                        }
                        else
                        {
                            appendtext(string.format("face landmark detected,[left:{0},right:{1},top:{2},bottom:{3},orient:{4}]\r\r\n", singlefaceinfo.facerect.left, singlefaceinfo.facerect.right, singlefaceinfo.facerect.top, singlefaceinfo.facerect.bottom, singlefaceinfo.faceorient));
                            imagesfeaturelist.add(currentleftfeature);
                        }
                    }));
                    if (image1 != null)
                    {
                        image1.dispose();
                    }

                }));
            }
        }

        private void btnregisterface_click(object sender, eventargs e)
        {
            if(string.isnullorempty(textboxname.text))
            {
                messagebox.show("set a name for current person");
                return;
            }

            imagesfeaturedictionary.add(currentleftfeature, textboxname.text);
            appendtext(string.format(textboxname.text + " register success!\r\r\n"));
        }

5. 人脸识别

当把许多人脸录入到系统中后,我们就可以选择一个需要比对的图片,进行识别了。

private void btnselectimagetorecognize_click(object sender, eventargs e)
        {
            openfiledialog openfiledialog = new openfiledialog();
            openfiledialog.title = "select";
            openfiledialog.filter = "image file|*.bmp;*.jpg;*.jpeg;*.png";
            //openfiledialog.multiselect = true;
            openfiledialog.filename = string.empty;
            if (openfiledialog.showdialog() == dialogresult.ok)
            {
                var numstart = imagepathlist.count;
                string filename = openfiledialog.filename;
                if (!checkimage(filename))
                    return;

                image1feature = intptr.zero;
                pictureboxtorecognize.imagelocation = filename;
                image srcimage = imageutil.readfromfile(filename);

                asf_singlefaceinfo singlefaceinfo = new asf_singlefaceinfo();
                //提取人脸特征
                image1feature = faceutil.extractfeature(pimageengine, srcimage, out singlefaceinfo);

                if (imagesfeaturelist.count == 0)
                {
                    messagebox.show("请注册人脸!", "提示", messageboxbuttons.ok, messageboxicon.error);
                    return;
                }

                if (image1feature == intptr.zero)
                {
                    if (pictureboxtorecognize.image == null)
                    {
                        messagebox.show("请选择识别图!", "提示", messageboxbuttons.ok, messageboxicon.error);
                    }
                    else
                    {
                        messagebox.show("比对失败,识别图未提取到特征值!", "提示", messageboxbuttons.ok, messageboxicon.error);
                    }
                    return;
                }

                for (int i = 0; i < imagesfeaturedictionary.count; i++)
                {
                    intptr feature = imagesfeaturedictionary.elementat(i).key;
                    float similarity = 0f;
                    int ret = asffunctions.asffacefeaturecompare(pimageengine, image1feature, feature, ref similarity);
                    //增加异常值处理
                    if (similarity.tostring().indexof("e") > -1)
                        similarity = 0f;

                    if(similarity > threshold)
                    {
                        string name = imagesfeaturedictionary.elementat(i).value;
                        appendtext("对比结果:" + name + "  可信度:" + similarity + "\r\n");
                        return;
                    }
                }
                appendtext("无结果\r\n");
            }
        }

6. 运行效果

本地离线识别最大的好处就是没有延迟,识别结果立马呈现。

C#版免费离线人脸识别之虹软ArcSoft V3.0(推荐)

7. 总结

本文只是简单介绍了如何使用虹软的离线sdk,进行人脸识别的方法,并且是图片的方式。

源码下载地址:https://github.com/hupo376787/arcfacedemo.git

如果需要摄像头,那么需要别的摄像头sdk来辅助实现。

如果以后有时间我会加上。

到此这篇关于c#版免费离线人脸识别——虹软arcsoft v3.0的文章就介绍到这了,更多相关c#离线人脸识别虹软内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!