zoukankan      html  css  js  c++  java
  • C#版免费离线人脸识别——虹软ArcSoft V3.0

    【温馨提示】 本文共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离线版识别速度极快

    图片来自官网

     

    2. 下载虹软SDK开发包

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

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

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

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

     

    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) ? "引擎初始化成功!
    " : string.Format("引擎初始化失败!错误码为:{0}
    ", retCode));
            }

    4. 注册人脸

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

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

            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
    
    "));
                            }
                            else
                            {
                                AppendText(string.Format("Face landmark detected,[left:{0},right:{1},top:{2},bottom:{3},orient:{4}]
    
    ", 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!
    
    "));
            }

    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 + "
    ");
                            return;
                        }
                    }
                    AppendText("无结果
    ");
                }
            }

    6. 运行效果

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

    7. 总结

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

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

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

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

  • 相关阅读:
    旧VC项目dpiAware支持
    小鹤双拼win10一键恢复布局
    Win10强制程序高DPI缩放设置
    dotnet部署出现Failed to load the dll from [ ... hostfxr.dll], HRESULT: 0x80070057
    linux下快速安装pyenv管理多版本python
    利用docker-compose快速部署测试用数据库服务器
    elastic query match_all 数据目标超过10000条出错 Result window is too large
    Docker for Windows 启动失败,提示Kubernetes证书无效
    windows 下 pyinstaller distutils not included with latest virtualenv (16.4.0)
    windows下python3.7.2内置venv虚拟环境下pyinstaller错误问题
  • 原文地址:https://www.cnblogs.com/hupo376787/p/12450003.html
Copyright © 2011-2022 走看看