C# 基于Arcface SDK实现人脸识别和注册

本文档介绍了如何使用C#结合虹软Arcface SDK进行人脸识别和注册的步骤。主要内容包括准备工作,如DLL封装,以及在Windows Form中设计用户界面,包括VideoSourcePlayer、PictureBox、Label和TextBox等组件的使用,实现人脸检测和ID注册功能。
摘要由CSDN通过智能技术生成

整个项目使用虹软技术完成开发

一,准备工作

1.Afoge视频参数类

using AForge.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FaceRecognization.Common
{
    public class CameraPara
    {
        /// <summary>
        /// 是否有摄像头
        /// </summary>
        public bool HasVideoDevice { get; set; }
        /// <summary>
        /// 视频源
        /// </summary>
        public VideoCaptureDevice VideoSource { get; set; }
        /// <summary>
        /// 视频图片的宽度
        /// </summary>
        public int FrameWidth { get; set; }
        /// <summary>
        /// 视频图片的高度
        /// </summary>
        public int FrameHeight { get; set; }
        /// <summary>
        /// 视频图片的字节数
        /// </summary>
        public int ByteCount { get; set; }
        public CameraPara()
        {
            var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            if (videoDevices.Count == 0)//没有检测到摄像头
            {
                HasVideoDevice = false;
                return;
            }

            VideoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);//连接第一个摄像头
            var videoResolution = VideoSource.VideoCapabilities.First(ii => ii.FrameSize.Width == VideoSource.VideoCapabilities.Max(jj => jj.FrameSize.Width)); //获取摄像头最高的分辨率

            FrameWidth = videoResolution.FrameSize.Width;
            FrameHeight = videoResolution.FrameSize.Height;
            ByteCount = videoResolution.BitCount / 8;
            VideoSource.VideoResolution = videoResolution;
            HasVideoDevice = true;
        }

    }
}

2.人脸识别相关的结构、类 和枚举

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace FaceRecognization.Face
{
    /// <summary>
    /// 人脸库
    /// </summary>
    public class FaceLib
    {
        public List<Item> Items { get; set; } = new List<Item>();
        public class Item
        {
            /// <summary>
            /// 用于排序
            /// </summary>
            public long OrderId { get; set; }
            /// <summary>
            /// 文件名作为ID
            /// </summary>
            public string ID { get; set; }
            /// <summary>
            /// 人脸模型
            /// </summary>
            public FaceModel FaceModel { get; set; }
        }
    }
    /// <summary>
    /// 人脸识别结果
    /// </summary>
    public class FaceResult
    {
        //public int NotMatchedCount { get; set; }
        public string ID { get; set; }
        public float Score { get; set; }
        public System.Drawing.Rectangle Rectangle { get; set; }
        public int Age { get; set; }
        /// <summary>
        /// 0:男,1:女,其他:未知
        /// </summary>
        public int Gender { get; set; }
        public override string ToString()
        {

            string ret = "";
            if (!string.IsNullOrEmpty(ID))
                ret = ID + ",";
            ret += Age + "岁";
            if (Gender == 0)
                ret += ",男";
            else if (Gender == 1)
                ret += ",女";

            return ret + "," + Score;
        }
    }

    /// <summary>
    /// 人脸跟踪、检测、性别年龄评估和获取人脸信息的输入参数
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct ImageData
    {
        public uint u32PixelArrayFormat;
        public int i32Width;
        public int i32Height;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public IntPtr[] ppu8Plane;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = UnmanagedType.I4)]
        public int[] pi32Pitch;
    }
    /// <summary>
    /// 人脸跟踪的结果
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    internal struct TraceResult
    {
        [MarshalAs(UnmanagedType.I4)]
        public int nFace;
        [MarshalAs(UnmanagedType.I4)]
        public int lfaceOrient;
        public IntPtr rcFace;
    }
    /// <summary>
    /// 人脸检测的结果
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    internal struct DetectResult
    {
        [MarshalAs(UnmanagedType.I4)]
        public int nFace;
        public IntPtr rcFace;
        public IntPtr lfaceOrient;
    }

    /// <summary>
    /// 人脸在图片中的位置
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct FaceRect
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }
    /// <summary>
    /// 获取人脸特征的输入参数
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    internal struct FaceFeatureInput
    {
        public FaceRect rcFace;
        public int lOrient;
    }
    /// <summary>
    /// 人脸特征
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct FaceModel
    {
        public IntPtr pbFeature;
        [MarshalAs(UnmanagedType.I4)]
        public int lFeatureSize;
    }
    /// <summary>
    /// 性别和年龄评估的输入参数
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    internal struct EstimationInput
    {
        public IntPtr pFaceRectArray;
        public IntPtr pFaceOrientArray;
        public int lFaceNumber;
    }
    /// <summary>
    /// 性别和年龄评估的结果
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    internal struct EstimationResult
    {
        public IntPtr pResult;
        public int lFaceNumber;
    }

    /// <summary>
    /// 错误代码
    /// </summary>
    public enum ErrorCode
    {
        /// <summary>
        /// 正确
        /// </summary>
        Ok = 0,

        /// <summary>
        /// 通用错误类型
        /// </summary>
        BasicBase = 0x0001,

        /// <summary>
        /// 错误原因不明
        /// </summary>
        Unknown = BasicBase,

        /// <summary>
        /// 无效的参数
        /// </summary>
        InvalidParam = BasicBase + 1,

        /// <summary>
        /// 引擎不支持
        /// </summary>
        Unsupported = BasicBase + 2,

        /// <summary>
        /// 内存不足
        /// </summary>
        NoMemory = BasicBase + 3,

        /// <summary>
        /// 状态错误
        /// </summary>
        BadState = BasicBase + 4,

        /// <summary>
        /// 用户取消相关操作
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值