C#版免费离线人脸识别——虹软ArcSoft

  • 1,302 views
  • 阅读模式

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

1. 前言

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

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

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

 web apiWindows SDKAndroid SDKiOS SDK离线使用价格速度
微软认知服务✔️收费取决于网速
旷视Face++✔️✔️✔️✔️收费web版取决于网速本地SDK离线版识别速度没测试过,但应该很快
OpenCV✔️✔️✔️✔️免费有点慢

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

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

 web api Windows SDK Android SDK iOS SDK 离线使用 价格 速度 
虹软人脸识别 ✔️ ✔️ ✔️✔️✔️免费版 - 需要在线激活收费版 - 离线激活,提供更多高级服务  web版取决于网速本地SDK离线版识别速度极快

图片来自官网

C#版免费离线人脸识别——虹软ArcSoft
C#版免费离线人脸识别——虹软ArcSoft
C#版免费离线人脸识别——虹软ArcSoft

2. 下载虹软SDK开发包

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

这里主要讲一下Windows下的SDK使用。

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

C#版免费离线人脸识别——虹软ArcSoft

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

C#版免费离线人脸识别——虹软ArcSoft
C#版免费离线人脸识别——虹软ArcSoft

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
        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

7. 总结

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

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

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

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

作者:猫叔Vincent

weinxin
扫码关注微信公众号--IT老五
微信扫一扫关注公众号,获取更多实用app,订阅地址不定时更新
IT老五
  • 版权声明 本文源自 猫叔Vincent 整理 发表于 2022-04-29 19:31:00
  • 转载请务必保留本文链接:https://itlao5.com/8714.html
评论  1  访客  1
    • bb
      bb 0

      垃圾,关键的东西都没说,免费版的只能申请一台机器的授权,直接说是学习版不就好了

    匿名

    发表评论

    匿名网友 填写信息

    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

    确定